]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blobdiff - javascript/clearhistory.js
fix timer init
[chrome-ext/clearhistory-advance-fork.git] / javascript / clearhistory.js
index af8fdcebc4184ebe38963620be859328a55da2fa..d05a2f694f9581438c57bcad119b50bd9d354583 100644 (file)
@@ -72,23 +72,39 @@ function clearHistory() {
     chrome.history.deleteAll(didClearHistory);
   } else {
     // Create the range
+    var timeStart = localStorage['timeStart'] === CONSTANTS.YES;
     var now = (new Date).getTime();
+    var startTime = (now - time * 60 * 60 * 1000);  // time from hrs to ms
+    if (timeStart) {
+      var endTime = now;
+    } else {
+      var endTime = startTime;
+      startTime = 0;
+    }
     var range = {
-      startTime: (now - time * 60 * 60 * 1000),  // time from hrs to ms
-      endTime: now
+      startTime: startTime,
+      endTime: endTime
     };
     // Delete history in the range
     chrome.history.deleteRange(range, didClearHistory);
   }
 }
 
+function runCleaner() {
+  var clearCookies = localStorage['cookies'] || CONSTANTS.NO;
+
+  if (clearCookies === CONSTANTS.YES) {
+    clearCookies(clearHistory);
+  } else {
+    clearHistory();
+  }
+}
+
 /**
  * Executes when the user clicks the browser action. Uses stored values from
  * {@code localStorage}
  */
 chrome.browserAction.onClicked.addListener(function(tab) {
-  //calls the method to provide the Google Analytics Data
-  getAnalyticsData();
   // Get the value from localStorage
   var showPrompt = localStorage['prompt'] || CONSTANTS.YES;
 
@@ -97,25 +113,26 @@ chrome.browserAction.onClicked.addListener(function(tab) {
 
   // Clear cookies, and then clear the history
   if (showPrompt === CONSTANTS.YES) {
-    confirm(message) && clearCookies(clearHistory);
-  } else {
-    clearCookies(clearHistory);
+    if (!confirm(message))
+      return;
   }
+  runCleaner();
 });
 
-   var _gaq = _gaq || [];
-/**
- * provides the Google analytics data
- */
-function getAnalyticsData() {
-  _gaq.push(['_setAccount', 'UA-28968723-1']);
-  _gaq.push(['_trackPageview']);
+chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) {
+  if (a)
+    return;
 
-  (function() {
-    var ga = document.createElement('script'); ga.type = 'text/javascript';
-    ga.async = true;
-    ga.src = 'https://ssl.google-analytics.com/ga.js';
-    var s = document.getElementsByTagName('script')[0];
-    s.parentNode.insertBefore(ga, s);
-  })();
-}
+  chrome.alarms.create(CONSTANTS.CLEANER_ALARM, {
+    // First fire 10 minutes from now.
+    'when': Date.now() + (10 * 60 * 1000),
+    'periodInMinutes': 24 * 60
+  });
+});
+
+chrome.alarms.onAlarm.addListener(function(alarm) {
+  var autoCleaner = localStorage['autoclear'] || CONSTANTS.NO;
+  if (autoCleaner === CONSTANTS.YES)
+    runCleaner();
+  console.log('running');
+});