]> git.wh0rd.org Git - 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 import {CONSTANTS} from './background.js';
9 import {getUnitsForTime} from './util.js';
10
11 function getClearDate(s) {
12   var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
13   if (time == -1)
14     return time;
15   return (Date.now() - (time * 60 * 60 * 1000));  // time from hrs to ms
16 }
17
18 /**
19  * Removes a single cookie
20  * @param {Object.<Cookie>} cookie The cookie to delete.
21  * @param {Function} callback The function to invoke when the cookie is deleted.
22  */
23 function removeCookie(cookie, callback) {
24   var url = 'http' + (cookie.secure ? 's' : '') + '://' + cookie.domain +
25             cookie.path;
26   chrome.cookies.remove({url: url, name: cookie.name}, callback);
27 }
28
29 /**
30  * Removes all cookies
31  * @param {Function=} callback The function to invoke when all cookies are
32  *     deleted (optional).
33  */
34 function clearCookies(callback, s) {
35   callback = callback || function() {};
36   chrome.cookies.getAll({}, function(cookies) {
37     var len = cookies.length;
38     // Synchronize the cookie deletion and execute the callback only when all
39     // cookie deletion functions have called back (finished).
40     (function doCookieCleanup(i) {
41       if (i === len) {
42         callback(s);
43       } else {
44         removeCookie(cookies[i], function() {
45           doCookieCleanup(i + 1);
46         });
47       }
48     })(0);
49   });
50 }
51
52 /**
53  * Clear download history
54  */
55 function clearDownloads(s) {
56   var query = {};
57   var date = new Date(getClearDate(s));
58   // chrome.downloads expects ISO 8601
59   var iso8601 = date.toISOString();
60   if (s.timeStart === CONSTANTS.YES)
61     query.startedAfter = iso8601;
62   else
63     query.startedBefore = iso8601;
64   chrome.downloads.erase(query);
65 }
66
67 /**
68  * Callback for when history deletion is successful
69  */
70 function didClearHistory(s) {
71   if (s.notify !== CONSTANTS.YES)
72     return;
73
74   var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
75   time = getUnitsForTime(time);
76   var timeMsg = (time.time === -1) ? 'notificationTimeAll' :
77                 (s.timeStart === CONSTANTS.YES) ? 'notificationTimeNewer' :
78                 'notificationTimeOlder';
79   var timeString = chrome.i18n.getMessage(timeMsg, [time.time, time.units]);
80   var message = chrome.i18n.getMessage('notificationBody', timeString);
81   var options = {
82     type: 'basic',
83     title: chrome.i18n.getMessage('notificationTitle'),
84     message: message,
85     iconUrl: chrome.extension.getURL('/images/icon128.png'),
86   };
87
88   var id = 'clearhistory advance notify';
89   chrome.notifications.clear(id, function(wasCleared) {
90     chrome.notifications.create(id, options, function(){});
91   });
92 }
93
94 /**
95  * Clears the history for the amount of time stored in {@code localStorage}
96  */
97 function clearHistory(s) {
98   //Get the values from localStorage
99   // time is in hours
100   var callback = function() { didClearHistory(s); };
101   var time = getClearDate(s);
102   if (time === -1) {
103     // Delete everything
104     chrome.history.deleteAll(callback);
105   } else {
106     // Create the range
107     var startTime = time;
108     if (s.timeStart === CONSTANTS.YES) {
109       var endTime = Date.now();
110     } else {
111       var endTime = time;
112       startTime = 0;
113     }
114     var range = {
115       startTime: startTime,
116       endTime: endTime
117     };
118     // Delete history in the range
119     chrome.history.deleteRange(range, callback);
120   }
121 }
122
123 /**
124  * Run through all the clearing funcs
125  */
126 function clearAll(s) {
127   if (s.downloads === CONSTANTS.YES)
128     clearDownloads(s);
129   clearHistory(s);
130 }
131
132 function runCleaner() {
133   var keys = [
134     'cookies', 'downloads', 'time', 'timeStart', 'notify',
135   ];
136   chrome.storage.sync.get(keys, function(s) {
137     if (s.cookies === CONSTANTS.YES) {
138       clearCookies(clearAll, s);
139     } else {
140       clearAll(s);
141     }
142   });
143 }
144
145 /**
146  * Callback from other pages (like the popup).
147  */
148 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
149   switch (request?.action) {
150     case 'runCleaner':
151       runCleaner();
152       break;
153   }
154 });
155
156 chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) {
157   if (a)
158     return;
159
160   chrome.alarms.create(CONSTANTS.CLEANER_ALARM, {
161     // First fire 10 minutes from now.
162     'when': Date.now() + (10 * 60 * 1000),
163     'periodInMinutes': 24 * 60
164   });
165 });
166
167 chrome.alarms.onAlarm.addListener(function(alarm) {
168   chrome.storage.sync.get('autoclear', function(s) {
169     var autoCleaner = s.autoclear || CONSTANTS.NO;
170     if (autoCleaner === CONSTANTS.YES)
171       runCleaner();
172   });
173 });