]> git.wh0rd.org Git - chrome-ext/clearhistory-advance-fork.git/commitdiff
popup: move prompt from background page to popup page
authorMike Frysinger <vapier@gentoo.org>
Fri, 7 Oct 2022 16:15:45 +0000 (22:00 +0545)
committerMike Frysinger <vapier@gentoo.org>
Fri, 7 Oct 2022 16:15:45 +0000 (22:00 +0545)
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.

javascript/clearhistory.js
javascript/popup.js [new file with mode: 0644]
manifest.files
manifest.json
views/popup.html [new file with mode: 0644]

index 6cfa78562e94de461488ec642c1ce8bb13e4c0c1..bae6a7a09f06ced0edc577ea35d5bb58771698d3 100644 (file)
@@ -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 (file)
index 0000000..cba1e10
--- /dev/null
@@ -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();
index b9a3a9b59154005957773e7e6afff863eb9668e4..c6b21037fa91f688776838aa52d3c10670e72d89 100644 (file)
@@ -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
index 5094d6e6c477f8b8f2b072ddcf2ea7afaa8fea62..1929cd9eef509d3acef174a861fc5411d0ecba14 100644 (file)
@@ -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 (file)
index 0000000..984372a
--- /dev/null
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset='utf-8'/>
+    <script type='module' src='../javascript/popup.js'></script>
+  </head>
+<body bgcolor='black'>
+</body>
+</html>