Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7.0.3 #482

Merged
merged 4 commits into from
May 2, 2024
Merged

7.0.3 #482

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/browser-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"lint": "eslint src/**/*.js --fix"
},
"devDependencies": {
"@types/chrome": "^0.0.260",
"@types/firefox-webext-browser": "^0.0.260",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/browser-extension/src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Cookie Dialog Monster",
"version": "7.0.2",
"version": "7.0.3",
"default_locale": "en",
"description": "__MSG_appDesc__",
"icons": {
Expand All @@ -16,6 +16,7 @@
"options_page": "options.html",
"author": "wanhose",
"background": {
"scripts": ["scripts/background.js"],
"service_worker": "scripts/background.js"
},
"content_scripts": [
Expand Down
66 changes: 35 additions & 31 deletions packages/browser-extension/src/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (typeof browser === 'undefined') {
browser = chrome;
}

/**
* @description API URL
* @type {string}
Expand All @@ -23,16 +27,16 @@ const reportMenuItemId = 'CDM-REPORT';
const settingsMenuItemId = 'CDM-SETTINGS';

/**
* @description A shortcut for chrome.scripting
* @type {chrome.scripting}
* @description A shortcut for browser.scripting
* @type {browser.scripting}
*/
const script = chrome.scripting;
const script = browser.scripting;

/**
* @description The storage to use
* @type {chrome.storage.LocalStorageArea}
* @type {browser.storage.LocalStorageArea}
*/
const storage = chrome.storage.local;
const storage = browser.storage.local;

/**
* @description Refresh data
Expand All @@ -42,7 +46,7 @@ const refreshData = (callback) => {
try {
fetch(`${apiUrl}/data/`).then((result) => {
result.json().then(({ data }) => {
chrome.storage.local.set({ data }, suppressLastError);
storage.set({ data }, suppressLastError);
callback?.(data);
});
});
Expand All @@ -55,14 +59,14 @@ const refreshData = (callback) => {
* @async
* @description Report active tab URL
* @param {any} message
* @param {chrome.tabs.Tab} tab
* @param {browser.tabs.Tab} tab
* @param {void?} callback
*/
const report = async (message, tab, callback) => {
try {
const reason = message.reason;
const userAgent = message.userAgent;
const version = chrome.runtime.getManifest().version;
const version = browser.runtime.getManifest().version;
const body = JSON.stringify({ reason, url: tab.url, userAgent, version });
const headers = { 'Content-type': 'application/json' };
const url = `${apiUrl}/report/`;
Expand All @@ -75,22 +79,22 @@ const report = async (message, tab, callback) => {
};

/**
* @description Supress `chrome.runtime.lastError`
* @description Supress `browser.runtime.lastError`
*/
const suppressLastError = () => void chrome.runtime.lastError;
const suppressLastError = () => void browser.runtime.lastError;

/**
* @description Listen to context menus clicked
*/
chrome.contextMenus.onClicked.addListener((info, tab) => {
browser.contextMenus.onClicked.addListener((info, tab) => {
const tabId = tab?.id;

switch (info.menuItemId) {
case reportMenuItemId:
if (tabId) chrome.tabs.sendMessage(tabId, { type: 'SHOW_REPORT_DIALOG' }, suppressLastError);
if (tabId) browser.tabs.sendMessage(tabId, { type: 'SHOW_REPORT_DIALOG' }, suppressLastError);
break;
case settingsMenuItemId:
chrome.runtime.openOptionsPage();
browser.runtime.openOptionsPage();
break;
default:
break;
Expand All @@ -100,26 +104,26 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
/**
* @description Listens to messages
*/
chrome.runtime.onMessage.addListener((message, sender, callback) => {
browser.runtime.onMessage.addListener((message, sender, callback) => {
const hostname = message.hostname;
const isPage = sender.frameId === 0;
const tabId = sender.tab?.id;

switch (message.type) {
case 'DISABLE_ICON':
if (isPage && tabId) {
chrome.action.setIcon({ path: '/assets/icons/disabled.png', tabId }, suppressLastError);
chrome.action.setBadgeText({ tabId, text: '' });
browser.action.setIcon({ path: '/assets/icons/disabled.png', tabId }, suppressLastError);
browser.action.setBadgeText({ tabId, text: '' });
}
break;
case 'ENABLE_ICON':
if (isPage && tabId) {
chrome.action.setIcon({ path: '/assets/icons/enabled.png', tabId }, suppressLastError);
browser.action.setIcon({ path: '/assets/icons/enabled.png', tabId }, suppressLastError);
}
break;
case 'ENABLE_POPUP':
if (isPage && tabId) {
chrome.action.setPopup({ popup: '/popup.html', tabId }, suppressLastError);
browser.action.setPopup({ popup: '/popup.html', tabId }, suppressLastError);
}
break;
case 'GET_DATA':
Expand Down Expand Up @@ -148,7 +152,7 @@ chrome.runtime.onMessage.addListener((message, sender, callback) => {
}
break;
case 'GET_TAB':
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => {
callback(tabs[0]);
});
return true;
Expand All @@ -165,8 +169,8 @@ chrome.runtime.onMessage.addListener((message, sender, callback) => {
break;
case 'SET_BADGE':
if (tabId) {
chrome.action.setBadgeBackgroundColor({ color: '#6b7280' });
chrome.action.setBadgeText({ tabId, text: message.value });
browser.action.setBadgeBackgroundColor({ color: '#6b7280' });
browser.action.setBadgeText({ tabId, text: message.value });
}
break;
case 'SET_HOSTNAME_STATE':
Expand All @@ -184,33 +188,33 @@ chrome.runtime.onMessage.addListener((message, sender, callback) => {
/**
* @description Listens to extension installed
*/
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create(
browser.runtime.onInstalled.addListener(() => {
browser.contextMenus.create(
{
contexts: ['all'],
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
documentUrlPatterns: browser.runtime.getManifest().content_scripts[0].matches,
id: extensionMenuItemId,
title: 'Cookie Dialog Monster',
},
suppressLastError
);
chrome.contextMenus.create(
browser.contextMenus.create(
{
contexts: ['all'],
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
documentUrlPatterns: browser.runtime.getManifest().content_scripts[0].matches,
id: settingsMenuItemId,
parentId: extensionMenuItemId,
title: chrome.i18n.getMessage('contextMenu_settingsOption'),
title: browser.i18n.getMessage('contextMenu_settingsOption'),
},
suppressLastError
);
chrome.contextMenus.create(
browser.contextMenus.create(
{
contexts: ['all'],
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
documentUrlPatterns: browser.runtime.getManifest().content_scripts[0].matches,
id: reportMenuItemId,
parentId: extensionMenuItemId,
title: chrome.i18n.getMessage('contextMenu_reportOption'),
title: browser.i18n.getMessage('contextMenu_reportOption'),
},
suppressLastError
);
Expand All @@ -219,6 +223,6 @@ chrome.runtime.onInstalled.addListener(() => {
/**
* @description Listen to first start
*/
chrome.runtime.onStartup.addListener(() => {
browser.runtime.onStartup.addListener(() => {
refreshData();
});
10 changes: 7 additions & 3 deletions packages/browser-extension/src/scripts/content.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (typeof browser === 'undefined') {
browser = chrome;
}

/**
* @typedef {Object} ExtensionData
* @property {string[]} commonWords
Expand Down Expand Up @@ -26,7 +30,7 @@ let { commonWords, fixes = [], skips, tokens } = {};
/**
* @description Shortcut to send messages to background script
*/
const dispatch = chrome.runtime.sendMessage;
const dispatch = browser.runtime.sendMessage;

/**
* @description Event name
Expand Down Expand Up @@ -350,9 +354,9 @@ const observer = new MutationObserver((mutations) => {

/**
* @description Listen to messages from any other scripts
* @listens chrome.tabs#onMessage
* @listens browser.runtime#onMessage
*/
chrome.runtime.onMessage.addListener((message) => {
browser.runtime.onMessage.addListener((message) => {
switch (message.type) {
case 'RESTORE': {
restoreDOM();
Expand Down
28 changes: 16 additions & 12 deletions packages/browser-extension/src/scripts/dialog.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (typeof browser === 'undefined') {
browser = chrome;
}

/**
* @description Report reasons
* @type {string[]}
Expand Down Expand Up @@ -41,54 +45,54 @@ const reportDialogHtml = `
<report-dialog-body>
<report-dialog-form-view>
<report-dialog-body-text>
${chrome.i18n.getMessage('reportDialog_bodyText')}
${browser.i18n.getMessage('reportDialog_bodyText')}
</report-dialog-body-text>
<report-dialog-form>
<report-dialog-radio-group>
<report-dialog-radio
aria-checked="false"
data-value="0" role="radio"
tabindex="0">
${chrome.i18n.getMessage('reportDialog_cannotClickOption')}
${browser.i18n.getMessage('reportDialog_cannotClickOption')}
</report-dialog-radio>
<report-dialog-radio
aria-checked="false"
data-value="1"
role="radio"
tabindex="0">
${chrome.i18n.getMessage('reportDialog_pageVisualGlitchOption')}
${browser.i18n.getMessage('reportDialog_pageVisualGlitchOption')}
</report-dialog-radio>
<report-dialog-radio
aria-checked="false"
data-value="2"
role="radio"
tabindex="0">
${chrome.i18n.getMessage('reportDialog_blankPageOption')}
${browser.i18n.getMessage('reportDialog_blankPageOption')}
</report-dialog-radio>
<report-dialog-radio
aria-checked="false"
data-value="3"
role="radio"
tabindex="0">
${chrome.i18n.getMessage('reportDialog_laggyPageOption')}
${browser.i18n.getMessage('reportDialog_laggyPageOption')}
</report-dialog-radio>
<report-dialog-radio
aria-checked="false"
data-value="4"
role="radio"
tabindex="0">
${chrome.i18n.getMessage('reportDialog_pageNotRespondingOption')}
${browser.i18n.getMessage('reportDialog_pageNotRespondingOption')}
</report-dialog-radio>
<report-dialog-radio
aria-checked="false"
data-value="5"
role="radio"
tabindex="0">
${chrome.i18n.getMessage('reportDialog_popupShowUpOption')}
${browser.i18n.getMessage('reportDialog_popupShowUpOption')}
</report-dialog-radio>
</report-dialog-radio-group>
<report-dialog-submit-button aria-disabled="true" role="button" tabindex="0">
${chrome.i18n.getMessage('contextMenu_reportOption')?.replace('...', '')}
${browser.i18n.getMessage('contextMenu_reportOption')?.replace('...', '')}
</report-dialog-submit-button>
</report-dialog-form>
</report-dialog-form-view>
Expand All @@ -107,13 +111,13 @@ const reportDialogHtml = `
<polyline points="22 4 12 14.01 9 11.01" />
</svg>
<report-dialog-submit-text>
${chrome.i18n.getMessage('reportDialog_submitText')}
${browser.i18n.getMessage('reportDialog_submitText')}
</report-dialog-submit-text>
<report-dialog-submit-extra-text>
${chrome.i18n.getMessage('reportDialog_submitExtraText')}
${browser.i18n.getMessage('reportDialog_submitExtraText')}
</report-dialog-submit-extra-text>
<report-dialog-issue-button role="button" tabindex="0">
${chrome.i18n.getMessage('contextMenu_issueOption')}
${browser.i18n.getMessage('contextMenu_issueOption')}
</report-dialog-issue-button>
</report-dialog-submit-view>
</report-dialog-body>
Expand Down Expand Up @@ -212,7 +216,7 @@ async function submitButtonClickHandler(event) {
/**
* @description Listen to messages
*/
chrome.runtime.onMessage.addListener((message) => {
browser.runtime.onMessage.addListener((message) => {
const isPage = window === window.top;

switch (message.type) {
Expand Down
12 changes: 8 additions & 4 deletions packages/browser-extension/src/scripts/options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
if (typeof browser === 'undefined') {
browser = chrome;
}

/**
* @description Shortcut to send messages to background script
*/
const dispatch = chrome.runtime.sendMessage;
const dispatch = browser.runtime.sendMessage;

/**
* @description Domain RegExp
Expand Down Expand Up @@ -54,7 +58,7 @@ function createList() {
* @returns {Promise<void>}
*/
async function handleAddClick() {
const exclusionValue = window.prompt(chrome.i18n.getMessage('options_addPrompt'));
const exclusionValue = window.prompt(browser.i18n.getMessage('options_addPrompt'));

if (exclusionValue?.trim() && (domainRx.test(exclusionValue) || exclusionValue === 'localhost')) {
const filterInputElement = document.getElementById('filter-input');
Expand Down Expand Up @@ -216,11 +220,11 @@ function translate() {
const { i18n, i18nPlaceholder } = node.dataset;

if (i18n) {
node.innerHTML = chrome.i18n.getMessage(i18n);
node.innerHTML = browser.i18n.getMessage(i18n);
}

if (i18nPlaceholder) {
node.setAttribute('placeholder', chrome.i18n.getMessage(i18nPlaceholder));
node.setAttribute('placeholder', browser.i18n.getMessage(i18nPlaceholder));
}
}
}
Expand Down