feat(browser-extension): improve code and cover some more edge cases
This commit is contained in:
parent
2d0ec352e1
commit
5cccc8e9b5
@ -21,6 +21,12 @@
|
|||||||
* @property {boolean} [skipMatch]
|
* @property {boolean} [skipMatch]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} GetElementsParams
|
||||||
|
* @property {boolean} [filterEarly]
|
||||||
|
* @property {HTMLElement} [from]
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} SetUpParams
|
* @typedef {Object} SetUpParams
|
||||||
* @property {boolean} [skipRunFn]
|
* @property {boolean} [skipRunFn]
|
||||||
@ -164,12 +170,11 @@ function containsCommonWord(element) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Force a DOM clean in the specific element
|
* @description Force a DOM clean in the specific element
|
||||||
* @param {HTMLElement} element
|
* @param {HTMLElement} from
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function forceClean(element) {
|
function forceClean(from) {
|
||||||
const nodes = [...element.querySelectorAll(tokens.selectors)];
|
const elements = getElements(tokens.selectors, { filterEarly: true, from });
|
||||||
const elements = nodes.flatMap((node) => filterNodeEarly(node));
|
|
||||||
|
|
||||||
if (elements.length) {
|
if (elements.length) {
|
||||||
fix();
|
fix();
|
||||||
@ -196,6 +201,37 @@ function forceElementStyles(mutations, observer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all elements that match the selector
|
||||||
|
* @param {string | string[]} [selector]
|
||||||
|
* @param {GetElementsParams} [params]
|
||||||
|
* @returns {HTMLElement[]}
|
||||||
|
*/
|
||||||
|
function getElements(selector, params = {}) {
|
||||||
|
const { filterEarly, from } = params;
|
||||||
|
let result = [];
|
||||||
|
|
||||||
|
if (selector?.length) {
|
||||||
|
result = [...(from ?? document).querySelectorAll(selector)];
|
||||||
|
|
||||||
|
if (filterEarly) {
|
||||||
|
result = result.flatMap((node) => filterNodeEarly(node));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all elements with their children that match the selector
|
||||||
|
* @param {string | string[]} selector
|
||||||
|
* @param {GetElementsParams} [params]
|
||||||
|
* @returns {HTMLElement[]}
|
||||||
|
*/
|
||||||
|
function getElementsWithChildren(selector, params) {
|
||||||
|
return getElements(selector, params).flatMap((element) => [element, ...element.children]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Calculate current hostname
|
* @description Calculate current hostname
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
@ -288,6 +324,7 @@ function match(element, skipMatch) {
|
|||||||
* @description Filter early nodes
|
* @description Filter early nodes
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @param {boolean} stopRecursion
|
* @param {boolean} stopRecursion
|
||||||
|
* @returns {HTMLElement[]}
|
||||||
*/
|
*/
|
||||||
function filterNodeEarly(node, stopRecursion) {
|
function filterNodeEarly(node, stopRecursion) {
|
||||||
if (node.nodeType !== Node.ELEMENT_NODE || !(node instanceof HTMLElement)) {
|
if (node.nodeType !== Node.ELEMENT_NODE || !(node instanceof HTMLElement)) {
|
||||||
@ -306,9 +343,7 @@ function filterNodeEarly(node, stopRecursion) {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function fix() {
|
function fix() {
|
||||||
const backdrops = tokens.backdrops?.length
|
const backdrops = getElements(tokens.backdrops);
|
||||||
? [...document.querySelectorAll(tokens.backdrops)]
|
|
||||||
: [];
|
|
||||||
const domains = skips.domains.map((x) => (x.split('.').length < 3 ? `*${x}` : x));
|
const domains = skips.domains.map((x) => (x.split('.').length < 3 ? `*${x}` : x));
|
||||||
|
|
||||||
for (const backdrop of backdrops) {
|
for (const backdrop of backdrops) {
|
||||||
@ -343,7 +378,7 @@ function fix() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'resetAll': {
|
case 'resetAll': {
|
||||||
const elements = document.querySelectorAll(selector);
|
const elements = getElements(selector);
|
||||||
elements.forEach((e) => e?.style?.setProperty(property, 'initial', 'important'));
|
elements.forEach((e) => e?.style?.setProperty(property, 'initial', 'important'));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -365,9 +400,7 @@ function fix() {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function restoreDOM() {
|
function restoreDOM() {
|
||||||
const backdrops = tokens.backdrops?.length
|
const backdrops = getElements(tokens.backdrops);
|
||||||
? [...document.querySelectorAll(tokens.backdrops)]
|
|
||||||
: [];
|
|
||||||
|
|
||||||
for (const backdrop of backdrops) {
|
for (const backdrop of backdrops) {
|
||||||
if (backdrop.children.length === 0 && backdrop.hasAttribute(dataAttributeName)) {
|
if (backdrop.children.length === 0 && backdrop.hasAttribute(dataAttributeName)) {
|
||||||
@ -375,7 +408,7 @@ function restoreDOM() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const elements = [...document.querySelectorAll(`[${dataAttributeName}]`)];
|
const elements = getElements(`[${dataAttributeName}]`);
|
||||||
|
|
||||||
for (const element of elements) {
|
for (const element of elements) {
|
||||||
element.removeAttribute(dataAttributeName);
|
element.removeAttribute(dataAttributeName);
|
||||||
@ -396,19 +429,17 @@ function restoreDOM() {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function run(params = {}) {
|
function run(params = {}) {
|
||||||
|
const { containers, elements, skipMatch } = params;
|
||||||
|
|
||||||
if (document.body?.children.length && !preview && state.enabled && tokens.selectors.length) {
|
if (document.body?.children.length && !preview && state.enabled && tokens.selectors.length) {
|
||||||
fix();
|
fix();
|
||||||
|
|
||||||
if (params.elements?.length) {
|
if (elements?.length) {
|
||||||
clean(params.elements, params.skipMatch);
|
clean(elements, skipMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.elements === undefined && params.containers?.length) {
|
if (elements === undefined && containers?.length) {
|
||||||
clean(
|
clean(containers.flatMap((x) => getElementsWithChildren(x, { filterEarly: true })));
|
||||||
params.containers
|
|
||||||
.flatMap((container) => document.querySelector(container)?.children ?? [])
|
|
||||||
.flatMap((node) => filterNodeEarly(node))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user