]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blame - javascript/clearhistory.js
add an option to run silently -- no notifications at all
[chrome-ext/clearhistory-advance-fork.git] / javascript / clearhistory.js
CommitLineData
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
8function 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 */
20function 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 31function 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 */
52function 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 67function didClearHistory(s) {
614dea95
MF
68 if (s.notify !== CONSTANTS.YES)
69 return;
70
03eef92c 71 var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
a5409a6b 72 time = getUnitsForTime(time);
016d6547
MF
73 var timeMsg = (time.time === -1) ? 'notificationTimeAll' :
74 (s.timeStart === CONSTANTS.YES) ? 'notificationTimeNewer' :
75 'notificationTimeOlder';
76 var timeString = chrome.i18n.getMessage(timeMsg, [time.time, time.units]);
a5409a6b 77 var message = chrome.i18n.getMessage('notificationBody', timeString);
614dea95
MF
78 var options = {
79 type: 'basic',
80 title: chrome.i18n.getMessage('notificationTitle'),
81 message: message,
82 iconUrl: chrome.extension.getURL('/images/icon128.png'),
83 };
84
85 var id = 'clearhistory advance notify';
86 chrome.notifications.clear(id, function(wasCleared) {
87 chrome.notifications.create(id, options, function(){});
88 });
a5409a6b
MF
89}
90
91/**
92 * Clears the history for the amount of time stored in {@code localStorage}
93 */
c8fabbc8 94function clearHistory(s) {
a5409a6b
MF
95 //Get the values from localStorage
96 // time is in hours
c8fabbc8
MF
97 var callback = function() { didClearHistory(s); };
98 var time = getClearDate(s);
99 if (time === -1) {
100 // Delete everything
101 chrome.history.deleteAll(callback);
102 } else {
103 // Create the range
104 var startTime = time;
105 if (s.timeStart === CONSTANTS.YES) {
106 var endTime = Date.now();
cc8be426 107 } else {
c8fabbc8
MF
108 var endTime = time;
109 time = 0;
cc8be426 110 }
c8fabbc8
MF
111 var range = {
112 startTime: startTime,
113 endTime: endTime
114 };
115 // Delete history in the range
116 chrome.history.deleteRange(range, callback);
117 }
a5409a6b
MF
118}
119
c8fabbc8
MF
120/**
121 * Run through all the clearing funcs
122 */
123function clearAll(s) {
124 if (s.downloads === CONSTANTS.YES)
125 clearDownloads(s);
126 clearHistory(s);
127}
6565d921 128
c8fabbc8
MF
129function runCleaner() {
130 var keys = [
614dea95 131 'cookies', 'downloads', 'time', 'timeStart', 'notify',
c8fabbc8
MF
132 ];
133 chrome.storage.sync.get(keys, function(s) {
134 if (s.cookies === CONSTANTS.YES) {
135 clearCookies(clearAll, s);
03eef92c 136 } else {
c8fabbc8 137 clearAll(s);
03eef92c
MF
138 }
139 });
6565d921
MF
140}
141
a5409a6b
MF
142/**
143 * Executes when the user clicks the browser action. Uses stored values from
144 * {@code localStorage}
145 */
146chrome.browserAction.onClicked.addListener(function(tab) {
a5409a6b 147 // Get the value from localStorage
03eef92c
MF
148 chrome.storage.sync.get('prompt', function(s) {
149 var showPrompt = s.prompt || CONSTANTS.YES;
a5409a6b
MF
150
151 // The confirmation message to ask
152 var message = chrome.i18n.getMessage('confirmPrompt');
153
154 // Clear cookies, and then clear the history
155 if (showPrompt === CONSTANTS.YES) {
77dffdcd
MF
156 if (!confirm(message))
157 return;
158 }
6565d921 159 runCleaner();
03eef92c 160 });
6565d921
MF
161});
162
defd9f79
MF
163chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) {
164 if (a)
165 return;
166
167 chrome.alarms.create(CONSTANTS.CLEANER_ALARM, {
168 // First fire 10 minutes from now.
169 'when': Date.now() + (10 * 60 * 1000),
170 'periodInMinutes': 24 * 60
171 });
6565d921
MF
172});
173
174chrome.alarms.onAlarm.addListener(function(alarm) {
03eef92c
MF
175 chrome.storage.sync.get('autoclear', function(s) {
176 var autoCleaner = s.autoclear || CONSTANTS.NO;
177 if (autoCleaner === CONSTANTS.YES)
178 runCleaner();
179 });
a5409a6b 180});