]> git.wh0rd.org Git - chrome-ext/clearhistory-advance-fork.git/blob - javascript/clearhistory.js
28eb2b060676ec161b554e177389f5316b24885a
[chrome-ext/clearhistory-advance-fork.git] / javascript / clearhistory.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
2
3 /**
4  * @fileoverview The main script for the Clear History extension for Chrome.
5  * @author arunjit@google.com (Arunjit Singh)
6  */
7
8 function getClearDate(s) {
9   var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
10   if (time == -1)
11     return time;
12   return (Date.now() - (time * 60 * 60 * 1000));  // time from hrs to ms
13 }
14
15 /**
16  * Removes a single cookie
17  * @param {Object.<Cookie>} cookie The cookie to delete.
18  * @param {Function} callback The function to invoke when the cookie is deleted.
19  */
20 function removeCookie(cookie, callback) {
21   var url = 'http' + (cookie.secure ? 's' : '') + '://' + cookie.domain +
22             cookie.path;
23   chrome.cookies.remove({url: url, name: cookie.name}, callback);
24 }
25
26 /**
27  * Removes all cookies
28  * @param {Function=} callback The function to invoke when all cookies are
29  *     deleted (optional).
30  */
31 function clearCookies(callback, s) {
32   callback = callback || function() {};
33   chrome.cookies.getAll({}, function(cookies) {
34     var len = cookies.length;
35     // Synchronize the cookie deletion and execute the callback only when all
36     // cookie deletion functions have called back (finished).
37     (function doCookieCleanup(i) {
38       if (i === len) {
39         callback(s);
40       } else {
41         removeCookie(cookies[i], function() {
42           doCookieCleanup(i + 1);
43         });
44       }
45     })(0);
46   });
47 }
48
49 /**
50  * Clear download history
51  */
52 function clearDownloads(s) {
53   var query = {};
54   var date = new Date(getClearDate(s));
55   // chrome.downloads expects ISO 8601
56   var iso8601 = date.toISOString();
57   if (s.timeStart === CONSTANTS.YES)
58     query.startedAfter = iso8601;
59   else
60     query.startedBefore = iso8601;
61   chrome.downloads.erase(query);
62 }
63
64 /**
65  * Callback for when history deletion is successful
66  */
67 function didClearHistory(s) {
68   var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
69   time = getUnitsForTime(time);
70   var timeMsg = (time.time === -1) ? 'notificationTimeAll' :
71                 (s.timeStart === CONSTANTS.YES) ? 'notificationTimeNewer' :
72                 'notificationTimeOlder';
73   var timeString = chrome.i18n.getMessage(timeMsg, [time.time, time.units]);
74   var message = chrome.i18n.getMessage('notificationBody', timeString);
75   var notification = webkitNotifications.createNotification(
76       chrome.extension.getURL('/images/icon48.png'),
77       chrome.i18n.getMessage('notificationTitle'),
78       message);
79   notification.show();
80   setTimeout(function() {
81     notification.cancel();
82   }, 5000);
83 }
84
85 /**
86  * Clears the history for the amount of time stored in {@code localStorage}
87  */
88 function clearHistory(s) {
89   //Get the values from localStorage
90   // time is in hours
91   var callback = function() { didClearHistory(s); };
92   var time = getClearDate(s);
93   if (time === -1) {
94     // Delete everything
95     chrome.history.deleteAll(callback);
96   } else {
97     // Create the range
98     var startTime = time;
99     if (s.timeStart === CONSTANTS.YES) {
100       var endTime = Date.now();
101     } else {
102       var endTime = time;
103       time = 0;
104     }
105     var range = {
106       startTime: startTime,
107       endTime: endTime
108     };
109     // Delete history in the range
110     chrome.history.deleteRange(range, callback);
111   }
112 }
113
114 /**
115  * Run through all the clearing funcs
116  */
117 function clearAll(s) {
118   if (s.downloads === CONSTANTS.YES)
119     clearDownloads(s);
120   clearHistory(s);
121 }
122
123 function runCleaner() {
124   var keys = [
125     'cookies', 'downloads', 'time', 'timeStart',
126   ];
127   chrome.storage.sync.get(keys, function(s) {
128     if (s.cookies === CONSTANTS.YES) {
129       clearCookies(clearAll, s);
130     } else {
131       clearAll(s);
132     }
133   });
134 }
135
136 /**
137  * Executes when the user clicks the browser action. Uses stored values from
138  * {@code localStorage}
139  */
140 chrome.browserAction.onClicked.addListener(function(tab) {
141   // Get the value from localStorage
142   chrome.storage.sync.get('prompt', function(s) {
143   var showPrompt = s.prompt || CONSTANTS.YES;
144
145   // The confirmation message to ask
146   var message = chrome.i18n.getMessage('confirmPrompt');
147
148   // Clear cookies, and then clear the history
149   if (showPrompt === CONSTANTS.YES) {
150     if (!confirm(message))
151       return;
152   }
153   runCleaner();
154   });
155 });
156
157 chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) {
158   if (a)
159     return;
160
161   chrome.alarms.create(CONSTANTS.CLEANER_ALARM, {
162     // First fire 10 minutes from now.
163     'when': Date.now() + (10 * 60 * 1000),
164     'periodInMinutes': 24 * 60
165   });
166 });
167
168 chrome.alarms.onAlarm.addListener(function(alarm) {
169   chrome.storage.sync.get('autoclear', function(s) {
170     var autoCleaner = s.autoclear || CONSTANTS.NO;
171     if (autoCleaner === CONSTANTS.YES)
172       runCleaner();
173   });
174 });