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)
9 * Removes a single cookie
10 * @param {Object.<Cookie>} cookie The cookie to delete.
11 * @param {Function} callback The function to invoke when the cookie is deleted.
13 function removeCookie(cookie, callback) {
14 var url = 'http' + (cookie.secure ? 's' : '') + '://' + cookie.domain +
16 chrome.cookies.remove({url: url, name: cookie.name}, callback);
21 * @param {Function=} callback The function to invoke when all cookies are
24 function clearCookies(callback) {
25 callback = callback || function() {};
26 chrome.cookies.getAll({}, function(cookies) {
27 var len = cookies.length;
28 // Synchronize the cookie deletion and execute the callback only when all
29 // cookie deletion functions have called back (finished).
30 (function doCookieCleanup(i) {
34 removeCookie(cookies[i], function() {
35 doCookieCleanup(i + 1);
43 * Callback for when history deletion is successful
45 function didClearHistory() {
46 var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME;
47 time = getUnitsForTime(time);
48 var timeString = (time.time === -1) ?
49 chrome.i18n.getMessage('notificationTimeAll') :
50 chrome.i18n.getMessage('notificationTime',
51 [time.time, time.units]);
52 var message = chrome.i18n.getMessage('notificationBody', timeString);
53 var notification = webkitNotifications.createNotification(
54 chrome.extension.getURL('/images/icon48.png'),
55 chrome.i18n.getMessage('notificationTitle'),
58 setTimeout(function() {
59 notification.cancel();
64 * Clears the history for the amount of time stored in {@code localStorage}
66 function clearHistory() {
67 //Get the values from localStorage
69 var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME;
72 chrome.history.deleteAll(didClearHistory);
75 var timeStart = localStorage['timeStart'] === CONSTANTS.YES;
76 var now = (new Date).getTime();
77 var startTime = (now - time * 60 * 60 * 1000); // time from hrs to ms
81 var endTime = startTime;
88 // Delete history in the range
89 chrome.history.deleteRange(range, didClearHistory);
93 function runCleaner() {
94 var clearCookies = localStorage['cookies'] || CONSTANTS.NO;
96 if (clearCookies === CONSTANTS.YES) {
97 clearCookies(clearHistory);
104 * Executes when the user clicks the browser action. Uses stored values from
105 * {@code localStorage}
107 chrome.browserAction.onClicked.addListener(function(tab) {
108 // Get the value from localStorage
109 var showPrompt = localStorage['prompt'] || CONSTANTS.YES;
111 // The confirmation message to ask
112 var message = chrome.i18n.getMessage('confirmPrompt');
114 // Clear cookies, and then clear the history
115 if (showPrompt === CONSTANTS.YES) {
116 if (!confirm(message))
122 chrome.alarms.create(CONSTANTS.CLEANER_ALARM, {
123 // First fire 10 minutes from now.
124 'when': Date.now() + (10 * 60 * 1000),
125 'periodInMinutes': 24 * 60
128 chrome.alarms.onAlarm.addListener(function(alarm) {
129 var autoCleaner = localStorage['autoclear'] || CONSTANTS.NO;
130 if (autoCleaner === CONSTANTS.YES)
132 console.log('running');