2021-06-27 11:17:53 +00:00
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Array of selectors
|
|
|
|
* @type {string[]}
|
2021-06-27 11:17:53 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
const classes = [];
|
2021-06-27 11:17:53 +00:00
|
|
|
|
2021-04-08 21:50:24 +00:00
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Shortcut to send messages to background script
|
|
|
|
* @type {void}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
const dispatch = chrome.runtime.sendMessage;
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
/**
|
|
|
|
* @description Array of skips to skip
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const skips = [];
|
|
|
|
|
2021-11-10 10:24:59 +00:00
|
|
|
/**
|
|
|
|
* @description Array of instructions
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
const fixes = [];
|
2021-11-10 10:24:59 +00:00
|
|
|
|
2021-11-07 13:01:36 +00:00
|
|
|
/**
|
|
|
|
* @description Hostname
|
|
|
|
*/
|
|
|
|
|
2022-05-12 17:58:52 +00:00
|
|
|
const hostname = document.location.hostname.split('.').slice(-2).join('.');
|
2021-11-07 13:01:36 +00:00
|
|
|
|
2021-11-06 19:09:38 +00:00
|
|
|
/**
|
|
|
|
* @description Is consent preview page?
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.');
|
2021-11-06 19:09:38 +00:00
|
|
|
|
2021-04-08 21:50:24 +00:00
|
|
|
/**
|
2021-10-30 12:26:22 +00:00
|
|
|
* @description Options provided to observer
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-25 12:16:31 +00:00
|
|
|
const options = { childList: true, subtree: true };
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-30 12:26:22 +00:00
|
|
|
* @description Selectors list
|
2021-11-10 10:24:59 +00:00
|
|
|
* @type {string[]}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
const selectors = [];
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-30 12:26:22 +00:00
|
|
|
* @description Target provided to observer
|
2021-10-05 07:00:07 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-30 12:26:22 +00:00
|
|
|
const target = document.body || document.documentElement;
|
2021-10-05 07:00:07 +00:00
|
|
|
|
2021-11-08 16:13:10 +00:00
|
|
|
/**
|
|
|
|
* @description Checks if node element is removable
|
2022-05-06 11:18:57 +00:00
|
|
|
* @param {any} node
|
|
|
|
* @returns {boolean}
|
2021-11-08 16:13:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const check = (node) =>
|
|
|
|
node instanceof HTMLElement &&
|
|
|
|
node.parentElement &&
|
2022-05-05 15:19:35 +00:00
|
|
|
!['BODY', 'HTML'].includes(node.tagName) &&
|
2022-05-06 11:18:57 +00:00
|
|
|
!(node.id && ['APP', 'ROOT'].includes(node.id.toUpperCase?.())) &&
|
|
|
|
node.matches(selectors);
|
2021-11-08 16:13:10 +00:00
|
|
|
|
2021-11-06 13:43:17 +00:00
|
|
|
/**
|
|
|
|
* @description Cleans DOM
|
2022-05-06 11:18:57 +00:00
|
|
|
* @param {HTMLElement[]} nodes
|
|
|
|
* @returns {void}
|
2021-11-06 13:43:17 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
const clean = (nodes) => nodes.filter(check).forEach((node) => (node.outerHTML = ''));
|
2021-11-06 13:43:17 +00:00
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
/**
|
|
|
|
* @description Fixes scroll issues
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const fix = () => {
|
2022-05-06 11:18:57 +00:00
|
|
|
if (skips.length && !skips.includes(hostname)) {
|
|
|
|
for (const item of [document.body, document.documentElement]) {
|
|
|
|
item?.classList.remove(...classes);
|
|
|
|
item?.style.setProperty('position', 'initial', 'important');
|
|
|
|
item?.style.setProperty('overflow-y', 'initial', 'important');
|
|
|
|
}
|
|
|
|
}
|
2021-11-06 19:09:38 +00:00
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
for (const fix of fixes) {
|
2022-05-05 15:19:35 +00:00
|
|
|
const [match, selector, action, property] = fix.split('##');
|
2021-11-10 10:24:59 +00:00
|
|
|
|
|
|
|
if (hostname.includes(match)) {
|
|
|
|
switch (action) {
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'click': {
|
2021-11-10 10:24:59 +00:00
|
|
|
const node = document.querySelector(selector);
|
2022-01-20 14:30:09 +00:00
|
|
|
node?.click();
|
2022-05-06 11:18:57 +00:00
|
|
|
break;
|
2022-01-20 14:30:09 +00:00
|
|
|
}
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'remove': {
|
2022-01-20 14:30:09 +00:00
|
|
|
const node = document.querySelector(selector);
|
|
|
|
node?.style?.removeProperty(property);
|
2022-05-06 11:18:57 +00:00
|
|
|
break;
|
2022-01-20 14:30:09 +00:00
|
|
|
}
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'reset': {
|
2022-01-20 14:30:09 +00:00
|
|
|
const node = document.querySelector(selector);
|
2022-05-05 15:19:35 +00:00
|
|
|
node?.style?.setProperty(property, 'initial', 'important');
|
2022-05-06 11:18:57 +00:00
|
|
|
break;
|
2022-01-20 14:30:09 +00:00
|
|
|
}
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'resetAll': {
|
2021-11-10 10:24:59 +00:00
|
|
|
const nodes = document.querySelectorAll(selector);
|
2022-05-06 11:18:57 +00:00
|
|
|
nodes.forEach((node) => node?.style?.setProperty(property, 'initial', 'important'));
|
|
|
|
break;
|
2022-01-20 14:30:09 +00:00
|
|
|
}
|
2021-11-10 10:24:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-11-08 16:13:10 +00:00
|
|
|
}
|
2022-05-06 11:18:57 +00:00
|
|
|
}
|
2021-04-08 21:50:24 +00:00
|
|
|
};
|
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
/**
|
|
|
|
* @description Mutation Observer instance
|
|
|
|
* @type {MutationObserver}
|
|
|
|
*/
|
|
|
|
|
2021-10-30 12:26:22 +00:00
|
|
|
const observer = new MutationObserver((mutations, instance) => {
|
2022-05-06 11:18:57 +00:00
|
|
|
const nodes = mutations.map((mutation) => Array.from(mutation.addedNodes)).flat();
|
|
|
|
|
2021-10-30 12:26:22 +00:00
|
|
|
instance.disconnect();
|
|
|
|
fix();
|
2022-05-06 11:18:57 +00:00
|
|
|
if (!preview) clean(nodes);
|
2021-10-30 12:26:22 +00:00
|
|
|
instance.observe(target, options);
|
|
|
|
});
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
/**
|
2022-05-06 11:18:57 +00:00
|
|
|
* @description Gets data
|
|
|
|
* @returns {Promise<any[]>}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
const promiseAll = () =>
|
|
|
|
Promise.all([
|
|
|
|
new Promise((resolve) => dispatch({ type: 'GET_CLASSES' }, null, resolve)),
|
|
|
|
new Promise((resolve) => dispatch({ type: 'GET_FIXES' }, null, resolve)),
|
|
|
|
new Promise((resolve) => dispatch({ type: 'GET_SELECTORS' }, null, resolve)),
|
|
|
|
new Promise((resolve) => dispatch({ type: 'GET_SKIPS' }, null, resolve)),
|
|
|
|
]);
|
2021-10-05 07:00:07 +00:00
|
|
|
|
2021-11-06 13:43:17 +00:00
|
|
|
/**
|
2021-12-07 16:15:28 +00:00
|
|
|
* @description Cleans DOM again after all
|
2021-11-08 16:13:10 +00:00
|
|
|
* @listens document#readystatechange
|
2021-11-06 13:43:17 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-05 15:19:35 +00:00
|
|
|
document.addEventListener('readystatechange', () => {
|
|
|
|
dispatch({ hostname, type: 'GET_CACHE' }, null, async ({ enabled }) => {
|
2022-05-12 17:58:52 +00:00
|
|
|
if (document.readyState === 'complete' && enabled && !preview && selectors.length) {
|
2022-05-06 11:18:57 +00:00
|
|
|
const nodes = Array.from(document.querySelectorAll(selectors));
|
|
|
|
|
2021-11-09 19:34:21 +00:00
|
|
|
fix();
|
2022-05-06 11:18:57 +00:00
|
|
|
clean(nodes);
|
|
|
|
setTimeout(() => clean(nodes), 2000);
|
2021-11-06 19:09:38 +00:00
|
|
|
}
|
2021-11-09 19:34:21 +00:00
|
|
|
});
|
2021-11-06 13:43:17 +00:00
|
|
|
});
|
|
|
|
|
2021-12-07 16:15:28 +00:00
|
|
|
/**
|
|
|
|
* @description Fix bfcache issues
|
|
|
|
* @listens window#unload
|
|
|
|
*/
|
|
|
|
|
2022-05-05 15:19:35 +00:00
|
|
|
window.addEventListener('unload', () => {});
|
2021-12-07 16:15:28 +00:00
|
|
|
|
2021-10-30 12:26:22 +00:00
|
|
|
/**
|
|
|
|
* @description Setups everything and starts to observe if enabled
|
|
|
|
*/
|
|
|
|
|
2022-05-05 15:19:35 +00:00
|
|
|
dispatch({ hostname, type: 'GET_CACHE' }, null, async ({ enabled }) => {
|
|
|
|
dispatch({ type: 'ENABLE_POPUP' });
|
2021-11-07 13:01:36 +00:00
|
|
|
|
|
|
|
if (enabled) {
|
2022-05-06 11:18:57 +00:00
|
|
|
const results = await promiseAll();
|
2022-01-09 11:08:45 +00:00
|
|
|
|
2022-05-06 11:18:57 +00:00
|
|
|
classes.push(...(results[0]?.classes ?? []));
|
|
|
|
fixes.push(...(results[1]?.fixes ?? []));
|
|
|
|
selectors.push(...(results[2]?.elements ?? []));
|
|
|
|
skips.push(...(results[3]?.skips ?? []));
|
2021-11-07 13:01:36 +00:00
|
|
|
observer.observe(target, options);
|
2022-05-05 15:19:35 +00:00
|
|
|
dispatch({ type: 'ENABLE_ICON' });
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
2021-11-07 13:01:36 +00:00
|
|
|
});
|