diff --git a/extension/content-script.js b/extension/content-script.js --- a/extension/content-script.js +++ b/extension/content-script.js @@ -58,13 +58,13 @@ } }); -var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync); - -storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) { +SettingsUtils.get().then((items) => { if (items.breezeScrollBars.enabled) { loadBreezeScrollBars(); } - if (items.mpris.enabled) { + + const mpris = items.mpris; + if (mpris.enabled) { loadMpris(); if (items.mprisMediaSessions.enabled) { loadMediaSessionsShim(); diff --git a/extension/extension-utils.js b/extension/extension-utils.js --- a/extension/extension-utils.js +++ b/extension/extension-utils.js @@ -23,8 +23,6 @@ let currentMessageSerial = 0; let pendingMessageReplyResolvers = {}; -var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync); - // Callback is called with following arguments (in that order); // - The actual message data/payload // - The name of the action triggered diff --git a/extension/extension.js b/extension/extension.js --- a/extension/extension.js +++ b/extension/extension.js @@ -41,12 +41,7 @@ } function sendSettings() { - storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) { - if (chrome.runtime.lastError) { - console.warn("Failed to load settings"); - return; - } - + SettingsUtils.get().then((items) => { sendPortMessage("settings", "changed", items); }); } diff --git a/extension/manifest.json b/extension/manifest.json --- a/extension/manifest.json +++ b/extension/manifest.json @@ -22,6 +22,7 @@ "background": { "scripts": [ "constants.js", + "utils.js", "extension-utils.js", "extension-kdeconnect.js", @@ -43,7 +44,7 @@ "content_scripts": [ { "matches": ["*://*/*"], - "js": ["constants.js", "content-utils.js", "content-script.js"], + "js": ["constants.js", "utils.js", "content-utils.js", "content-script.js"], "run_at":"document_start", "all_frames": true, "match_about_blank": true diff --git a/extension/options.html b/extension/options.html --- a/extension/options.html +++ b/extension/options.html @@ -7,6 +7,7 @@ + diff --git a/extension/options.js b/extension/options.js --- a/extension/options.js +++ b/extension/options.js @@ -16,8 +16,6 @@ along with this program. If not, see . */ -var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync); - function tabClicked(tabbar, tabbutton) { tabbar.buttons.forEach(function (button) { var tablink = button.dataset.tabLink @@ -34,15 +32,8 @@ }); } -function loadSettings(cb) { - storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) { - if (chrome.runtime.lastError) { - if (typeof cb === "function") { - cb(false); - } - return; - } - +function loadSettings() { + SettingsUtils.get().then((items) => { for (let key in items) { if (!items.hasOwnProperty(key)) { continue; @@ -94,10 +85,6 @@ }); } } - - if (typeof cb === "function") { - cb(true); - } }); } @@ -141,14 +128,11 @@ } } - try { - storage.set(settings, function () { - return cb(chrome.runtime.lastError); - }); - // When the extension is reloaded, any call to extension APIs throws - } catch (e) { - cb(e); - } + SettingsUtils.set(settings).then(() => { + cb(); + }, (err) => { + cb(err); + }); } function updateDependencies(control, extension, settingsKey) { diff --git a/extension/utils.js b/extension/utils.js new file mode 100644 --- /dev/null +++ b/extension/utils.js @@ -0,0 +1,54 @@ +/* + Copyright (C) 2019 Kai Uwe Broulik + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +class SettingsUtils { + static storage() { + return (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync); + } + + static get() { + return new Promise((resolve, reject) => { + SettingsUtils.storage().get(DEFAULT_EXTENSION_SETTINGS, (items) => { + const error = chrome.runtime.lastError; + if (error) { + return reject(error); + } + + resolve(items); + }); + }); + } + + static set(settings) { + return new Promise((resolve, reject) => { + try { + SettingsUtils.storage().set(settings, () => { + const error = chrome.runtime.lastError; + if (error) { + return reject(error); + } + + resolve(); + }); + } catch (e) { + reject(e); + } + }); + } +} + +