Skip to content

Commit

Permalink
Fix head search process
Browse files Browse the repository at this point in the history
  • Loading branch information
yakisova41 committed Dec 8, 2024
1 parent 3c4f21b commit eb83f97
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions extension/userscript.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,25 @@ function replaceManifest(head) {
head.prepend(manifestEl);
}

/**
* Run a callback in no time when a head element is found.
* @param {*} callback
*/
function headFinder(callback) {
const observer = new MutationObserver((mutations, obs) => {
const head = document.querySelector("head");
if (head) {
callback(head);
obs.disconnect();
}
});

observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
}

/**
* スクリプトを実行
*/
Expand All @@ -728,26 +747,15 @@ function main() {
if (head !== null && head !== undefined) {
headFound(head);
} else {
const i = setInterval(() => {
const head = document.head;
if (head !== undefined && head !== null) {
clearInterval(i);
headFound(head);
}
headFinder((head) => {
headFound(head);
});
}
} else {
// extension
const i = setInterval(() => {
const head = document.head;
if (head !== null) {
clearInterval(i);
headFound(head);

setTimeout(() => {
replaceManifest(head);
}, 100);
}
headFinder((head) => {
headFound(head);
replaceManifest(head);
});
}
}
Expand Down

0 comments on commit eb83f97

Please sign in to comment.