2021-06-27 11:17:53 +00:00
|
|
|
/**
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Data properties
|
2022-08-10 11:29:59 +00:00
|
|
|
* @type {{ classes: string[], fixes: string[], elements: string[], skips: string[], tags: string[] }?}
|
2021-06-27 11:17:53 +00:00
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
let data = null;
|
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
|
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
|
|
|
|
2021-11-10 10:24:59 +00:00
|
|
|
/**
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Current hostname
|
|
|
|
* @type {string}
|
2021-11-10 10:24:59 +00:00
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
const hostname = document.location.hostname.split('.').slice(-3).join('.').replace('www.', '');
|
2021-11-07 13:01:36 +00:00
|
|
|
|
2021-11-06 19:09:38 +00:00
|
|
|
/**
|
2022-05-19 12:17:31 +00:00
|
|
|
* @description Options provided to observer
|
|
|
|
* @type {MutationObserverInit}
|
2021-11-06 19:09:38 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-19 12:17:31 +00:00
|
|
|
const options = { childList: true, subtree: true };
|
2021-11-06 19:09:38 +00:00
|
|
|
|
2021-04-08 21:50:24 +00:00
|
|
|
/**
|
2022-05-19 12:17:31 +00:00
|
|
|
* @description Is consent preview page?
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-19 12:17:31 +00:00
|
|
|
const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.');
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2022-08-14 09:58:31 +00:00
|
|
|
/**
|
|
|
|
* @description Extension state
|
|
|
|
* @type {{ enabled: boolean }}
|
|
|
|
*/
|
|
|
|
|
|
|
|
let state = { enabled: true };
|
|
|
|
|
2021-04-08 21:50:24 +00:00
|
|
|
/**
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Matches if node element is removable
|
|
|
|
* @param {Element} node
|
2022-05-06 11:18:57 +00:00
|
|
|
* @returns {boolean}
|
2021-11-08 16:13:10 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-02 19:03:43 +00:00
|
|
|
const match = (node) => {
|
|
|
|
if (!(node instanceof HTMLElement)) return false;
|
|
|
|
|
|
|
|
const rect = node.getBoundingClientRect();
|
|
|
|
const isFullscreen = rect.bottom + rect.top > 0 && rect.bottom - rect.top === 0;
|
|
|
|
const isVisible = rect.top <= (window.innerHeight || document.documentElement.clientHeight);
|
|
|
|
|
|
|
|
return (
|
2022-08-10 11:29:59 +00:00
|
|
|
!data?.tags.includes(node.tagName?.toUpperCase?.()) &&
|
2022-08-02 19:03:43 +00:00
|
|
|
(isFullscreen || isVisible) &&
|
|
|
|
(node.offsetParent || window.getComputedStyle(node).position === 'fixed') &&
|
|
|
|
node.parentElement &&
|
|
|
|
node.matches(data?.elements ?? [])
|
|
|
|
);
|
|
|
|
};
|
2021-11-08 16:13:10 +00:00
|
|
|
|
2021-11-06 13:43:17 +00:00
|
|
|
/**
|
|
|
|
* @description Cleans DOM
|
2022-08-02 19:03:43 +00:00
|
|
|
* @param {Element[]} nodes
|
2022-07-20 08:27:05 +00:00
|
|
|
* @param {boolean?} skipMatch
|
2022-05-06 11:18:57 +00:00
|
|
|
* @returns {void}
|
2021-11-06 13:43:17 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-19 13:52:10 +00:00
|
|
|
const clean = (nodes, skipMatch) =>
|
2022-07-20 08:27:05 +00:00
|
|
|
nodes.filter((node) => skipMatch || match(node)).forEach((node) => node.remove());
|
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-07-20 08:27:05 +00:00
|
|
|
document.getElementsByClassName('_31e')[0]?.classList.remove('_31e');
|
|
|
|
|
|
|
|
if (data?.skips.length && !data.skips.includes(hostname)) {
|
2022-05-06 11:18:57 +00:00
|
|
|
for (const item of [document.body, document.documentElement]) {
|
2022-07-20 08:27:05 +00:00
|
|
|
item?.classList.remove(...(data?.classes ?? []));
|
2022-05-06 11:18:57 +00:00
|
|
|
item?.style.setProperty('position', 'initial', 'important');
|
|
|
|
item?.style.setProperty('overflow-y', 'initial', 'important');
|
|
|
|
}
|
|
|
|
}
|
2021-11-06 19:09:38 +00:00
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
for (const fix of data?.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}
|
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
const observer = new MutationObserver((mutations) => {
|
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
|
|
|
fix();
|
2022-07-20 08:27:05 +00:00
|
|
|
if (data?.elements.length && !preview) clean(nodes);
|
2021-10-30 12:26:22 +00:00
|
|
|
});
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2021-11-06 13:43:17 +00:00
|
|
|
/**
|
2022-08-02 10:36:48 +00:00
|
|
|
* @async
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Fixes bfcache issues
|
|
|
|
* @listens window#pageshow
|
2021-11-06 13:43:17 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-14 09:58:31 +00:00
|
|
|
window.addEventListener('pageshow', (event) => {
|
|
|
|
if (data?.elements.length && event.persisted && state.enabled && !preview) {
|
|
|
|
const nodes = [...document.querySelectorAll(data.elements)];
|
2022-08-02 10:36:48 +00:00
|
|
|
|
2022-08-14 09:58:31 +00:00
|
|
|
fix();
|
|
|
|
if (nodes.length) clean(nodes, true);
|
2022-07-20 08:27:05 +00:00
|
|
|
}
|
2021-11-06 13:43:17 +00:00
|
|
|
});
|
|
|
|
|
2021-12-07 16:15:28 +00:00
|
|
|
/**
|
2022-08-02 10:36:48 +00:00
|
|
|
* @async
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Sets up everything
|
2021-10-30 12:26:22 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-02 10:36:48 +00:00
|
|
|
(async () => {
|
2022-08-14 09:58:31 +00:00
|
|
|
state = (await dispatch({ hostname, type: 'GET_STATE' })) ?? state;
|
2022-05-05 15:19:35 +00:00
|
|
|
dispatch({ type: 'ENABLE_POPUP' });
|
2021-11-07 13:01:36 +00:00
|
|
|
|
2022-08-14 09:58:31 +00:00
|
|
|
if (state.enabled) {
|
2022-08-02 10:36:48 +00:00
|
|
|
data = await dispatch({ hostname, type: 'GET_DATA' });
|
|
|
|
observer.observe(document.body ?? document.documentElement, options);
|
|
|
|
dispatch({ type: 'ENABLE_ICON' });
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
2022-08-02 10:36:48 +00:00
|
|
|
})();
|