diff --git a/src/contentScript.js b/src/contentScript.js index d0f9ce9..2a0a2a9 100644 --- a/src/contentScript.js +++ b/src/contentScript.js @@ -51,26 +51,6 @@ function checkForShouldKillSticky(result) { return shouldKillSticky; } -/* Some websites add sticky elements after loading, or make existing elements - sticky (either on a timer, or based on user actions). CONSTANT VIGILANCE - counteracts this behavior, by watching the DOM for mutations, and, if - necessary, killing any newly-formed stickies. - */ -function startConstantVigilance() { - var observer = new MutationObserver((mutationsList, observer) => { - for (var mutation of mutationsList) { - killSticky(mutation.target); - } - }); - - console.log("Commencing vigilance for stickies!"); - observer.observe(document.querySelector("html"), { - attributes: true, - childList: true, - subtree: true - }); -} - /******************/ /* INITIALIZATION */ /******************/ @@ -80,10 +60,6 @@ function initialize() { let shouldKillSticky = checkForShouldKillSticky(result); updateIcon(shouldKillSticky); if (shouldKillSticky) { - function beginKillingStickies() { - killSticky(); - startConstantVigilance(); - } if (document.readyState == "loading") { document.addEventListener("DOMContentLoaded", beginKillingStickies); } else { diff --git a/src/functions.js b/src/functions.js index 4c605ce..d2f7b8f 100644 --- a/src/functions.js +++ b/src/functions.js @@ -47,13 +47,11 @@ function updateIcon(shouldKillSticky, tabID) { 'sticky' or 'fixed', and removes those elements. */ function killSticky(root) { - var message = root ? "Killing all stickies!" : "Killing more stickies!"; - console.log(message); - root = root || document.querySelector("body"); root.querySelectorAll('*').forEach(element => { if (getComputedStyle(element).position === 'fixed' || getComputedStyle(element).position === 'sticky') { + console.log("Killing sticky!"); element.remove(); } }); @@ -70,3 +68,31 @@ function restoreScrollability() { container.style.setProperty("overflow-y", "auto", "important"); }); } + +/* Some websites add sticky elements after loading, or make existing elements + sticky (either on a timer, or based on user actions). CONSTANT VIGILANCE + counteracts this behavior, by watching the DOM for mutations, and, if + necessary, killing any newly-formed stickies. + */ +function startConstantVigilance() { + var observer = new MutationObserver((mutationsList, observer) => { + for (var mutation of mutationsList) { + killSticky(mutation.target); + } + }); + + console.log("Commencing vigilance for stickies!"); + observer.observe(document.querySelector("html"), { + attributes: true, + childList: true, + subtree: true + }); +} + +/* Immediately kill all stickies, then commence constant vigilance. + */ +function beginKillingStickies() { + killSticky(); + startConstantVigilance(); +} + diff --git a/src/popup.js b/src/popup.js index af1f76d..7d66aba 100644 --- a/src/popup.js +++ b/src/popup.js @@ -249,7 +249,7 @@ function initialize() { Otherwise, show the reload button. */ let shouldKillSticky = AKS.pageMatched && !AKS.pageExcluded; if (AKS.pageMatched && !AKS.pageExcluded) { - chrome.tabs.executeScript(null, { code: 'killSticky()' }); + chrome.tabs.executeScript(null, { code: 'beginKillingStickies()' }); reloadButton.classList.toggle("active", false); } else { reloadButton.classList.toggle("active", true);