fix(browser-extension): overall performance, also actions count
This commit is contained in:
parent
54d6bbe684
commit
9a6ad202c4
@ -38,11 +38,26 @@ const script = browser.scripting;
|
|||||||
*/
|
*/
|
||||||
const storage = browser.storage.local;
|
const storage = browser.storage.local;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Supress `browser.runtime.lastError`
|
||||||
|
*/
|
||||||
|
const suppressLastError = () => void browser.runtime.lastError;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Map match to pattern format
|
||||||
|
* @param {string} match
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function matchToPattern(match) {
|
||||||
|
return `^${match.replaceAll('*.', '*(.)?').replaceAll('*', '.*')}$`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Refresh data
|
* @description Refresh data
|
||||||
* @param {void?} callback
|
* @param {void?} callback
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
const refreshData = (callback) => {
|
function refreshData(callback) {
|
||||||
try {
|
try {
|
||||||
fetch(`${apiUrl}/data/`).then((result) => {
|
fetch(`${apiUrl}/data/`).then((result) => {
|
||||||
result.json().then(({ data }) => {
|
result.json().then(({ data }) => {
|
||||||
@ -53,7 +68,7 @@ const refreshData = (callback) => {
|
|||||||
} catch {
|
} catch {
|
||||||
refreshData(callback);
|
refreshData(callback);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @async
|
* @async
|
||||||
@ -61,8 +76,9 @@ const refreshData = (callback) => {
|
|||||||
* @param {any} message
|
* @param {any} message
|
||||||
* @param {browser.tabs.Tab} tab
|
* @param {browser.tabs.Tab} tab
|
||||||
* @param {void?} callback
|
* @param {void?} callback
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
const report = async (message, tab, callback) => {
|
async function report(message, tab, callback) {
|
||||||
try {
|
try {
|
||||||
const reason = message.reason;
|
const reason = message.reason;
|
||||||
const url = message.url;
|
const url = message.url;
|
||||||
@ -76,12 +92,7 @@ const report = async (message, tab, callback) => {
|
|||||||
} catch {
|
} catch {
|
||||||
console.error("Can't send report");
|
console.error("Can't send report");
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @description Supress `browser.runtime.lastError`
|
|
||||||
*/
|
|
||||||
const suppressLastError = () => void browser.runtime.lastError;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Listen to context menus clicked
|
* @description Listen to context menus clicked
|
||||||
@ -159,19 +170,9 @@ browser.runtime.onMessage.addListener((message, sender, callback) => {
|
|||||||
script.insertCSS({ files: ['styles/dialog.css'], target: { tabId } });
|
script.insertCSS({ files: ['styles/dialog.css'], target: { tabId } });
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'INSERT_EXTENSION_CSS':
|
|
||||||
if (isPage && tabId !== undefined) {
|
|
||||||
script.insertCSS({ files: ['styles/extension.css'], target: { tabId } });
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'REFRESH_DATA':
|
case 'REFRESH_DATA':
|
||||||
refreshData(callback);
|
refreshData(callback);
|
||||||
return true;
|
return true;
|
||||||
case 'RELOAD_TAB':
|
|
||||||
if (tabId !== undefined) {
|
|
||||||
browser.tabs.reload(tabId, { bypassCache: true });
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'REPORT':
|
case 'REPORT':
|
||||||
if (tabId !== undefined) {
|
if (tabId !== undefined) {
|
||||||
report(message, sender.tab, callback);
|
report(message, sender.tab, callback);
|
||||||
@ -199,10 +200,12 @@ browser.runtime.onMessage.addListener((message, sender, callback) => {
|
|||||||
* @description Listens to extension installed
|
* @description Listens to extension installed
|
||||||
*/
|
*/
|
||||||
browser.runtime.onInstalled.addListener(() => {
|
browser.runtime.onInstalled.addListener(() => {
|
||||||
|
const documentUrlPatterns = browser.runtime.getManifest().content_scripts[0].matches;
|
||||||
|
|
||||||
browser.contextMenus.create(
|
browser.contextMenus.create(
|
||||||
{
|
{
|
||||||
contexts: ['all'],
|
contexts: ['all'],
|
||||||
documentUrlPatterns: browser.runtime.getManifest().content_scripts[0].matches,
|
documentUrlPatterns,
|
||||||
id: extensionMenuItemId,
|
id: extensionMenuItemId,
|
||||||
title: 'Cookie Dialog Monster',
|
title: 'Cookie Dialog Monster',
|
||||||
},
|
},
|
||||||
@ -211,7 +214,7 @@ browser.runtime.onInstalled.addListener(() => {
|
|||||||
browser.contextMenus.create(
|
browser.contextMenus.create(
|
||||||
{
|
{
|
||||||
contexts: ['all'],
|
contexts: ['all'],
|
||||||
documentUrlPatterns: browser.runtime.getManifest().content_scripts[0].matches,
|
documentUrlPatterns,
|
||||||
id: settingsMenuItemId,
|
id: settingsMenuItemId,
|
||||||
parentId: extensionMenuItemId,
|
parentId: extensionMenuItemId,
|
||||||
title: browser.i18n.getMessage('contextMenu_settingsOption'),
|
title: browser.i18n.getMessage('contextMenu_settingsOption'),
|
||||||
@ -221,7 +224,7 @@ browser.runtime.onInstalled.addListener(() => {
|
|||||||
browser.contextMenus.create(
|
browser.contextMenus.create(
|
||||||
{
|
{
|
||||||
contexts: ['all'],
|
contexts: ['all'],
|
||||||
documentUrlPatterns: browser.runtime.getManifest().content_scripts[0].matches,
|
documentUrlPatterns,
|
||||||
id: reportMenuItemId,
|
id: reportMenuItemId,
|
||||||
parentId: extensionMenuItemId,
|
parentId: extensionMenuItemId,
|
||||||
title: browser.i18n.getMessage('contextMenu_reportOption'),
|
title: browser.i18n.getMessage('contextMenu_reportOption'),
|
||||||
@ -239,6 +242,7 @@ browser.runtime.onStartup.addListener(() => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Listen to the moment before a request is made to apply the rules
|
* @description Listen to the moment before a request is made to apply the rules
|
||||||
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
browser.webRequest.onBeforeRequest.addListener(
|
browser.webRequest.onBeforeRequest.addListener(
|
||||||
async (details) => {
|
async (details) => {
|
||||||
@ -247,9 +251,7 @@ browser.webRequest.onBeforeRequest.addListener(
|
|||||||
if (tabId > -1 && type === 'main_frame') {
|
if (tabId > -1 && type === 'main_frame') {
|
||||||
const manifest = browser.runtime.getManifest();
|
const manifest = browser.runtime.getManifest();
|
||||||
const excludeMatches = manifest.content_scripts[0].exclude_matches;
|
const excludeMatches = manifest.content_scripts[0].exclude_matches;
|
||||||
const excludePatterns = excludeMatches.map(
|
const excludePatterns = excludeMatches.map(matchToPattern);
|
||||||
(match) => `^${match.replaceAll('*.', '*(.)?').replaceAll('*', '.*')}$`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (excludePatterns.some((pattern) => new RegExp(pattern).test(url))) {
|
if (excludePatterns.some((pattern) => new RegExp(pattern).test(url))) {
|
||||||
return;
|
return;
|
||||||
@ -260,13 +262,13 @@ browser.webRequest.onBeforeRequest.addListener(
|
|||||||
const state = store[hostname] ?? { enabled: true };
|
const state = store[hostname] ?? { enabled: true };
|
||||||
|
|
||||||
if (data?.rules?.length) {
|
if (data?.rules?.length) {
|
||||||
browser.declarativeNetRequest.updateSessionRules({
|
const rules = data.rules.map((rule) => ({
|
||||||
addRules: state.enabled
|
...rule,
|
||||||
? data.rules.map((rule) => ({
|
condition: { ...rule.condition, tabIds: [tabId] },
|
||||||
...rule,
|
}));
|
||||||
condition: { ...rule.condition, tabIds: [tabId] },
|
|
||||||
}))
|
await browser.declarativeNetRequest.updateSessionRules({
|
||||||
: undefined,
|
addRules: state.enabled ? rules : undefined,
|
||||||
removeRuleIds: data.rules.map((rule) => rule.id),
|
removeRuleIds: data.rules.map((rule) => rule.id),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -274,3 +276,27 @@ browser.webRequest.onBeforeRequest.addListener(
|
|||||||
},
|
},
|
||||||
{ urls: ['<all_urls>'] }
|
{ urls: ['<all_urls>'] }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Listen for errors on network requests
|
||||||
|
*/
|
||||||
|
browser.webRequest.onErrorOccurred.addListener(
|
||||||
|
async (details) => {
|
||||||
|
const { error, tabId, url } = details;
|
||||||
|
|
||||||
|
if (tabId > -1) {
|
||||||
|
const hostname = new URL(url).hostname.split('.').slice(-3).join('.').replace('www.', '');
|
||||||
|
const { data, ...store } = await storage.get(['data', hostname]);
|
||||||
|
const state = store[hostname] ?? { enabled: true };
|
||||||
|
|
||||||
|
if (error === 'net::ERR_BLOCKED_BY_CLIENT' && state.enabled) {
|
||||||
|
const sessionRules = await browser.declarativeNetRequest.getSessionRules();
|
||||||
|
|
||||||
|
if (sessionRules.some((rule) => new RegExp(rule.condition.urlFilter).test(url))) {
|
||||||
|
await browser.tabs.sendMessage(tabId, { type: 'INCREASE_ACTIONS_COUNT' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ urls: ['<all_urls>'] }
|
||||||
|
);
|
||||||
|
@ -92,13 +92,7 @@ const options = { childList: true, subtree: true };
|
|||||||
* @description Elements that were already matched and are removable
|
* @description Elements that were already matched and are removable
|
||||||
* @type {Set<HTMLElement>}
|
* @type {Set<HTMLElement>}
|
||||||
*/
|
*/
|
||||||
const removables = new Set();
|
const seen = new Set();
|
||||||
|
|
||||||
/**
|
|
||||||
* @description Elements that were already seen
|
|
||||||
* @type {HTMLElement[]}
|
|
||||||
*/
|
|
||||||
const seen = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Extension state
|
* @description Extension state
|
||||||
@ -124,14 +118,13 @@ function clean(elements, skipMatch) {
|
|||||||
|
|
||||||
if (match(element, skipMatch)) {
|
if (match(element, skipMatch)) {
|
||||||
if (element instanceof HTMLDialogElement) element.close();
|
if (element instanceof HTMLDialogElement) element.close();
|
||||||
else element.setAttribute(dataAttributeName, 'true');
|
hide(element);
|
||||||
|
|
||||||
actions.add(new Date().getTime().toString());
|
actions.add(new Date().getTime().toString());
|
||||||
dispatch({ type: 'SET_BADGE', value: actions.size });
|
dispatch({ type: 'SET_BADGE', value: actions.size });
|
||||||
removables.add(element);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
seen.push(element);
|
seen.add(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index < elements.length) {
|
if (index < elements.length) {
|
||||||
@ -248,7 +241,7 @@ function match(element, skipMatch) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (seen.includes(element)) {
|
if (seen.has(element)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,6 +377,19 @@ function fix() {
|
|||||||
dispatch({ type: 'SET_BADGE', value: actions.size });
|
dispatch({ type: 'SET_BADGE', value: actions.size });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Hide DOM element
|
||||||
|
* @param {HTMLElement} element
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
function hide(element) {
|
||||||
|
element.style.setProperty('clip-path', 'circle(0px)', 'important');
|
||||||
|
element.style.setProperty('display', 'none', 'important');
|
||||||
|
element.style.setProperty('height', '0px', 'important');
|
||||||
|
element.style.setProperty('overflow', 'hidden', 'important');
|
||||||
|
element.style.setProperty('transform', 'scale(0)', 'important');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Clean DOM when this function is called
|
* @description Clean DOM when this function is called
|
||||||
* @param {RunParams} [params]
|
* @param {RunParams} [params]
|
||||||
@ -423,7 +429,6 @@ async function setUp(params = {}) {
|
|||||||
tokens = data?.tokens ?? tokens;
|
tokens = data?.tokens ?? tokens;
|
||||||
|
|
||||||
dispatch({ type: 'ENABLE_ICON' });
|
dispatch({ type: 'ENABLE_ICON' });
|
||||||
dispatch({ type: 'INSERT_EXTENSION_CSS' });
|
|
||||||
dispatch({ type: 'SET_BADGE', value: actions.size });
|
dispatch({ type: 'SET_BADGE', value: actions.size });
|
||||||
observer.observe(document.body ?? document.documentElement, options);
|
observer.observe(document.body ?? document.documentElement, options);
|
||||||
if (!params.skipRunFn) run({ containers: tokens.containers });
|
if (!params.skipRunFn) run({ containers: tokens.containers });
|
||||||
@ -455,13 +460,8 @@ const observer = new MutationObserver((mutations) => {
|
|||||||
*/
|
*/
|
||||||
browser.runtime.onMessage.addListener(async (message) => {
|
browser.runtime.onMessage.addListener(async (message) => {
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case 'RESTORE': {
|
case 'INCREASE_ACTIONS_COUNT': {
|
||||||
await dispatch({ type: 'RELOAD_TAB' });
|
actions.add(new Date().getTime().toString());
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'RUN': {
|
|
||||||
await setUp({ skipRunFn: !!removables.size });
|
|
||||||
run({ elements: [...removables], skipMatch: true });
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,28 +146,24 @@ async function handleLinkRedirect(event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @async
|
|
||||||
* @description Disable or enable extension on current page
|
* @description Disable or enable extension on current page
|
||||||
* @param {MouseEvent} event
|
* @returns {void}
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
*/
|
||||||
async function handlePowerToggle(event) {
|
function handlePowerToggle() {
|
||||||
const target = event.currentTarget;
|
|
||||||
const next = { enabled: !state.enabled };
|
const next = { enabled: !state.enabled };
|
||||||
|
|
||||||
browser.runtime.sendMessage({ hostname, state: next, type: 'SET_HOSTNAME_STATE' });
|
browser.runtime.sendMessage({ hostname, state: next, type: 'SET_HOSTNAME_STATE' });
|
||||||
browser.tabs.sendMessage(state.tabId, { type: next.enabled ? 'RUN' : 'RESTORE' });
|
browser.tabs.reload(state.tabId, { bypassCache: true });
|
||||||
target.setAttribute('aria-disabled', 'true');
|
|
||||||
target.setAttribute('data-value', next.enabled ? 'on' : 'off');
|
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @async
|
||||||
* @description Open options page
|
* @description Open options page
|
||||||
* @returns {void}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
function handleSettingsClick() {
|
async function handleSettingsClick() {
|
||||||
browser.runtime.openOptionsPage();
|
await browser.runtime.openOptionsPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
*[data-cookie-dialog-monster='true'] {
|
|
||||||
clip-path: circle(0px) !important;
|
|
||||||
display: none !important;
|
|
||||||
height: 0px !important;
|
|
||||||
overflow: hidden !important;
|
|
||||||
transform: scale(0) !important;
|
|
||||||
visibility: hidden !important;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user