From d5812ec367629ffea8c06ac436997043c8a7deec Mon Sep 17 00:00:00 2001 From: Said Achmiz Date: Mon, 4 Feb 2019 16:20:36 -0500 Subject: [PATCH] Added killed sticky count badge --- src/contentScript.js | 63 ++++++++++++++++++++++++++++++ src/functions.js | 54 ------------------------- src/platform/chrome/background.js | 54 ++++++++++++------------- src/platform/chrome/manifest.json | 4 +- src/platform/firefox/background.js | 23 ++++++++--- 5 files changed, 108 insertions(+), 90 deletions(-) diff --git a/src/contentScript.js b/src/contentScript.js index 2a0a2a9..bfa70f5 100644 --- a/src/contentScript.js +++ b/src/contentScript.js @@ -2,6 +2,7 @@ /* HELPERS */ /***********/ +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* Given the result of a query to storage (for the matching and exclusion pattern lists, and the current mode - whitelist or blacklist), this function determines whether stickies should be killed on the currently loaded page. @@ -51,6 +52,68 @@ function checkForShouldKillSticky(result) { return shouldKillSticky; } +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +/* This is the code that actually does the sticky killing! It simply selects + all elements whose 'position' CSS property has a computed value of either + 'sticky' or 'fixed', and removes those elements. + */ +function killSticky(root) { + root = root || document.querySelector("body"); + root.querySelectorAll('*').forEach(element => { + if (getComputedStyle(element).position === 'fixed' || + getComputedStyle(element).position === 'sticky') { + console.log("Killing sticky!"); + element.remove(); + AKS.stickiesKilled++; + chrome.runtime.sendMessage({ newBadgeText: "" + AKS.stickiesKilled }); + } + }); + + // Compensate for full-screen paywalls. + restoreScrollability(); +} + +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +/* Full-screen paywalls not only bring up sticky elements, they also make + the page un-scrollable. This ensures that the page remains scrollable. + */ +function restoreScrollability() { + document.querySelectorAll("html, body").forEach(container => { + 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() { + if (!window.AKS) window.AKS = { stickiesKilled: 0 }; + + killSticky(); + startConstantVigilance(); +} + /******************/ /* INITIALIZATION */ /******************/ diff --git a/src/functions.js b/src/functions.js index d2f7b8f..7c7e1c7 100644 --- a/src/functions.js +++ b/src/functions.js @@ -42,57 +42,3 @@ function updateIcon(shouldKillSticky, tabID) { chrome.runtime.sendMessage({ "killingStickies" : shouldKillSticky, "tabID": tabID }); } -/* This is the code that actually does the sticky killing! It simply selects - all elements whose 'position' CSS property has a computed value of either - 'sticky' or 'fixed', and removes those elements. - */ -function killSticky(root) { - root = root || document.querySelector("body"); - root.querySelectorAll('*').forEach(element => { - if (getComputedStyle(element).position === 'fixed' || - getComputedStyle(element).position === 'sticky') { - console.log("Killing sticky!"); - element.remove(); - } - }); - - // Compensate for full-screen paywalls. - restoreScrollability(); -} - -/* Full-screen paywalls not only bring up sticky elements, they also make - the page un-scrollable. This ensures that the page remains scrollable. - */ -function restoreScrollability() { - document.querySelectorAll("html, body").forEach(container => { - 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/platform/chrome/background.js b/src/platform/chrome/background.js index 740c807..e8f012d 100644 --- a/src/platform/chrome/background.js +++ b/src/platform/chrome/background.js @@ -1,16 +1,3 @@ -/* Enables the page action (i.e., the browser toolbar icon). - */ -chrome.runtime.onInstalled.addListener(() => { - chrome.declarativeContent.onPageChanged.removeRules(undefined, () => { - chrome.declarativeContent.onPageChanged.addRules([{ - conditions: [ new chrome.declarativeContent.PageStateMatcher({ - pageUrl: { schemes: [ 'http', 'https' ] }, - }) - ], actions: [ new chrome.declarativeContent.ShowPageAction() ] - }]); - }); -}); - /* Toggle the toolbar icon based on whether stickies are, or are not, set to be killed on the current tab. @@ -19,19 +6,30 @@ chrome.runtime.onInstalled.addListener(() => { tab are. */ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - let icons = request.killingStickies ? { - "16": "images/ASK_on_16.png", - "32": "images/ASK_on_32.png", - "48": "images/ASK_on_48.png", - "128": "images/ASK_on_128.png" - } : { - "16": "images/ASK_off_16.png", - "32": "images/ASK_off_32.png", - "48": "images/ASK_off_48.png", - "128": "images/ASK_off_128.png" - }; - chrome.pageAction.setIcon({ - path: icons, - tabId: request.tabID || sender.tab.id - }); + if (typeof request.killingStickies != "undefined") { + let icons = request.killingStickies ? { + "16": "images/ASK_on_16.png", + "32": "images/ASK_on_32.png", + "48": "images/ASK_on_48.png", + "128": "images/ASK_on_128.png" + } : { + "16": "images/ASK_off_16.png", + "32": "images/ASK_off_32.png", + "48": "images/ASK_off_48.png", + "128": "images/ASK_off_128.png" + }; + chrome.browserAction.setIcon({ + path: icons, + tabId: request.tabID || sender.tab.id + }); + } else if (typeof request.newBadgeText != "undefined") { + chrome.browserAction.setBadgeText({ + tabId: sender.tab.id, + text: request.newBadgeText + }); + } +}); + +chrome.browserAction.setBadgeBackgroundColor({ + color: "#666" }); \ No newline at end of file diff --git a/src/platform/chrome/manifest.json b/src/platform/chrome/manifest.json index 94fd41b..6674083 100644 --- a/src/platform/chrome/manifest.json +++ b/src/platform/chrome/manifest.json @@ -5,7 +5,7 @@ "description": "Get rid of sticky elements on websites - permanently!", "author": "Said Achmiz", "homepage_url": "https://git.sr.ht/~achmizs/AlwaysKillSticky.git", - "permissions": [ "declarativeContent", "activeTab", "storage" ], + "permissions": [ "activeTab", "storage" ], "background": { "scripts": ["background.js"], "persistent": false @@ -16,7 +16,7 @@ "js": [ "functions.js", "contentScript.js" ] } ], - "page_action": { + "browser_action": { "default_popup": "popup.html", "default_title": "AlwaysKillSticky - Click the icon to control sticky-killing on this site!", "default_icon": { diff --git a/src/platform/firefox/background.js b/src/platform/firefox/background.js index d0ade03..0fac3cf 100644 --- a/src/platform/firefox/background.js +++ b/src/platform/firefox/background.js @@ -6,15 +6,26 @@ tab are. */ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - let icons = request.killingStickies ? { + if (typeof request.killingStickies != "undefined") { + let icons = request.killingStickies ? { "19": "images/ASK_on_19.png", "38": "images/ASK_on_38.png", } : { "19": "images/ASK_off_19.png", "38": "images/ASK_off_38.png", - }; - chrome.browserAction.setIcon({ - path: icons, - tabId: request.tabID || sender.tab.id - }); + }; + chrome.browserAction.setIcon({ + path: icons, + tabId: request.tabID || sender.tab.id + }); + } else if (typeof request.newBadgeText != "undefined") { + chrome.browserAction.setBadgeText({ + tabId: sender.tab.id, + text: request.newBadgeText + }); + } +}); + +chrome.browserAction.setBadgeBackgroundColor({ + color: "#666" }); \ No newline at end of file