Merge pull request #18 from wanhose/5.1.0

5.1.0
This commit is contained in:
wanhose 2021-10-28 13:54:07 +02:00 committed by GitHub
commit 969a47bfae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 76 deletions

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Cookie Dialog Monster", "name": "Cookie Dialog Monster",
"version": "5.0.2", "version": "5.1.0",
"default_locale": "en", "default_locale": "en",
"description": "__MSG_appDesc__", "description": "__MSG_appDesc__",
"icons": { "icons": {
@ -20,6 +20,7 @@
"content_scripts": [ "content_scripts": [
{ {
"all_frames": true, "all_frames": true,
"exclude_matches": ["*://*.gfycat.com/*"],
"js": ["scripts/content.js"], "js": ["scripts/content.js"],
"matches": ["http://*/*", "https://*/*"], "matches": ["http://*/*", "https://*/*"],
"run_at": "document_start" "run_at": "document_start"

View File

@ -1,30 +1,25 @@
/** /**
* @var cacheInitialState
* @description Cache initial state * @description Cache initial state
* @type {{ enabled: boolean, matches: string[] }} * @type {{ enabled: boolean, matches: string[] }}
*/ */
const cacheInitialState = { const initial = {
enabled: true, enabled: true,
matches: [], matches: [],
}; };
/** /**
* @function isValid
* @description Check cache validity * @description Check cache validity
*
* @param {object} [cache] * @param {object} [cache]
*/ */
const isValid = (cache) => const check = (cache) =>
typeof cache.enabled === "boolean" && typeof cache.enabled === "boolean" &&
Array.isArray(cache.matches) && Array.isArray(cache.matches) &&
cache.matches.every((match) => typeof match === "string"); cache.matches.every((match) => typeof match === "string");
/** /**
* @function disableIcon
* @description Disables icon * @description Disables icon
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -36,9 +31,7 @@ const disableIcon = (tabId) => {
}; };
/** /**
* @function disablePopup
* @description Disables popup * @description Disables popup
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -50,9 +43,7 @@ const disablePopup = (tabId) => {
}; };
/** /**
* @function enableIcon
* @description Enables icon * @description Enables icon
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -64,9 +55,7 @@ const enableIcon = (tabId) => {
}; };
/** /**
* @function enablePopup
* @description Enables popup * @description Enables popup
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -78,39 +67,35 @@ const enablePopup = (tabId) => {
}; };
/** /**
* @function getCache
* @description Retrieves cache state * @description Retrieves cache state
*
* @param {string} [hostname] * @param {string} [hostname]
* @param {void} [responseCallback] * @param {void} [callback]
* @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state * @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state
*/ */
const getCache = (hostname, responseCallback) => { const getCache = (hostname, callback) => {
chrome.storage.local.get(null, (store) => { chrome.storage.local.get(null, (store) => {
try { try {
const cache = store[hostname]; const cache = store[hostname];
if (!isValid(cache)) throw new Error(); if (!check(cache)) throw new Error();
responseCallback(cache); callback(cache);
} catch { } catch {
chrome.storage.local.set({ [hostname]: cacheInitialState }); chrome.storage.local.set({ [hostname]: initial });
responseCallback(cacheInitialState); callback(initial);
} }
}); });
}; };
/** /**
* @async * @async
* @function getClasses
* @description Retrieves a selectors list * @description Retrieves a selectors list
* * @param {void} [callback]
* @param {void} [responseCallback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ matches: string[] }>} A selectors list
*/ */
const getClasses = async (responseCallback) => { const getClasses = async (callback) => {
try { try {
const url = const url =
"https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/classes.txt"; "https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/classes.txt";
@ -119,22 +104,20 @@ const getClasses = async (responseCallback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
responseCallback({ classes: data.split("\n") }); callback({ classes: data.split("\n") });
} catch { } catch {
responseCallback({ classes: [] }); callback({ classes: [] });
} }
}; };
/** /**
* @async * @async
* @function getSelectors
* @description Retrieves a selectors list * @description Retrieves a selectors list
* * @param {void} [callback]
* @param {void} [responseCallback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ matches: string[] }>} A selectors list
*/ */
const getSelectors = async (responseCallback) => { const getSelectors = async (callback) => {
try { try {
const url = const url =
"https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/elements.txt"; "https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/elements.txt";
@ -143,23 +126,21 @@ const getSelectors = async (responseCallback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
responseCallback({ selectors: data.split("\n") }); callback({ selectors: data.split("\n") });
} catch { } catch {
responseCallback({ selectors: [] }); callback({ selectors: [] });
} }
}; };
/** /**
* @function getTab
* @description Retrieves current tab information * @description Retrieves current tab information
* * @param {void} [callback]
* @param {void} [responseCallback]
* @returns {Promise<{ id: string, location: string }>} Current tab information * @returns {Promise<{ id: string, location: string }>} Current tab information
*/ */
const getTab = (responseCallback) => { const getTab = (callback) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
responseCallback({ callback({
id: tabs[0].id, id: tabs[0].id,
hostname: new URL(tabs[0].url).hostname, hostname: new URL(tabs[0].url).hostname,
}); });
@ -167,9 +148,7 @@ const getTab = (responseCallback) => {
}; };
/** /**
* @function updateCache
* @description Update cache state * @description Update cache state
*
* @param {string} [hostname] * @param {string} [hostname]
* @param {object} [state] * @param {object} [state]
*/ */
@ -197,7 +176,7 @@ const updateCache = (hostname, state) => {
* @description Listens to messages * @description Listens to messages
*/ */
chrome.runtime.onMessage.addListener((request, sender, responseCallback) => { chrome.runtime.onMessage.addListener((request, sender, callback) => {
const hasPermission = !sender.frameId || sender.frameId === 0; const hasPermission = !sender.frameId || sender.frameId === 0;
let tabId = sender.tab ? sender.tab.id : undefined; let tabId = sender.tab ? sender.tab.id : undefined;
@ -222,16 +201,16 @@ chrome.runtime.onMessage.addListener((request, sender, responseCallback) => {
if (hasPermission) enablePopup(tabId); if (hasPermission) enablePopup(tabId);
break; break;
case "GET_CACHE": case "GET_CACHE":
getCache(request.hostname, responseCallback); getCache(request.hostname, callback);
break; break;
case "GET_CLASSES": case "GET_CLASSES":
getClasses(responseCallback); getClasses(callback);
break; break;
case "GET_SELECTORS": case "GET_SELECTORS":
getSelectors(responseCallback); getSelectors(callback);
break; break;
case "GET_TAB": case "GET_TAB":
getTab(responseCallback); getTab(callback);
break; break;
case "UPDATE_CACHE": case "UPDATE_CACHE":
updateCache(request.hostname, request.state); updateCache(request.hostname, request.state);

View File

@ -64,7 +64,6 @@ const fix = () => {
}; };
/** /**
* @function removeElements
* @description Removes matched elements from a selectors array * @description Removes matched elements from a selectors array
* @param {string[]} selectors * @param {string[]} selectors
* @param {boolean} updateCache * @param {boolean} updateCache
@ -79,7 +78,7 @@ const removeElements = (selectors, updateCache) => {
const tagName = element.tagName.toUpperCase(); const tagName = element.tagName.toUpperCase();
if (!["BODY", "HTML"].includes(tagName)) { if (!["BODY", "HTML"].includes(tagName)) {
element.remove(); element.outerHTML = "";
if (updateCache) { if (updateCache) {
selectorsFromCache = [...selectorsFromCache, selector]; selectorsFromCache = [...selectorsFromCache, selector];
@ -95,11 +94,11 @@ const removeElements = (selectors, updateCache) => {
}; };
/** /**
* @function runTasks * @async
* @description Starts running tasks * @description Runs tasks
*/ */
const runTasks = async () => { const run = async () => {
if (attempts <= 20) { if (attempts <= 20) {
fix(); fix();
removeElements(selectorsFromCache); removeElements(selectorsFromCache);
@ -110,13 +109,13 @@ const runTasks = async () => {
if (attempts <= 5) await Promise.all(selectors.map((fn) => fn())); if (attempts <= 5) await Promise.all(selectors.map((fn) => fn()));
if (document.readyState === "complete") attempts += 1; if (document.readyState === "complete") attempts += 1;
} }
requestAnimationFrame(run);
} }
}; };
/** /**
* @function search
* @description Retrieves HTML element if selector exists * @description Retrieves HTML element if selector exists
*
* @param {string} selector * @param {string} selector
* @returns {HTMLElement | null} An HTML element or null * @returns {HTMLElement | null} An HTML element or null
*/ */
@ -139,27 +138,29 @@ const search = (selector) => {
/** /**
* @description Setups classes selectors * @description Setups classes selectors
* @type {Promise<boolean>} * @returns {Promise<boolean>}
*/ */
const setupClasses = new Promise((resolve) => { const setupClasses = () =>
new Promise((resolve) => {
dispatch({ type: "GET_CLASSES" }, null, ({ classes }) => { dispatch({ type: "GET_CLASSES" }, null, ({ classes }) => {
classesFromNetwork = classes; classesFromNetwork = classes;
resolve(true); resolve(true);
}); });
}); });
/** /**
* @description Setups elements selectors * @description Setups elements selectors
* @type {Promise<boolean>} * @returns {Promise<boolean>}
*/ */
const setupSelectors = new Promise((resolve) => { const setupSelectors = () =>
new Promise((resolve) => {
dispatch({ type: "GET_SELECTORS" }, null, ({ selectors }) => { dispatch({ type: "GET_SELECTORS" }, null, ({ selectors }) => {
selectorsFromNetwork = chunkerize(selectors); selectorsFromNetwork = chunkerize(selectors);
resolve(true); resolve(true);
}); });
}); });
dispatch( dispatch(
{ hostname: document.location.hostname, type: "GET_CACHE" }, { hostname: document.location.hostname, type: "GET_CACHE" },
@ -170,9 +171,8 @@ dispatch(
if (enabled) { if (enabled) {
selectorsFromCache = matches; selectorsFromCache = matches;
dispatch({ type: "ENABLE_ICON" }); dispatch({ type: "ENABLE_ICON" });
await Promise.all([setupClasses, setupSelectors]); await Promise.all([setupClasses(), setupSelectors()]);
await runTasks(); await run();
setInterval(runTasks, 500);
} }
} }
); );

View File

@ -5,7 +5,7 @@
*/ */
const chromeUrl = const chromeUrl =
"https://chrome.google.com/webstore/detail/cookie-dialog-monster/djcbfpkdhdkaflcigibkbpboflaplabg"; "https://chrome.google.com/webstore/detail/djcbfpkdhdkaflcigibkbpboflaplabg";
/** /**
* @constant dispatch * @constant dispatch
@ -33,7 +33,6 @@ const firefoxUrl =
const isChromium = chrome.runtime.getURL("").startsWith("chrome-extension://"); const isChromium = chrome.runtime.getURL("").startsWith("chrome-extension://");
/** /**
* @function handlePowerChange
* @description Disables or enables extension on current page * @description Disables or enables extension on current page
*/ */
@ -58,7 +57,6 @@ const handlePowerChange = () => {
}; };
/** /**
* @function handleReload
* @description Reload current page * @description Reload current page
*/ */
@ -69,9 +67,7 @@ const handleReload = () => {
}; };
/** /**
* @function handleRate
* @description Shows negative or positive messages * @description Shows negative or positive messages
*
* @param {MouseEvent} event * @param {MouseEvent} event
*/ */
@ -94,7 +90,6 @@ const handleRate = (event) => {
}; };
/** /**
* @function handleContentLoaded
* @description Setup stars handlers and result message links * @description Setup stars handlers and result message links
*/ */
@ -121,8 +116,6 @@ const handleContentLoaded = () => {
/** /**
* @description Listen to document ready * @description Listen to document ready
*
* @type {Document}
* @listens document#ready * @listens document#ready
*/ */