X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=javascript%2Fclearhistory.js;h=b40f5958a187ef542f93230b1dd081a55a49f4df;hb=614dea955c957d58c79da346fa36fec176c59728;hp=7c9596f943ec287b28890ce8dd026176be9cb587;hpb=6565d921d8384840180e5f61c09a6b51c2e2bc54;p=chrome-ext%2Fclearhistory-advance-fork.git diff --git a/javascript/clearhistory.js b/javascript/clearhistory.js index 7c9596f..b40f595 100644 --- a/javascript/clearhistory.js +++ b/javascript/clearhistory.js @@ -5,6 +5,13 @@ * @author arunjit@google.com (Arunjit Singh) */ +function getClearDate(s) { + var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; + if (time == -1) + return time; + return (Date.now() - (time * 60 * 60 * 1000)); // time from hrs to ms +} + /** * Removes a single cookie * @param {Object.} cookie The cookie to delete. @@ -21,7 +28,7 @@ function removeCookie(cookie, callback) { * @param {Function=} callback The function to invoke when all cookies are * deleted (optional). */ -function clearCookies(callback) { +function clearCookies(callback, s) { callback = callback || function() {}; chrome.cookies.getAll({}, function(cookies) { var len = cookies.length; @@ -29,7 +36,7 @@ function clearCookies(callback) { // cookie deletion functions have called back (finished). (function doCookieCleanup(i) { if (i === len) { - callback(); + callback(s); } else { removeCookie(cookies[i], function() { doCookieCleanup(i + 1); @@ -39,65 +46,97 @@ function clearCookies(callback) { }); } +/** + * Clear download history + */ +function clearDownloads(s) { + var query = {}; + var date = new Date(getClearDate(s)); + // chrome.downloads expects ISO 8601 + var iso8601 = date.toISOString(); + if (s.timeStart === CONSTANTS.YES) + query.startedAfter = iso8601; + else + query.startedBefore = iso8601; + chrome.downloads.erase(query); +} + /** * Callback for when history deletion is successful */ -function didClearHistory() { - var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME; +function didClearHistory(s) { + if (s.notify !== CONSTANTS.YES) + return; + + var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; time = getUnitsForTime(time); - var timeString = (time.time === -1) ? - chrome.i18n.getMessage('notificationTimeAll') : - chrome.i18n.getMessage('notificationTime', - [time.time, time.units]); + var timeMsg = (time.time === -1) ? 'notificationTimeAll' : + (s.timeStart === CONSTANTS.YES) ? 'notificationTimeNewer' : + 'notificationTimeOlder'; + var timeString = chrome.i18n.getMessage(timeMsg, [time.time, time.units]); var message = chrome.i18n.getMessage('notificationBody', timeString); - var notification = webkitNotifications.createNotification( - chrome.extension.getURL('/images/icon48.png'), - chrome.i18n.getMessage('notificationTitle'), - message); - notification.show(); - setTimeout(function() { - notification.cancel(); - }, 5000); + var options = { + type: 'basic', + title: chrome.i18n.getMessage('notificationTitle'), + message: message, + iconUrl: chrome.extension.getURL('/images/icon128.png'), + }; + + var id = 'clearhistory advance notify'; + chrome.notifications.clear(id, function(wasCleared) { + chrome.notifications.create(id, options, function(){}); + }); } /** * Clears the history for the amount of time stored in {@code localStorage} */ -function clearHistory() { +function clearHistory(s) { //Get the values from localStorage // time is in hours - var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME; + var callback = function() { didClearHistory(s); }; + var time = getClearDate(s); if (time === -1) { // Delete everything - chrome.history.deleteAll(didClearHistory); + chrome.history.deleteAll(callback); } else { // Create the range - var timeStart = localStorage['timeStart'] === CONSTANTS.YES; - var now = (new Date).getTime(); - var startTime = (now - time * 60 * 60 * 1000); // time from hrs to ms - if (timeStart) { - var endTime = now; + var startTime = time; + if (s.timeStart === CONSTANTS.YES) { + var endTime = Date.now(); } else { - var endTime = startTime; - startTime = 0; + var endTime = time; + time = 0; } var range = { startTime: startTime, endTime: endTime }; // Delete history in the range - chrome.history.deleteRange(range, didClearHistory); + chrome.history.deleteRange(range, callback); } } -function runCleaner() { - var clearCookies = localStorage['cookies'] || CONSTANTS.NO; +/** + * Run through all the clearing funcs + */ +function clearAll(s) { + if (s.downloads === CONSTANTS.YES) + clearDownloads(s); + clearHistory(s); +} - if (clearCookies === CONSTANTS.YES) { - clearCookies(clearHistory); - } else { - clearHistory(); - } +function runCleaner() { + var keys = [ + 'cookies', 'downloads', 'time', 'timeStart', 'notify', + ]; + chrome.storage.sync.get(keys, function(s) { + if (s.cookies === CONSTANTS.YES) { + clearCookies(clearAll, s); + } else { + clearAll(s); + } + }); } /** @@ -106,7 +145,8 @@ function runCleaner() { */ chrome.browserAction.onClicked.addListener(function(tab) { // Get the value from localStorage - var showPrompt = localStorage['prompt'] || CONSTANTS.YES; + chrome.storage.sync.get('prompt', function(s) { + var showPrompt = s.prompt || CONSTANTS.YES; // The confirmation message to ask var message = chrome.i18n.getMessage('confirmPrompt'); @@ -117,17 +157,24 @@ chrome.browserAction.onClicked.addListener(function(tab) { return; } runCleaner(); + }); }); -chrome.alarms.create(CONSTANTS.CLEANER_ALARM, { - // First fire 10 minutes from now. - 'when': Date.now() + (10 * 60 * 1000), - 'periodInMinutes': 24 * 60 +chrome.alarms.get(CONSTANTS.CLEANER_ALARM, function(a) { + if (a) + return; + + chrome.alarms.create(CONSTANTS.CLEANER_ALARM, { + // First fire 10 minutes from now. + 'when': Date.now() + (10 * 60 * 1000), + 'periodInMinutes': 24 * 60 + }); }); chrome.alarms.onAlarm.addListener(function(alarm) { - var autoCleaner = localStorage['autoclear'] || CONSTANTS.NO; - if (autoCleaner === CONSTANTS.YES) - runCleaner(); - console.log('running'); + chrome.storage.sync.get('autoclear', function(s) { + var autoCleaner = s.autoclear || CONSTANTS.NO; + if (autoCleaner === CONSTANTS.YES) + runCleaner(); + }); });