From a32dd45c765761ca6e229800adfbbc2615bd06c2 Mon Sep 17 00:00:00 2001 From: achmizs Date: Fri, 1 Feb 2019 13:31:47 -0500 Subject: [PATCH] Page action icon now updated immediately on state toggle; some refactoring --- background.js | 2 +- contentScript.js | 8 -------- functions.js | 8 ++++++++ popup.js | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/background.js b/background.js index 4e61808..9527931 100644 --- a/background.js +++ b/background.js @@ -32,6 +32,6 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { }; chrome.pageAction.setIcon({ path: icons, - tabId: sender.tab.id + tabId: request.tabID || sender.tab.id }); }); \ No newline at end of file diff --git a/contentScript.js b/contentScript.js index 1b86eea..cea8bcc 100644 --- a/contentScript.js +++ b/contentScript.js @@ -51,14 +51,6 @@ function checkForShouldKillSticky(result) { 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 */ /******************/ diff --git a/functions.js b/functions.js index 525c62d..7edb698 100644 --- a/functions.js +++ b/functions.js @@ -34,6 +34,14 @@ String.prototype.hasPrefix = function (prefix) { /* 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 all elements whose 'position' CSS property has a computed value of either 'sticky' or 'fixed', and removes those elements. diff --git a/popup.js b/popup.js index eec8f93..bca1c4f 100644 --- a/popup.js +++ b/popup.js @@ -200,6 +200,7 @@ function initialize() { // Retrieve saved settings. chrome.tabs.query({currentWindow: true, active: true}, (tabs) => { AKS.activeTabLocation = tabs[0].url; + AKS.activeTabID = tabs[0].id; chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => { updateState(result); updateUIState(); @@ -208,23 +209,36 @@ function initialize() { // Listener for main button. document.querySelector("button.main-button").addActivateEvent((event) => { + /* This doesn’t actually kill the stickies yet; that’s below, in the + callback to storage.sync.set. */ toggleState(); + // Prepare the changes for saving. var changes = { "exclusionPatterns": AKS.exclusionPatterns.join("\n"), "mode": AKS.mode }; if (AKS.mode == "blacklist") changes.matchingPatterns = AKS.matchingPatterns.join("\n"); + + // Save the changes. chrome.storage.sync.set(changes, () => { + // Update the UI, once changes are saved. updateUIState(); 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) { chrome.tabs.executeScript(null, { code: 'killSticky()' }); reloadButton.classList.toggle("active", false); } else { reloadButton.classList.toggle("active", true); } + + // Update the page action (toolbar) icon. + updateIcon(shouldKillSticky, AKS.activeTabID); }); });