]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blobdiff - javascript/clearhistory.js
convert to MV3
[chrome-ext/clearhistory-advance-fork.git] / javascript / clearhistory.js
index f614fff5e9844130d6ba4d74e0f3c9323f3dc4c6..bae6a7a09f06ced0edc577ea35d5bb58771698d3 100644 (file)
@@ -5,6 +5,9 @@
  * @author arunjit@google.com (Arunjit Singh)
  */
 
+import {CONSTANTS} from './background.js';
+import {getUnitsForTime} from './util.js';
+
 function getClearDate(s) {
   var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
   if (time == -1)
@@ -65,21 +68,27 @@ function clearDownloads(s) {
  * Callback for when history deletion is successful
  */
 function didClearHistory(s) {
+  if (s.notify !== CONSTANTS.YES)
+    return;
+
   var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
   time = getUnitsForTime(time);
-  var timeString = (time.time === -1) ?
-                   chrome.i18n.getMessage('notificationTimeAll') :
-                   chrome.i18n.getMessage('notificationTime',
-                                          [time.time, time.units]);
+  var timeMsg = (time.time === -1) ? 'notificationTimeAll' :
+                (s.timeStart === CONSTANTS.YES) ? 'notificationTimeNewer' :
+                'notificationTimeOlder';
+  var timeString = chrome.i18n.getMessage(timeMsg, [time.time, time.units]);
   var message = chrome.i18n.getMessage('notificationBody', timeString);
-  var notification = webkitNotifications.createNotification(
-      chrome.extension.getURL('/images/icon48.png'),
-      chrome.i18n.getMessage('notificationTitle'),
-      message);
-  notification.show();
-  setTimeout(function() {
-    notification.cancel();
-  }, 5000);
+  var options = {
+    type: 'basic',
+    title: chrome.i18n.getMessage('notificationTitle'),
+    message: message,
+    iconUrl: chrome.extension.getURL('/images/icon128.png'),
+  };
+
+  var id = 'clearhistory advance notify';
+  chrome.notifications.clear(id, function(wasCleared) {
+    chrome.notifications.create(id, options, function(){});
+  });
 }
 
 /**
@@ -100,7 +109,7 @@ function clearHistory(s) {
       var endTime = Date.now();
     } else {
       var endTime = time;
-      time = 0;
+      startTime = 0;
     }
     var range = {
       startTime: startTime,
@@ -122,7 +131,7 @@ function clearAll(s) {
 
 function runCleaner() {
   var keys = [
-    'cookies', 'downloads', 'time', 'timeStart',
+    'cookies', 'downloads', 'time', 'timeStart', 'notify',
   ];
   chrome.storage.sync.get(keys, function(s) {
     if (s.cookies === CONSTANTS.YES) {
@@ -134,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) {