]>
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 | ||
c8fabbc8 MF |
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 | ||
a5409a6b MF |
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 | */ | |
c8fabbc8 | 31 | function clearCookies(callback, s) { |
a5409a6b MF |
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) { | |
c8fabbc8 | 39 | callback(s); |
a5409a6b MF |
40 | } else { |
41 | removeCookie(cookies[i], function() { | |
42 | doCookieCleanup(i + 1); | |
43 | }); | |
44 | } | |
45 | })(0); | |
46 | }); | |
47 | } | |
48 | ||
c8fabbc8 MF |
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 | ||
a5409a6b MF |
64 | /** |
65 | * Callback for when history deletion is successful | |
66 | */ | |
c8fabbc8 | 67 | function didClearHistory(s) { |
03eef92c | 68 | var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; |
a5409a6b | 69 | time = getUnitsForTime(time); |
016d6547 MF |
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]); | |
a5409a6b MF |
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 | */ | |
c8fabbc8 | 88 | function clearHistory(s) { |
a5409a6b MF |
89 | //Get the values from localStorage |
90 | // time is in hours | |
c8fabbc8 MF |
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(); | |
cc8be426 | 101 | } else { |
c8fabbc8 MF |
102 | var endTime = time; |
103 | time = 0; | |
cc8be426 | 104 | } |
c8fabbc8 MF |
105 | var range = { |
106 | startTime: startTime, | |
107 | endTime: endTime | |
108 | }; | |
109 | // Delete history in the range | |
110 | chrome.history.deleteRange(range, callback); | |
111 | } | |
a5409a6b MF |
112 | } |
113 | ||
c8fabbc8 MF |
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 | } | |
6565d921 | 122 | |
c8fabbc8 MF |
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); | |
03eef92c | 130 | } else { |
c8fabbc8 | 131 | clearAll(s); |
03eef92c MF |
132 | } |
133 | }); | |
6565d921 MF |
134 | } |
135 | ||
a5409a6b MF |
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) { | |
a5409a6b | 141 | // Get the value from localStorage |
03eef92c MF |
142 | chrome.storage.sync.get('prompt', function(s) { |
143 | var showPrompt = s.prompt || CONSTANTS.YES; | |
a5409a6b MF |
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) { | |
77dffdcd MF |
150 | if (!confirm(message)) |
151 | return; | |
152 | } | |
6565d921 | 153 | runCleaner(); |
03eef92c | 154 | }); |
6565d921 MF |
155 | }); |
156 | ||
defd9f79 MF |
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 | }); | |
6565d921 MF |
166 | }); |
167 | ||
168 | chrome.alarms.onAlarm.addListener(function(alarm) { | |
03eef92c MF |
169 | chrome.storage.sync.get('autoclear', function(s) { |
170 | var autoCleaner = s.autoclear || CONSTANTS.NO; | |
171 | if (autoCleaner === CONSTANTS.YES) | |
172 | runCleaner(); | |
173 | }); | |
a5409a6b | 174 | }); |