From b3b6d7718cd9b04a927629d892332411955c0fc7 Mon Sep 17 00:00:00 2001 From: Martin Meredith Date: Wed, 14 Sep 2016 11:55:16 +0100 Subject: [PATCH] cleanup of settings --- extension/manifest.json | 2 +- extension/scripts/background.js | 38 ++++++++++++++++----------------- extension/scripts/options.js | 20 ++++++++++------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/extension/manifest.json b/extension/manifest.json index 933131d..79a8b93 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -18,12 +18,12 @@ ] }, "permissions": [ - "activeTab", "notifications", "storage" ], "optional_permissions": [ "", + "activeTab", "cookies" ], "options_page": "html/options.html" diff --git a/extension/scripts/background.js b/extension/scripts/background.js index c862fa0..fb20eeb 100644 --- a/extension/scripts/background.js +++ b/extension/scripts/background.js @@ -1,19 +1,31 @@ chrome.browserAction.onClicked.addListener(function (activeTab) { chrome.permissions.request({ - permissions: ['cookies'], + permissions: ['activeTab', 'cookies'], origins: [activeTab.url] }, function (granted) { var message = "Omnomnomnom"; if (granted) { - chrome.storage.sync.get("sound", function(data) { - if (data.sound) { - var audio = new Audio(); - audio.src = chrome.extension.getURL('/sound/omnomnom.mp3'); - audio.play(); + + chrome.storage.sync.get( + ['sound', 'clearLocalStorage', 'clearSessionStorage'], + function(data) { + if (data.sound) { + var audio = new Audio(); + audio.src = chrome.extension.getURL('/sound/omnomnom.mp3'); + audio.play(); + } + + if (data.clearLocalStorage) { + chrome.tabs.executeScript({code:'localStorage.clear();'}) + } + + if (data.clearSessionStorage) { + chrome.tabs.executeScript({code:'localStorage.clear();'}) + } } - }); + ); chrome.cookies.getAll({url: activeTab.url}, function (cookies) { $.each(cookies, function (index, cookie) { @@ -24,18 +36,6 @@ chrome.browserAction.onClicked.addListener(function (activeTab) { ); }); }); - - chrome.storage.sync.get("clearLocalStorage", function(data) { - if (data.clearLocalStorage) { - chrome.tabs.executeScript({code:'localStorage.clear();'}) - } - }); - - chrome.storage.sync.get("clearSessionStorage", function(data) { - if (data.clearSessionStorage) { - chrome.tabs.executeScript({code:'localStorage.clear();'}) - } - }); } else { message = "Cookie Monster is sad that there are no cookies"; } diff --git a/extension/scripts/options.js b/extension/scripts/options.js index d9c8899..f3bf7c1 100644 --- a/extension/scripts/options.js +++ b/extension/scripts/options.js @@ -1,17 +1,21 @@ (function ($) { $(function () { - chrome.storage.sync.get("sound", function(data) { - $('#sound').prop("checked", data.sound); - console.log(data); - }); + chrome.storage.sync.get( + ['sound', 'clearLocalStorage', 'clearSessionStorage'], + function (data) { + $('#sound').prop("checked", data.sound); + $('#local-storage').prop("checked", data.clearLocalStorage); + $('#session-storage').prop("checked", data.clearSessionStorage); + } + ); $('#save_btn').closest('form').submit(function (e) { e.preventDefault(); chrome.storage.sync.set({ - "sound": $('#sound').prop("checked"), - "clearLocalStorage": $('#local-storage').prop("checked"), - "clearSessionStorage": $('#session-storage').prop("checked"), - }, function() { + 'sound': $('#sound').prop("checked"), + 'clearLocalStorage': $('#local-storage').prop('checked'), + 'clearSessionStorage': $('#session-storage').prop('checked') + }, function () { window.alert('The options have been saved!'); }); });