X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=javascript%2Fclearhistory.js;h=73206ce175b6d6b37d031c4b1cbae9cbed3eeca7;hb=03eef92c55cf1da39e0151aaeb5aa756c243246f;hp=b2d27149114ce6e33e9c1812334a3691c43bd94c;hpb=cc8be4269068a6dac17a2b163308152a0f6a3938;p=chrome-ext%2Fclearhistory-advance-fork.git diff --git a/javascript/clearhistory.js b/javascript/clearhistory.js index b2d2714..73206ce 100644 --- a/javascript/clearhistory.js +++ b/javascript/clearhistory.js @@ -43,7 +43,8 @@ function clearCookies(callback) { * Callback for when history deletion is successful */ function didClearHistory() { - var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME; + chrome.storage.sync.get('time', function(s) { + var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; time = getUnitsForTime(time); var timeString = (time.time === -1) ? chrome.i18n.getMessage('notificationTimeAll') : @@ -58,6 +59,7 @@ function didClearHistory() { setTimeout(function() { notification.cancel(); }, 5000); + }); } /** @@ -66,28 +68,42 @@ function didClearHistory() { function clearHistory() { //Get the values from localStorage // time is in hours - var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME; - if (time === -1) { - // Delete everything - chrome.history.deleteAll(didClearHistory); - } 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; + chrome.storage.sync.get(['time', 'timeStart'], function(s) { + var time = ~~(s.time) || CONSTANTS.DEFAULT_TIME; + if (time === -1) { + // Delete everything + chrome.history.deleteAll(didClearHistory); } else { - var endTime = startTime; - startTime = 0; + // 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 range = { - startTime: startTime, - endTime: endTime - }; - // Delete history in the range - chrome.history.deleteRange(range, didClearHistory); - } + }); +} + +function runCleaner() { + chrome.storage.sync.get('cookies', function(s) { + var clearCookies = s.cookies || CONSTANTS.NO; + + if (clearCookies === CONSTANTS.YES) { + clearCookies(clearHistory); + } else { + clearHistory(); + } + }); } /** @@ -96,8 +112,8 @@ function clearHistory() { */ chrome.browserAction.onClicked.addListener(function(tab) { // Get the value from localStorage - var showPrompt = localStorage['prompt'] || CONSTANTS.YES; - var clearCookies = localStorage['cookies'] || CONSTANTS.NO; + chrome.storage.sync.get('prompt', function(s) { + var showPrompt = s.prompt || CONSTANTS.YES; // The confirmation message to ask var message = chrome.i18n.getMessage('confirmPrompt'); @@ -107,9 +123,25 @@ chrome.browserAction.onClicked.addListener(function(tab) { if (!confirm(message)) return; } - if (clearCookies === CONSTANTS.YES) { - clearCookies(clearHistory); - } else { - clearHistory(); - } + runCleaner(); + }); +}); + +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) { + chrome.storage.sync.get('autoclear', function(s) { + var autoCleaner = s.autoclear || CONSTANTS.NO; + if (autoCleaner === CONSTANTS.YES) + runCleaner(); + }); });