]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blobdiff - javascript/clearhistory.js
add support for clearing download history
[chrome-ext/clearhistory-advance-fork.git] / javascript / clearhistory.js
index 73206ce175b6d6b37d031c4b1cbae9cbed3eeca7..f614fff5e9844130d6ba4d74e0f3c9323f3dc4c6 100644 (file)
@@ -5,6 +5,13 @@
  * @author arunjit@google.com (Arunjit Singh)
  */
 
+function getClearDate(s) {
+  var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
+  if (time == -1)
+    return time;
+  return (Date.now() - (time * 60 * 60 * 1000));  // time from hrs to ms
+}
+
 /**
  * Removes a single cookie
  * @param {Object.<Cookie>} cookie The cookie to delete.
@@ -21,7 +28,7 @@ function removeCookie(cookie, callback) {
  * @param {Function=} callback The function to invoke when all cookies are
  *     deleted (optional).
  */
-function clearCookies(callback) {
+function clearCookies(callback, s) {
   callback = callback || function() {};
   chrome.cookies.getAll({}, function(cookies) {
     var len = cookies.length;
@@ -29,7 +36,7 @@ function clearCookies(callback) {
     // cookie deletion functions have called back (finished).
     (function doCookieCleanup(i) {
       if (i === len) {
-        callback();
+        callback(s);
       } else {
         removeCookie(cookies[i], function() {
           doCookieCleanup(i + 1);
@@ -39,11 +46,25 @@ function clearCookies(callback) {
   });
 }
 
+/**
+ * Clear download history
+ */
+function clearDownloads(s) {
+  var query = {};
+  var date = new Date(getClearDate(s));
+  // chrome.downloads expects ISO 8601
+  var iso8601 = date.toISOString();
+  if (s.timeStart === CONSTANTS.YES)
+    query.startedAfter = iso8601;
+  else
+    query.startedBefore = iso8601;
+  chrome.downloads.erase(query);
+}
+
 /**
  * Callback for when history deletion is successful
  */
-function didClearHistory() {
-  chrome.storage.sync.get('time', function(s) {
+function didClearHistory(s) {
   var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
   time = getUnitsForTime(time);
   var timeString = (time.time === -1) ?
@@ -59,49 +80,55 @@ function didClearHistory() {
   setTimeout(function() {
     notification.cancel();
   }, 5000);
-  });
 }
 
 /**
  * Clears the history for the amount of time stored in {@code localStorage}
  */
-function clearHistory() {
+function clearHistory(s) {
   //Get the values from localStorage
   // time is in hours
-  chrome.storage.sync.get(['time', 'timeStart'], function(s) {
-    var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
-    if (time === -1) {
-      // Delete everything
-      chrome.history.deleteAll(didClearHistory);
+  var callback = function() { didClearHistory(s); };
+  var time = getClearDate(s);
+  if (time === -1) {
+    // Delete everything
+    chrome.history.deleteAll(callback);
+  } else {
+    // Create the range
+    var startTime = time;
+    if (s.timeStart === CONSTANTS.YES) {
+      var endTime = Date.now();
     } else {
-      // Create the range
-      var timeStart = s.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: startTime,
-        endTime: endTime
-      };
-      // Delete history in the range
-      chrome.history.deleteRange(range, didClearHistory);
+      var endTime = time;
+      time = 0;
     }
-  });
+    var range = {
+      startTime: startTime,
+      endTime: endTime
+    };
+    // Delete history in the range
+    chrome.history.deleteRange(range, callback);
+  }
 }
 
-function runCleaner() {
-  chrome.storage.sync.get('cookies', function(s) {
-    var clearCookies = s.cookies || CONSTANTS.NO;
+/**
+ * Run through all the clearing funcs
+ */
+function clearAll(s) {
+  if (s.downloads === CONSTANTS.YES)
+    clearDownloads(s);
+  clearHistory(s);
+}
 
-    if (clearCookies === CONSTANTS.YES) {
-      clearCookies(clearHistory);
+function runCleaner() {
+  var keys = [
+    'cookies', 'downloads', 'time', 'timeStart',
+  ];
+  chrome.storage.sync.get(keys, function(s) {
+    if (s.cookies === CONSTANTS.YES) {
+      clearCookies(clearAll, s);
     } else {
-      clearHistory();
+      clearAll(s);
     }
   });
 }