]>
git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blob - javascript/clearhistory.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview The main script for the Clear History extension for Chrome.
5 * @author arunjit@google.com (Arunjit Singh)
8 import {CONSTANTS
} from './background.js';
9 import {getUnitsForTime
} from './util.js';
11 function getClearDate(s
) {
12 var time
= ~~(s
.time
) || CONSTANTS
.DEFAULT_TIME
;
15 return (Date
.now() - (time
* 60 * 60 * 1000)); // time from hrs to ms
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.
23 function removeCookie(cookie
, callback
) {
24 var url
= 'http' + (cookie
.secure
? 's' : '') + '://' + cookie
.domain
+
26 chrome
.cookies
.remove({url
: url
, name
: cookie
.name
}, callback
);
31 * @param {Function=} callback The function to invoke when all cookies are
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
) {
44 removeCookie(cookies
[i
], function() {
45 doCookieCleanup(i
+ 1);
53 * Clear download history
55 function clearDownloads(s
) {
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
;
63 query
.startedBefore
= iso8601
;
64 chrome
.downloads
.erase(query
);
68 * Callback for when history deletion is successful
70 function didClearHistory(s
) {
71 if (s
.notify
!== CONSTANTS
.YES
)
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
);
83 title
: chrome
.i18n
.getMessage('notificationTitle'),
85 iconUrl
: chrome
.extension
.getURL('/images/icon128.png'),
88 var id
= 'clearhistory advance notify';
89 chrome
.notifications
.clear(id
, function(wasCleared
) {
90 chrome
.notifications
.create(id
, options
, function(){});
95 * Clears the history for the amount of time stored in {@code localStorage}
97 function clearHistory(s
) {
98 //Get the values from localStorage
100 var callback = function() { didClearHistory(s
); };
101 var time
= getClearDate(s
);
104 chrome
.history
.deleteAll(callback
);
107 var startTime
= time
;
108 if (s
.timeStart
=== CONSTANTS
.YES
) {
109 var endTime
= Date
.now();
115 startTime
: startTime
,
118 // Delete history in the range
119 chrome
.history
.deleteRange(range
, callback
);
124 * Run through all the clearing funcs
126 function clearAll(s
) {
127 if (s
.downloads
=== CONSTANTS
.YES
)
132 function runCleaner() {
134 'cookies', 'downloads', 'time', 'timeStart', 'notify',
136 chrome
.storage
.sync
.get(keys
, function(s
) {
137 if (s
.cookies
=== CONSTANTS
.YES
) {
138 clearCookies(clearAll
, s
);
146 * Callback from other pages (like the popup).
148 chrome
.runtime
.onMessage
.addListener(function(request
, sender
, sendResponse
) {
149 switch (request
?.action
) {
156 chrome
.alarms
.get(CONSTANTS
.CLEANER_ALARM
, function(a
) {
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
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
)