]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blame - javascript/clearhistory.js
convert to MV3
[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
c930a5d6
MF
8import {CONSTANTS} from './background.js';
9import {getUnitsForTime} from './util.js';
10
c8fabbc8
MF
11function 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
a5409a6b
MF
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 */
23function 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 */
c8fabbc8 34function clearCookies(callback, s) {
a5409a6b
MF
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) {
c8fabbc8 42 callback(s);
a5409a6b
MF
43 } else {
44 removeCookie(cookies[i], function() {
45 doCookieCleanup(i + 1);
46 });
47 }
48 })(0);
49 });
50}
51
c8fabbc8
MF
52/**
53 * Clear download history
54 */
55function 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
a5409a6b
MF
67/**
68 * Callback for when history deletion is successful
69 */
c8fabbc8 70function didClearHistory(s) {
614dea95
MF
71 if (s.notify !== CONSTANTS.YES)
72 return;
73
03eef92c 74 var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME;
a5409a6b 75 time = getUnitsForTime(time);
016d6547
MF
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]);
a5409a6b 80 var message = chrome.i18n.getMessage('notificationBody', timeString);
614dea95
MF
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 });
a5409a6b
MF
92}
93
94/**
95 * Clears the history for the amount of time stored in {@code localStorage}
96 */
c8fabbc8 97function clearHistory(s) {
a5409a6b
MF
98 //Get the values from localStorage
99 // time is in hours
c8fabbc8
MF
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();
cc8be426 110 } else {
c8fabbc8 111 var endTime = time;
9b4e9061 112 startTime = 0;
cc8be426 113 }
c8fabbc8
MF
114 var range = {
115 startTime: startTime,
116 endTime: endTime
117 };
118 // Delete history in the range
119 chrome.history.deleteRange(range, callback);
120 }
a5409a6b
MF
121}
122
c8fabbc8
MF
123/**
124 * Run through all the clearing funcs
125 */
126function clearAll(s) {
127 if (s.downloads === CONSTANTS.YES)
128 clearDownloads(s);
129 clearHistory(s);
130}
6565d921 131
c8fabbc8
MF
132function runCleaner() {
133 var keys = [
614dea95 134 'cookies', 'downloads', 'time', 'timeStart', 'notify',
c8fabbc8
MF
135 ];
136 chrome.storage.sync.get(keys, function(s) {
137 if (s.cookies === CONSTANTS.YES) {
138 clearCookies(clearAll, s);
03eef92c 139 } else {
c8fabbc8 140 clearAll(s);
03eef92c
MF
141 }
142 });
6565d921
MF
143}
144
a5409a6b 145/**
a3c71eeb 146 * Callback from other pages (like the popup).
a5409a6b 147 */
a3c71eeb
MF
148chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
149 switch (request?.action) {
150 case 'runCleaner':
151 runCleaner();
152 break;
77dffdcd 153 }
6565d921
MF
154});
155
defd9f79
MF
156chrome.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 });
6565d921
MF
165});
166
167chrome.alarms.onAlarm.addListener(function(alarm) {
03eef92c
MF
168 chrome.storage.sync.get('autoclear', function(s) {
169 var autoCleaner = s.autoclear || CONSTANTS.NO;
170 if (autoCleaner === CONSTANTS.YES)
171 runCleaner();
172 });
a5409a6b 173});