1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview The main script for the Clear History extension for Chrome.
5 * @author arunjit@google.com (Arunjit Singh)
8 function getClearDate(s) {
9 var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
12 return (Date.now() - (time * 60 * 60 * 1000)); // time from hrs to ms
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.
20 function removeCookie(cookie, callback) {
21 var url = 'http' + (cookie.secure ? 's' : '') + '://' + cookie.domain +
23 chrome.cookies.remove({url: url, name: cookie.name}, callback);
28 * @param {Function=} callback The function to invoke when all cookies are
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) {
41 removeCookie(cookies[i], function() {
42 doCookieCleanup(i + 1);
50 * Clear download history
52 function clearDownloads(s) {
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;
60 query.startedBefore = iso8601;
61 chrome.downloads.erase(query);
65 * Callback for when history deletion is successful
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'),
80 setTimeout(function() {
81 notification.cancel();
86 * Clears the history for the amount of time stored in {@code localStorage}
88 function clearHistory(s) {
89 //Get the values from localStorage
91 var callback = function() { didClearHistory(s); };
92 var time = getClearDate(s);
95 chrome.history.deleteAll(callback);
99 if (s.timeStart === CONSTANTS.YES) {
100 var endTime = Date.now();
106 startTime: startTime,
109 // Delete history in the range
110 chrome.history.deleteRange(range, callback);
115 * Run through all the clearing funcs
117 function clearAll(s) {
118 if (s.downloads === CONSTANTS.YES)
123 function runCleaner() {
125 'cookies', 'downloads', 'time', 'timeStart',
127 chrome.storage.sync.get(keys, function(s) {
128 if (s.cookies === CONSTANTS.YES) {
129 clearCookies(clearAll, s);
137 * Executes when the user clicks the browser action. Uses stored values from
138 * {@code localStorage}
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;
145 // The confirmation message to ask
146 var message = chrome.i18n.getMessage('confirmPrompt');
148 // Clear cookies, and then clear the history
149 if (showPrompt === CONSTANTS.YES) {
150 if (!confirm(message))
157 chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) {
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
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)