2021-06-27 11:17:53 +00:00
|
|
|
/**
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Data properties
|
2023-06-10 14:56:24 +00:00
|
|
|
* @type {{ classes: string[], commonWords?: 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 };
|
|
|
|
|
2022-08-14 10:57:38 +00:00
|
|
|
/**
|
|
|
|
* @description Cleans DOM
|
|
|
|
* @param {Element[]} nodes
|
|
|
|
* @param {boolean?} skipMatch
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const clean = (nodes, skipMatch) => {
|
2023-04-17 17:21:31 +00:00
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
const node = nodes[i];
|
|
|
|
|
|
|
|
if (match(node, skipMatch)) {
|
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
node.style.setProperty('display', 'none', 'important');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!node.hasAttribute('data-cookie-dialog-monster')) {
|
|
|
|
node.setAttribute('data-cookie-dialog-monster', 'true');
|
|
|
|
node.style.setProperty('display', 'none', 'important');
|
|
|
|
observer.observe(node, { attributes: true, attributeFilter: ['style'] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 10:57:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-06-10 14:56:24 +00:00
|
|
|
* @description Forces a DOM clean in the specific node
|
|
|
|
* @param {HTMLElement} node
|
2022-08-14 10:57:38 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
|
2023-06-10 14:56:24 +00:00
|
|
|
const forceClean = (node) => {
|
2022-08-14 10:57:38 +00:00
|
|
|
if (data?.elements.length && state.enabled && !preview) {
|
2023-06-10 14:56:24 +00:00
|
|
|
const nodes = [...node.querySelectorAll(data.elements)];
|
2022-08-14 10:57:38 +00:00
|
|
|
|
|
|
|
if (nodes.length) {
|
|
|
|
fix();
|
|
|
|
clean(nodes, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-08 21:50:24 +00:00
|
|
|
/**
|
2022-11-29 19:41:58 +00:00
|
|
|
* @description Checks if an element is visible in the viewport
|
|
|
|
* @param {HTMLElement} node
|
2022-05-06 11:18:57 +00:00
|
|
|
* @returns {boolean}
|
2021-11-08 16:13:10 +00:00
|
|
|
*/
|
|
|
|
|
2022-11-29 19:41:58 +00:00
|
|
|
const isInViewport = (node) => {
|
2023-05-20 15:16:01 +00:00
|
|
|
const height = window.innerHeight || document.documentElement.clientHeight;
|
|
|
|
const position = node.getBoundingClientRect();
|
|
|
|
const scroll = window.scrollY || window.pageYOffset;
|
|
|
|
|
2023-05-20 18:17:07 +00:00
|
|
|
return (
|
|
|
|
position.bottom === position.top ||
|
|
|
|
(scroll + position.top <= scroll + height && scroll + position.bottom >= scroll)
|
|
|
|
);
|
2022-08-02 19:03:43 +00:00
|
|
|
};
|
2021-11-08 16:13:10 +00:00
|
|
|
|
2022-11-29 19:41:58 +00:00
|
|
|
/**
|
2023-05-20 15:16:01 +00:00
|
|
|
* @description Checks if node element is removable
|
2022-11-29 19:41:58 +00:00
|
|
|
* @param {Element} node
|
|
|
|
* @param {boolean?} skipMatch
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
|
2023-06-10 14:56:24 +00:00
|
|
|
const match = (node, skipMatch) => {
|
|
|
|
if (node instanceof HTMLElement) {
|
|
|
|
if (node.hasAttributes()) {
|
|
|
|
return (
|
|
|
|
// 2023-06-10: twitch.tv temporary fix
|
|
|
|
node.className !== 'chat-line__message' &&
|
|
|
|
// ...
|
|
|
|
!node.getAttribute('data-cookie-dialog-monster') &&
|
|
|
|
!data?.tags.includes(node.tagName?.toUpperCase?.()) &&
|
|
|
|
isInViewport(node) &&
|
|
|
|
(skipMatch || node.matches(data?.elements ?? []))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// 2023-06-10: fix edge case force cleaning on children if no attributes
|
|
|
|
if (data?.commonWords && node.outerHTML.match(new RegExp(commonWords?.join('|')))) {
|
|
|
|
forceClean(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
2022-11-29 19:41:58 +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-12-01 16:01:47 +00:00
|
|
|
const backdrop = document.getElementsByClassName('modal-backdrop')[0];
|
2022-12-01 17:07:38 +00:00
|
|
|
const facebook = document.getElementsByClassName('_31e')[0];
|
|
|
|
const fixes = data?.fixes ?? [];
|
|
|
|
const skips = data?.skips ?? [];
|
2022-12-01 16:01:47 +00:00
|
|
|
|
2022-12-01 17:07:38 +00:00
|
|
|
if (backdrop?.children.length === 0) {
|
2022-12-01 16:01:47 +00:00
|
|
|
backdrop.remove();
|
|
|
|
}
|
|
|
|
|
2022-12-01 17:07:38 +00:00
|
|
|
facebook?.classList.remove('_31e');
|
2022-07-20 08:27:05 +00:00
|
|
|
|
2022-12-01 17:07:38 +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
|
|
|
}
|
2022-12-01 17:07:38 +00:00
|
|
|
|
2023-05-20 15:16:01 +00:00
|
|
|
if (skips.indexOf(hostname) === -1) {
|
2022-12-01 17:07:38 +00:00
|
|
|
for (const item of [document.body, document.documentElement]) {
|
|
|
|
item?.classList.remove(...(data?.classes ?? []));
|
|
|
|
item?.style.setProperty('position', 'initial', 'important');
|
|
|
|
item?.style.setProperty('overflow-y', 'initial', 'important');
|
|
|
|
}
|
|
|
|
}
|
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-14 10:57:38 +00:00
|
|
|
* @description Fixes already existing element when page load issues
|
2023-05-20 15:16:01 +00:00
|
|
|
* @listens window#DOMContentLoaded
|
2022-08-14 10:57:38 +00:00
|
|
|
*/
|
|
|
|
|
2023-05-20 15:16:01 +00:00
|
|
|
window.addEventListener('DOMContentLoaded', () => {
|
2023-06-10 14:56:24 +00:00
|
|
|
forceClean(document.documentElement);
|
2022-08-14 10:57:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
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) => {
|
2023-05-20 15:16:01 +00:00
|
|
|
if (event.persisted) {
|
2023-06-10 14:56:24 +00:00
|
|
|
forceClean(document.documentElement);
|
2023-05-20 15:16:01 +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' });
|
|
|
|
dispatch({ type: 'ENABLE_ICON' });
|
2023-06-10 14:56:24 +00:00
|
|
|
observer.observe(document.documentElement, options);
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
2022-08-02 10:36:48 +00:00
|
|
|
})();
|