From c8fabbc84cc9550e8d8218a05fa38f4f36600bc5 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 21 Jan 2014 00:43:24 -0500 Subject: [PATCH] add support for clearing download history --- _locales/en/messages.json | 3 +- css/options.css | 1 + javascript/clearhistory.js | 95 ++++++++++++++++++++++++-------------- javascript/options.js | 5 +- manifest.json | 1 + views/options.html | 11 +++++ 6 files changed, 80 insertions(+), 36 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 52b1e3c..7ea4116 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -16,7 +16,8 @@ }, "optionsTimeAll": {"message": "Everything"}, "optionsPrompt": {"message": "Prompt before deleting"}, - "optionsCookies": {"message": "Clear cookies too"}, + "optionsCookies": {"message": "Clear all cookies"}, + "optionsDownloads": {"message": "Clear download history"}, "optionsSaved": {"message": "Preferences saved!"}, "optionsAutoclear": {"message": "Automatically purge old entries"}, "confirmPrompt": {"message": "Are you sure you want to delete the history?"}, diff --git a/css/options.css b/css/options.css index 6e8cf79..95dee56 100644 --- a/css/options.css +++ b/css/options.css @@ -52,6 +52,7 @@ hr { #opt-time, #opt-prompt, #opt-cookies, +#opt-downloads, #opt-autoclear { background-color: white; clear: left; diff --git a/javascript/clearhistory.js b/javascript/clearhistory.js index 73206ce..f614fff 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,11 +46,25 @@ 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() { - chrome.storage.sync.get('time', function(s) { +function didClearHistory(s) { var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; time = getUnitsForTime(time); var timeString = (time.time === -1) ? @@ -59,49 +80,55 @@ function didClearHistory() { setTimeout(function() { notification.cancel(); }, 5000); - }); } /** * 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 - chrome.storage.sync.get(['time', 'timeStart'], function(s) { - var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; - if (time === -1) { - // Delete everything - chrome.history.deleteAll(didClearHistory); + var callback = function() { didClearHistory(s); }; + var time = getClearDate(s); + if (time === -1) { + // Delete everything + chrome.history.deleteAll(callback); + } else { + // Create the range + var startTime = time; + if (s.timeStart === CONSTANTS.YES) { + var endTime = Date.now(); } else { - // Create the range - var timeStart = s.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; - } else { - var endTime = startTime; - startTime = 0; - } - var range = { - startTime: startTime, - endTime: endTime - }; - // Delete history in the range - chrome.history.deleteRange(range, didClearHistory); + var endTime = time; + time = 0; } - }); + var range = { + startTime: startTime, + endTime: endTime + }; + // Delete history in the range + chrome.history.deleteRange(range, callback); + } } -function runCleaner() { - chrome.storage.sync.get('cookies', function(s) { - var clearCookies = s.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); +function runCleaner() { + var keys = [ + 'cookies', 'downloads', 'time', 'timeStart', + ]; + chrome.storage.sync.get(keys, function(s) { + if (s.cookies === CONSTANTS.YES) { + clearCookies(clearAll, s); } else { - clearHistory(); + clearAll(s); } }); } diff --git a/javascript/options.js b/javascript/options.js index 94ea31e..f40b46c 100644 --- a/javascript/options.js +++ b/javascript/options.js @@ -32,6 +32,7 @@ function init() { $('#optionsTimeFor')[0].innerText = chrome.i18n.getMessage('optionsTimeFor'); $('#optionsTimeStart')[0].innerText = chrome.i18n.getMessage('optionsTimeStart'); $('#optionsCookies')[0].innerText = chrome.i18n.getMessage('optionsCookies'); + $('#optionsDownloads')[0].innerText = chrome.i18n.getMessage('optionsDownloads'); $('#optionsAutoclear')[0].innerText = chrome.i18n.getMessage('optionsAutoclear'); $('#optionsSaved > b')[0].innerText = chrome.i18n.getMessage('optionsSaved'); @@ -45,19 +46,21 @@ function init() { // Load or set localStorage data var settings = [ - 'timeStart', 'time', 'prompt', 'cookies', 'autoclear', + 'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear', ]; chrome.storage.sync.get(settings, function(s) { var timeStart = s.timeStart || CONSTANTS.YES; var time = ~~(s.time) || (s.time = CONSTANTS.DEFAULT_TIME); var showPrompt = s.prompt || (s.prompt = CONSTANTS.YES); var clearCookies = s.cookies || CONSTANTS.NO; + var clearDownloads = s.downloads || CONSTANTS.NO; var autoClear = s.autoclear || CONSTANTS.NO; $('input[name=timeStart]')[0].checked = (timeStart === CONSTANTS.YES); $('input[name=time][value="' + time + '"]')[0].checked = true; $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES); $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES); + $('input[name=downloads]')[0].checked = (clearDownloads === CONSTANTS.YES); $('input[name=autoclear]')[0].checked = (autoClear === CONSTANTS.YES); }); } diff --git a/manifest.json b/manifest.json index 266ee17..9196f47 100644 --- a/manifest.json +++ b/manifest.json @@ -9,6 +9,7 @@ "tabs", "history", "cookies", + "downloads", "notifications", "storage", "http://*/*", diff --git a/views/options.html b/views/options.html index 97b42fd..ca3ca6f 100644 --- a/views/options.html +++ b/views/options.html @@ -92,6 +92,17 @@ +
+
+   +
+
+
+ +
+
+
+

-- 2.39.5