]>
Commit | Line | Data |
---|---|---|
a5409a6b MF |
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 | /** | |
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. | |
12 | */ | |
13 | function removeCookie(cookie, callback) { | |
14 | var url = 'http' + (cookie.secure ? 's' : '') + '://' + cookie.domain + | |
15 | cookie.path; | |
16 | chrome.cookies.remove({url: url, name: cookie.name}, callback); | |
17 | } | |
18 | ||
19 | /** | |
20 | * Removes all cookies | |
21 | * @param {Function=} callback The function to invoke when all cookies are | |
22 | * deleted (optional). | |
23 | */ | |
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) { | |
31 | if (i === len) { | |
32 | callback(); | |
33 | } else { | |
34 | removeCookie(cookies[i], function() { | |
35 | doCookieCleanup(i + 1); | |
36 | }); | |
37 | } | |
38 | })(0); | |
39 | }); | |
40 | } | |
41 | ||
42 | /** | |
43 | * Callback for when history deletion is successful | |
44 | */ | |
45 | function didClearHistory() { | |
03eef92c MF |
46 | chrome.storage.sync.get('time', function(s) { |
47 | var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; | |
a5409a6b MF |
48 | time = getUnitsForTime(time); |
49 | var timeString = (time.time === -1) ? | |
50 | chrome.i18n.getMessage('notificationTimeAll') : | |
51 | chrome.i18n.getMessage('notificationTime', | |
52 | [time.time, time.units]); | |
53 | var message = chrome.i18n.getMessage('notificationBody', timeString); | |
54 | var notification = webkitNotifications.createNotification( | |
55 | chrome.extension.getURL('/images/icon48.png'), | |
56 | chrome.i18n.getMessage('notificationTitle'), | |
57 | message); | |
58 | notification.show(); | |
59 | setTimeout(function() { | |
60 | notification.cancel(); | |
61 | }, 5000); | |
03eef92c | 62 | }); |
a5409a6b MF |
63 | } |
64 | ||
65 | /** | |
66 | * Clears the history for the amount of time stored in {@code localStorage} | |
67 | */ | |
68 | function clearHistory() { | |
69 | //Get the values from localStorage | |
70 | // time is in hours | |
03eef92c MF |
71 | chrome.storage.sync.get(['time', 'timeStart'], function(s) { |
72 | var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; | |
73 | if (time === -1) { | |
74 | // Delete everything | |
75 | chrome.history.deleteAll(didClearHistory); | |
cc8be426 | 76 | } else { |
03eef92c MF |
77 | // Create the range |
78 | var timeStart = s.timeStart === CONSTANTS.YES; | |
79 | var now = (new Date).getTime(); | |
80 | var startTime = (now - time * 60 * 60 * 1000); // time from hrs to ms | |
81 | if (timeStart) { | |
82 | var endTime = now; | |
83 | } else { | |
84 | var endTime = startTime; | |
85 | startTime = 0; | |
86 | } | |
87 | var range = { | |
88 | startTime: startTime, | |
89 | endTime: endTime | |
90 | }; | |
91 | // Delete history in the range | |
92 | chrome.history.deleteRange(range, didClearHistory); | |
cc8be426 | 93 | } |
03eef92c | 94 | }); |
a5409a6b MF |
95 | } |
96 | ||
6565d921 | 97 | function runCleaner() { |
03eef92c MF |
98 | chrome.storage.sync.get('cookies', function(s) { |
99 | var clearCookies = s.cookies || CONSTANTS.NO; | |
6565d921 | 100 | |
03eef92c MF |
101 | if (clearCookies === CONSTANTS.YES) { |
102 | clearCookies(clearHistory); | |
103 | } else { | |
104 | clearHistory(); | |
105 | } | |
106 | }); | |
6565d921 MF |
107 | } |
108 | ||
a5409a6b MF |
109 | /** |
110 | * Executes when the user clicks the browser action. Uses stored values from | |
111 | * {@code localStorage} | |
112 | */ | |
113 | chrome.browserAction.onClicked.addListener(function(tab) { | |
a5409a6b | 114 | // Get the value from localStorage |
03eef92c MF |
115 | chrome.storage.sync.get('prompt', function(s) { |
116 | var showPrompt = s.prompt || CONSTANTS.YES; | |
a5409a6b MF |
117 | |
118 | // The confirmation message to ask | |
119 | var message = chrome.i18n.getMessage('confirmPrompt'); | |
120 | ||
121 | // Clear cookies, and then clear the history | |
122 | if (showPrompt === CONSTANTS.YES) { | |
77dffdcd MF |
123 | if (!confirm(message)) |
124 | return; | |
125 | } | |
6565d921 | 126 | runCleaner(); |
03eef92c | 127 | }); |
6565d921 MF |
128 | }); |
129 | ||
defd9f79 MF |
130 | chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) { |
131 | if (a) | |
132 | return; | |
133 | ||
134 | chrome.alarms.create(CONSTANTS.CLEANER_ALARM, { | |
135 | // First fire 10 minutes from now. | |
136 | 'when': Date.now() + (10 * 60 * 1000), | |
137 | 'periodInMinutes': 24 * 60 | |
138 | }); | |
6565d921 MF |
139 | }); |
140 | ||
141 | chrome.alarms.onAlarm.addListener(function(alarm) { | |
03eef92c MF |
142 | chrome.storage.sync.get('autoclear', function(s) { |
143 | var autoCleaner = s.autoclear || CONSTANTS.NO; | |
144 | if (autoCleaner === CONSTANTS.YES) | |
145 | runCleaner(); | |
146 | }); | |
a5409a6b | 147 | }); |