Skip to content

Commit

Permalink
Merge pull request #33 from yakisova41/fix-head-find-process
Browse files Browse the repository at this point in the history
Fix head search process
  • Loading branch information
yakisova41 authored Dec 8, 2024
2 parents 3c4f21b + eb83f97 commit fa9a931
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 fa9a931

Please sign in to comment.