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

View File

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

View File

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

View File

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