]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blob - javascript/clearhistory.js
convert to MV3
[chrome-ext/clearhistory-advance-fork.git] / javascript / clearhistory.js
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 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
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 */
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) {
38 if (i === len) {
39 callback(s);
40 } else {
41 removeCookie(cookies[i], function() {
42 doCookieCleanup(i + 1);
43 });
44 }
45 })(0);
46 });
47 }
48
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
64 /**
65 * Callback for when history deletion is successful
66 */
67 function didClearHistory(s) {
68 if (s.notify !== CONSTANTS.YES)
69 return;
70
71 var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
72 time = getUnitsForTime(time);
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]);
77 var message = chrome.i18n.getMessage('notificationBody', timeString);
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 });
89 }
90
91 /**
92 * Clears the history for the amount of time stored in {@code localStorage}
93 */
94 function clearHistory(s) {
95 //Get the values from localStorage
96 // time is in hours
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();
107 } else {
108 var endTime = time;
109 startTime = 0;
110 }
111 var range = {
112 startTime: startTime,
113 endTime: endTime
114 };
115 // Delete history in the range
116 chrome.history.deleteRange(range, callback);
117 }
118 }
119
120 /**
121 * Run through all the clearing funcs
122 */
123 function clearAll(s) {
124 if (s.downloads === CONSTANTS.YES)
125 clearDownloads(s);
126 clearHistory(s);
127 }
128
129 function runCleaner() {
130 var keys = [
131 'cookies', 'downloads', 'time', 'timeStart', 'notify',
132 ];
133 chrome.storage.sync.get(keys, function(s) {
134 if (s.cookies === CONSTANTS.YES) {
135 clearCookies(clearAll, s);
136 } else {
137 clearAll(s);
138 }
139 });
140 }
141
142 /**
143 * Executes when the user clicks the browser action. Uses stored values from
144 * {@code localStorage}
145 */
146 chrome.browserAction.onClicked.addListener(function(tab) {
147 // Get the value from localStorage
148 chrome.storage.sync.get('prompt', function(s) {
149 var showPrompt = s.prompt || CONSTANTS.YES;
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) {
156 if (!confirm(message))
157 return;
158 }
159 runCleaner();
160 });
161 });
162
163 chrome.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 });
172 });
173
174 chrome.alarms.onAlarm.addListener(function(alarm) {
175 chrome.storage.sync.get('autoclear', function(s) {
176 var autoCleaner = s.autoclear || CONSTANTS.NO;
177 if (autoCleaner === CONSTANTS.YES)
178 runCleaner();
179 });
180 });