From: Mike Frysinger Date: Fri, 7 Oct 2022 16:15:45 +0000 (+0545) Subject: popup: move prompt from background page to popup page X-Git-Tag: v2.0~1 X-Git-Url: https://git.wh0rd.org/?p=chrome-ext%2Fclearhistory-advance-fork.git;a=commitdiff_plain;h=a3c71eebbda67bf92f5f419dc39f76df1d27835b popup: move prompt from background page to popup page MV3 will change the background page to a service worker who doesn't have access to any UI, so we have to use the popup page for that. --- diff --git a/javascript/clearhistory.js b/javascript/clearhistory.js index 6cfa785..bae6a7a 100644 --- a/javascript/clearhistory.js +++ b/javascript/clearhistory.js @@ -143,24 +143,14 @@ function runCleaner() { } /** - * Executes when the user clicks the browser action. Uses stored values from - * {@code localStorage} + * Callback from other pages (like the popup). */ -chrome.browserAction.onClicked.addListener(function(tab) { - // Get the value from localStorage - chrome.storage.sync.get('prompt', function(s) { - var showPrompt = s.prompt || CONSTANTS.YES; - - // The confirmation message to ask - var message = chrome.i18n.getMessage('confirmPrompt'); - - // Clear cookies, and then clear the history - if (showPrompt === CONSTANTS.YES) { - if (!confirm(message)) - return; +chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + switch (request?.action) { + case 'runCleaner': + runCleaner(); + break; } - runCleaner(); - }); }); chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) { diff --git a/javascript/popup.js b/javascript/popup.js new file mode 100644 index 0000000..cba1e10 --- /dev/null +++ b/javascript/popup.js @@ -0,0 +1,25 @@ +/** + * @fileoverview Executes when the user clicks the browser action. + */ + +import {CONSTANTS} from './background.js'; + +async function init() { + const {prompt = CONSTANTS.YES} = await chrome.storage.sync.get('prompt'); + + if (prompt === CONSTANTS.YES) { + // The confirmation message to ask. + const message = chrome.i18n.getMessage('confirmPrompt'); + + if (!confirm(message)) { + window.close(); + return; + } + } + + // Notify background page to do the actual clear. + await chrome.runtime.sendMessage({action: 'runCleaner'}); + + window.close(); +} +init(); diff --git a/manifest.files b/manifest.files index b9a3a9b..c6b2103 100644 --- a/manifest.files +++ b/manifest.files @@ -8,6 +8,8 @@ images/icon128.png javascript/background.js javascript/clearhistory.js javascript/options.js +javascript/popup.js javascript/util.js views/background.html views/options.html +views/popup.html diff --git a/manifest.json b/manifest.json index 5094d6e..1929cd9 100644 --- a/manifest.json +++ b/manifest.json @@ -22,7 +22,8 @@ "options_page": "views/options.html", "browser_action": { "default_icon": "images/icon32.png", - "default_title": "__MSG_title__" + "default_title": "__MSG_title__", + "default_popup": "views/popup.html" }, "icons": { "16": "images/icon16.png", diff --git a/views/popup.html b/views/popup.html new file mode 100644 index 0000000..984372a --- /dev/null +++ b/views/popup.html @@ -0,0 +1,9 @@ + + + + + + + + +