Page action icon now updated immediately on state toggle; some refactoring

This commit is contained in:
Said Achmiz 2019-02-01 13:31:47 -05:00
parent cb247d23ed
commit a32dd45c76
4 changed files with 23 additions and 9 deletions

View File

@ -32,6 +32,6 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
}; };
chrome.pageAction.setIcon({ chrome.pageAction.setIcon({
path: icons, path: icons,
tabId: sender.tab.id tabId: request.tabID || sender.tab.id
}); });
}); });

View File

@ -51,14 +51,6 @@ function checkForShouldKillSticky(result) {
return shouldKillSticky; return shouldKillSticky;
} }
/* This function sends a message to the background script (background.js),
which then updates the page action icon (i.e., the browser toolbar icon)
to reflect whether killing stickies is enabled on the current page.
*/
function updateIcon(shouldKillSticky) {
chrome.runtime.sendMessage({ "killingStickies" : shouldKillSticky });
}
/******************/ /******************/
/* INITIALIZATION */ /* INITIALIZATION */
/******************/ /******************/

View File

@ -34,6 +34,14 @@ String.prototype.hasPrefix = function (prefix) {
/* HELPERS */ /* HELPERS */
/***********/ /***********/
/* This function sends a message to the background script (background.js),
which then updates the page action icon (i.e., the browser toolbar icon)
to reflect whether killing stickies is enabled on the current page.
*/
function updateIcon(shouldKillSticky, tabID) {
chrome.runtime.sendMessage({ "killingStickies" : shouldKillSticky, "tabID": tabID });
}
/* This is the code that actually does the sticky killing! It simply selects /* 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 all elements whose 'position' CSS property has a computed value of either
'sticky' or 'fixed', and removes those elements. 'sticky' or 'fixed', and removes those elements.

View File

@ -200,6 +200,7 @@ function initialize() {
// Retrieve saved settings. // Retrieve saved settings.
chrome.tabs.query({currentWindow: true, active: true}, (tabs) => { chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
AKS.activeTabLocation = tabs[0].url; AKS.activeTabLocation = tabs[0].url;
AKS.activeTabID = tabs[0].id;
chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => { chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => {
updateState(result); updateState(result);
updateUIState(); updateUIState();
@ -208,23 +209,36 @@ function initialize() {
// Listener for main button. // Listener for main button.
document.querySelector("button.main-button").addActivateEvent((event) => { document.querySelector("button.main-button").addActivateEvent((event) => {
/* This doesnt actually kill the stickies yet; thats below, in the
callback to storage.sync.set. */
toggleState(); toggleState();
// Prepare the changes for saving.
var changes = { var changes = {
"exclusionPatterns": AKS.exclusionPatterns.join("\n"), "exclusionPatterns": AKS.exclusionPatterns.join("\n"),
"mode": AKS.mode "mode": AKS.mode
}; };
if (AKS.mode == "blacklist") if (AKS.mode == "blacklist")
changes.matchingPatterns = AKS.matchingPatterns.join("\n"); changes.matchingPatterns = AKS.matchingPatterns.join("\n");
// Save the changes.
chrome.storage.sync.set(changes, () => { chrome.storage.sync.set(changes, () => {
// Update the UI, once changes are saved.
updateUIState(); updateUIState();
let reloadButton = document.querySelector("button.reload-button"); let reloadButton = document.querySelector("button.reload-button");
/* If need be, actually kill stickies on the current page.
Otherwise, show the reload button. */
let shouldKillSticky = AKS.pageMatched && !AKS.pageExcluded;
if (AKS.pageMatched && !AKS.pageExcluded) { if (AKS.pageMatched && !AKS.pageExcluded) {
chrome.tabs.executeScript(null, { code: 'killSticky()' }); chrome.tabs.executeScript(null, { code: 'killSticky()' });
reloadButton.classList.toggle("active", false); reloadButton.classList.toggle("active", false);
} else { } else {
reloadButton.classList.toggle("active", true); reloadButton.classList.toggle("active", true);
} }
// Update the page action (toolbar) icon.
updateIcon(shouldKillSticky, AKS.activeTabID);
}); });
}); });