Merge pull request #54 from wanhose/v6.0.2

6.0.2
This commit is contained in:
wanhose 2022-08-14 12:01:50 +02:00 committed by GitHub
commit b115a21795
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 50 deletions

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "Cookie Dialog Monster", "name": "Cookie Dialog Monster",
"version": "6.0.1", "version": "6.0.2",
"default_locale": "en", "default_locale": "en",
"description": "__MSG_appDesc__", "description": "__MSG_appDesc__",
"icons": { "icons": {

View File

@ -31,6 +31,13 @@ const options = { childList: true, subtree: true };
const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.'); const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.');
/**
* @description Extension state
* @type {{ enabled: boolean }}
*/
let state = { enabled: true };
/** /**
* @description Matches if node element is removable * @description Matches if node element is removable
* @param {Element} node * @param {Element} node
@ -128,16 +135,12 @@ const observer = new MutationObserver((mutations) => {
* @listens window#pageshow * @listens window#pageshow
*/ */
window.addEventListener('pageshow', async (event) => { window.addEventListener('pageshow', (event) => {
if (event.persisted) { if (data?.elements.length && event.persisted && state.enabled && !preview) {
const state = await dispatch({ hostname, type: 'GET_STATE' }); const nodes = [...document.querySelectorAll(data.elements)];
if (data?.elements?.length && state?.enabled && !preview) { fix();
const nodes = [...document.querySelectorAll(data.elements)]; if (nodes.length) clean(nodes, true);
fix();
if (nodes.length) clean(nodes, true);
}
} }
}); });
@ -147,10 +150,10 @@ window.addEventListener('pageshow', async (event) => {
*/ */
(async () => { (async () => {
const state = await dispatch({ hostname, type: 'GET_STATE' }); state = (await dispatch({ hostname, type: 'GET_STATE' })) ?? state;
dispatch({ type: 'ENABLE_POPUP' }); dispatch({ type: 'ENABLE_POPUP' });
if (state?.enabled) { if (state.enabled) {
data = await dispatch({ hostname, type: 'GET_DATA' }); data = await dispatch({ hostname, type: 'GET_DATA' });
observer.observe(document.body ?? document.documentElement, options); observer.observe(document.body ?? document.documentElement, options);
dispatch({ type: 'ENABLE_ICON' }); dispatch({ type: 'ENABLE_ICON' });

View File

@ -54,16 +54,55 @@ const isEdge = navigator.userAgent.indexOf('Edg') !== -1;
const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1; const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
/**
* @description Extension state
* @type {{ enabled: boolean }}
*/
let state = { enabled: true };
/**
* @async
* @description Setup stars handlers and result message links
*/
const handleContentLoaded = async () => {
const tab = await dispatch({ type: 'GET_TAB' });
hostname = tab?.url
? new URL(tab.url).hostname.split('.').slice(-3).join('.').replace('www.', '')
: undefined;
state = (await dispatch({ hostname, type: 'GET_STATE' })) ?? state;
const host = document.getElementById('host');
host.innerText = hostname ?? 'unknown';
const like = document.getElementById('like');
like.addEventListener('click', handleRate);
const power = document.getElementById('power');
power.addEventListener('change', handlePowerChange);
if (!state.enabled) power.removeAttribute('checked');
const store = document.getElementById('store');
if (isEdge) store?.setAttribute('href', edgeUrl);
else if (isChromium) store?.setAttribute('href', chromeUrl);
else if (isFirefox) store?.setAttribute('href', firefoxUrl);
const unlike = document.getElementById('unlike');
unlike.addEventListener('click', handleRate);
translate();
};
/** /**
* @description Disables or enables extension on current page * @description Disables or enables extension on current page
*/ */
const handlePowerChange = async () => { const handlePowerChange = async () => {
const state = await dispatch({ hostname, type: 'GET_STATE' }); state = { enabled: !state.enabled };
const enabled = typeof state?.enabled === 'undefined' ? false : !state.enabled; dispatch({ hostname, state, type: 'UPDATE_STATE' });
await chrome.tabs.reload({ bypassCache: true });
dispatch({ hostname, state: { enabled }, type: 'UPDATE_STATE' });
chrome.tabs.reload({ bypassCache: true });
}; };
/** /**
@ -89,38 +128,6 @@ const handleRate = (event) => {
} }
}; };
/**
* @async
* @description Setup stars handlers and result message links
*/
const handleContentLoaded = async () => {
const state = await dispatch({ hostname, type: 'GET_STATE' });
const tab = await dispatch({ type: 'GET_TAB' });
hostname = tab?.url
? new URL(tab.url).hostname.split('.').slice(-3).join('.').replace('www.', '')
: undefined;
translate();
const host = document.getElementById('host');
const like = document.getElementById('like');
const power = document.getElementById('power');
const store = document.getElementById('store');
const unlike = document.getElementById('unlike');
like.addEventListener('click', handleRate);
power.addEventListener('change', handlePowerChange);
unlike.addEventListener('click', handleRate);
host.innerText = hostname ?? 'unknown';
if (isEdge) store?.setAttribute('href', edgeUrl);
else if (isChromium) store?.setAttribute('href', chromeUrl);
else if (isFirefox) store?.setAttribute('href', firefoxUrl);
if (!state.enabled) power.removeAttribute('checked');
};
/** /**
* @description Applies translations to tags with i18n data attribute * @description Applies translations to tags with i18n data attribute
*/ */
@ -138,7 +145,7 @@ const translate = () => {
/** /**
* @description Listen to document ready * @description Listen to document ready
* @listens document#ready * @listens document#DOMContentLoaded
*/ */
document.addEventListener('DOMContentLoaded', handleContentLoaded); document.addEventListener('DOMContentLoaded', handleContentLoaded);