fix(browser-extension): some performance issues and regressions
This commit is contained in:
parent
d388394a89
commit
0b96039dca
@ -56,34 +56,18 @@ function clean(elements, skipMatch) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @description Flat child nodes for a given element
|
|
||||||
* @param {HTMLElement} element
|
|
||||||
* @returns {number[]}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function flatElement(element) {
|
|
||||||
return [...element.childNodes].flatMap((childNode) =>
|
|
||||||
childNode.nodeType === Node.TEXT_NODE
|
|
||||||
? [childNode.nodeType]
|
|
||||||
: [...[...childNode.childNodes].map((x) => x.nodeType)]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Forces a DOM clean in the specific element
|
* @description Forces a DOM clean in the specific element
|
||||||
* @param {HTMLElement} element
|
* @param {HTMLElement} element
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function forceClean(element) {
|
function forceClean(element) {
|
||||||
if (data?.elements.length && state.enabled && !preview) {
|
|
||||||
const elements = [...element.querySelectorAll(data.elements)];
|
const elements = [...element.querySelectorAll(data.elements)];
|
||||||
|
|
||||||
if (elements.length) {
|
if (elements.length) {
|
||||||
fix();
|
fix();
|
||||||
clean(elements, true);
|
clean(elements, true);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -132,10 +116,6 @@ function match(element, skipMatch) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (element.childNodes.length && flatElement(element).every((x) => x === Node.TEXT_NODE)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (element.hasAttributes()) {
|
if (element.hasAttributes()) {
|
||||||
return (
|
return (
|
||||||
// 2023-06-10: twitch.tv temporary fix
|
// 2023-06-10: twitch.tv temporary fix
|
||||||
@ -233,14 +213,12 @@ const observer = new MutationObserver((mutations) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Fixes already existing element when page load issues
|
* @description Fixes still existing elements when page fully load
|
||||||
* @listens window#DOMContentLoaded
|
* @listens window#load
|
||||||
*/
|
*/
|
||||||
|
|
||||||
window.addEventListener('DOMContentLoaded', () => {
|
window.addEventListener('load', () => {
|
||||||
if (readingTime() < 4) {
|
window.dispatchEvent(new Event('run'));
|
||||||
forceClean(document.body);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -250,7 +228,23 @@ window.addEventListener('DOMContentLoaded', () => {
|
|||||||
|
|
||||||
window.addEventListener('pageshow', (event) => {
|
window.addEventListener('pageshow', (event) => {
|
||||||
if (event.persisted) {
|
if (event.persisted) {
|
||||||
|
window.dispatchEvent(new Event('run'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Forces a clean when this event is fired
|
||||||
|
* @listens window#run
|
||||||
|
*/
|
||||||
|
|
||||||
|
window.addEventListener('run', () => {
|
||||||
|
if (data?.elements.length && state.enabled && !preview) {
|
||||||
|
if (readingTime() < 4) {
|
||||||
forceClean(document.body);
|
forceClean(document.body);
|
||||||
|
} else {
|
||||||
|
// 2023-06-13: look into the first level of the document body, there are dialogs there very often
|
||||||
|
clean([...document.body.children]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -265,6 +259,12 @@ window.addEventListener('pageshow', (event) => {
|
|||||||
|
|
||||||
if (state.enabled) {
|
if (state.enabled) {
|
||||||
data = await dispatch({ hostname, type: 'GET_DATA' });
|
data = await dispatch({ hostname, type: 'GET_DATA' });
|
||||||
|
|
||||||
|
// 2023-06-13: hack to force clean when data request takes too long and there are no changes later
|
||||||
|
if (document.readyState === 'complete') {
|
||||||
|
window.dispatchEvent(new Event('run'));
|
||||||
|
}
|
||||||
|
|
||||||
dispatch({ type: 'ENABLE_ICON' });
|
dispatch({ type: 'ENABLE_ICON' });
|
||||||
observer.observe(document.body ?? document.documentElement, options);
|
observer.observe(document.body ?? document.documentElement, options);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user