Added killed sticky count badge

This commit is contained in:
Said Achmiz 2019-02-04 16:20:36 -05:00
parent 64e66ccc4b
commit d5812ec367
5 changed files with 108 additions and 90 deletions

View File

@ -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 */
/******************/

View File

@ -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();
}

View File

@ -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"
});

View File

@ -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": {

View File

@ -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"
});