]>
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 function getClearDate(s
) {
9 var time
= ~~(s
.time
) || CONSTANTS
.DEFAULT_TIME
;
12 return (Date
.now() - (time
* 60 * 60 * 1000)); // time from hrs to ms
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.
20 function removeCookie(cookie
, callback
) {
21 var url
= 'http' + (cookie
.secure
? 's' : '') + '://' + cookie
.domain
+
23 chrome
.cookies
.remove({url
: url
, name
: cookie
.name
}, callback
);
28 * @param {Function=} callback The function to invoke when all cookies are
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
) {
41 removeCookie(cookies
[i
], function() {
42 doCookieCleanup(i
+ 1);
50 * Clear download history
52 function clearDownloads(s
) {
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
;
60 query
.startedBefore
= iso8601
;
61 chrome
.downloads
.erase(query
);
65 * Callback for when history deletion is successful
67 function didClearHistory(s
) {
68 if (s
.notify
!== CONSTANTS
.YES
)
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
);
80 title
: chrome
.i18n
.getMessage('notificationTitle'),
82 iconUrl
: chrome
.extension
.getURL('/images/icon128.png'),
85 var id
= 'clearhistory advance notify';
86 chrome
.notifications
.clear(id
, function(wasCleared
) {
87 chrome
.notifications
.create(id
, options
, function(){});
92 * Clears the history for the amount of time stored in {@code localStorage}
94 function clearHistory(s
) {
95 //Get the values from localStorage
97 var callback = function() { didClearHistory(s
); };
98 var time
= getClearDate(s
);
101 chrome
.history
.deleteAll(callback
);
104 var startTime
= time
;
105 if (s
.timeStart
=== CONSTANTS
.YES
) {
106 var endTime
= Date
.now();
112 startTime
: startTime
,
115 // Delete history in the range
116 chrome
.history
.deleteRange(range
, callback
);
121 * Run through all the clearing funcs
123 function clearAll(s
) {
124 if (s
.downloads
=== CONSTANTS
.YES
)
129 function runCleaner() {
131 'cookies', 'downloads', 'time', 'timeStart', 'notify',
133 chrome
.storage
.sync
.get(keys
, function(s
) {
134 if (s
.cookies
=== CONSTANTS
.YES
) {
135 clearCookies(clearAll
, s
);
143 * Executes when the user clicks the browser action. Uses stored values from
144 * {@code localStorage}
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
;
151 // The confirmation message to ask
152 var message
= chrome
.i18n
.getMessage('confirmPrompt');
154 // Clear cookies, and then clear the history
155 if (showPrompt
=== CONSTANTS
.YES
) {
156 if (!confirm(message
))
163 chrome
.alarms
.get(CONSTANTS
.CLEANER_ALARM
, function(a
) {
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
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
)