From 99136868659f63930b8b2e59e39c29f2be40e180 Mon Sep 17 00:00:00 2001 From: Croydon Date: Fri, 6 Jan 2017 17:17:37 +0100 Subject: [PATCH] Add embedded WebExtension + sync settings to new storage (#13) --- index.js | 116 ++++++++++++++++++++++++++----------- package.json | 5 +- webextension/background.js | 99 +++++++++++++++++++++++++++++++ webextension/manifest.json | 9 +++ 4 files changed, 194 insertions(+), 35 deletions(-) create mode 100644 webextension/background.js create mode 100644 webextension/manifest.json diff --git a/index.js b/index.js index 94fe0da..f2eb55b 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,10 @@ var hotkey = require("sdk/hotkeys").Hotkey; var { viewFor } = require("sdk/view/core"); var { Services } = require("resource://gre/modules/Services.jsm"); +// WebExtension +const webExtension = require("sdk/webextension"); +var webextPort; + // Modules var { unload } = require("./lib/unload.js"); var { VerticalTabsReloaded } = require("./lib/verticaltabs.js"); @@ -84,6 +88,35 @@ function changeHotkey() { initHotkeys(); } +// +// WebExtenions Communication +// + +// Send message to WebExtension +function webext_sendMsg(message) +{ + webextPort.postMessage(message); +} + +// Handle messages from WebExtension +function webext_replyHandler(message, sender, sendResponse) +{ + if(message.type == "settings.get") + { + webext_sendChangedSetting(message.name); + } +} + +// Changed addon preferences, send to WebExtension +function webext_sendChangedSetting(settingName) +{ + webext_sendMsg({ + type: "settings.post", + name: settingName, + value: preferences[settingName] + }); +} + // Entry point exports.main = function (options, callbacks) { //debugOutput(options.loadReason); @@ -105,39 +138,55 @@ exports.main = function (options, callbacks) { preferencesService.set("browser.tabs.animate", preferences["animate"]); }); - - // Initialize VerticalTabsReloaded object for each window. - - for (let window of windows.browserWindows) { - let lowLevelWindow = viewFor(window); - let windowID = windowUtils.getOuterId(lowLevelWindow); - GLOBAL_SCOPE["vt"+windowID] = new VerticalTabsReloaded(lowLevelWindow); - unload(GLOBAL_SCOPE["vt" + windowID].unload.bind(GLOBAL_SCOPE["vt"+windowID]), lowLevelWindow); - } - - windows.browserWindows.on('open', function(window) { - let lowLevelWindow = viewFor(window); - let windowID = windowUtils.getOuterId(lowLevelWindow); - GLOBAL_SCOPE["vt"+windowID] = new VerticalTabsReloaded(lowLevelWindow); - unload(GLOBAL_SCOPE["vt" + windowID].unload.bind(GLOBAL_SCOPE["vt"+windowID]), lowLevelWindow); - }); - - windows.browserWindows.on('close', function(window) { - let lowLevelWindow = viewFor(window); - let windowID = windowUtils.getOuterId(lowLevelWindow); - GLOBAL_SCOPE["vt"+windowID].unload(); - delete GLOBAL_SCOPE["vt"+windowID]; - }); - - initHotkeys(); - simplePrefs.on("toggleDisplayHotkey", changeHotkey); - - unload(function() { - destroyHotkey(); - simplePrefs.off("toggleDisplayHotkey", changeHotkey); - }); - -}; + // WebExtension startup + communication + webExtension.startup().then(api => + { + const {browser} = api; + browser.runtime.onConnect.addListener((port) => + { + webextPort = port; // make it global + }); + + browser.runtime.onMessage.addListener((msg, sender, sendResponse) => + { + webext_replyHandler(msg, sender, sendResponse); + }); + + // Initialize VerticalTabsReloaded object for each window. + + for (let window of windows.browserWindows) { + let lowLevelWindow = viewFor(window); + let windowID = windowUtils.getOuterId(lowLevelWindow); + GLOBAL_SCOPE["vt"+windowID] = new VerticalTabsReloaded(lowLevelWindow); + unload(GLOBAL_SCOPE["vt" + windowID].unload.bind(GLOBAL_SCOPE["vt"+windowID]), lowLevelWindow); + } + + windows.browserWindows.on('open', function(window) { + let lowLevelWindow = viewFor(window); + let windowID = windowUtils.getOuterId(lowLevelWindow); + GLOBAL_SCOPE["vt"+windowID] = new VerticalTabsReloaded(lowLevelWindow); + unload(GLOBAL_SCOPE["vt" + windowID].unload.bind(GLOBAL_SCOPE["vt"+windowID]), lowLevelWindow); + }); + + windows.browserWindows.on('close', function(window) { + let lowLevelWindow = viewFor(window); + let windowID = windowUtils.getOuterId(lowLevelWindow); + GLOBAL_SCOPE["vt"+windowID].unload(); + delete GLOBAL_SCOPE["vt"+windowID]; + }); + + initHotkeys(); + simplePrefs.on("toggleDisplayHotkey", changeHotkey); + + simplePrefs.on("", webext_sendChangedSetting); + + unload(function() { + destroyHotkey(); + simplePrefs.off("toggleDisplayHotkey", changeHotkey); + simplePrefs.on("", webext_sendChangedSetting); + }); + }); +} exports.onUnload = function (reason) { //debugOutput("onUnload:" + reason); @@ -161,3 +210,4 @@ function debugOutput(output) { console.log(output); } } + diff --git a/package.json b/package.json index 98d3854..bde7172 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "icon": "resource://verticaltabsreloaded-at-go-dev-dot-de/data/icon.png", "author": "Croydon", "license": "MPL-2.0", - "version": "0.7.1", + "version": "0.8.0-alpha", "permissions": { "private-browsing": true, @@ -15,8 +15,9 @@ }, "engines": { - "firefox": "45.0 - 51.0" + "firefox": "45.0 - 53.0" }, + "hasEmbeddedWebExtension": true, "preferences-branch": "@verticaltabsreloaded", "preferences": [{ "name": "right", diff --git a/webextension/background.js b/webextension/background.js new file mode 100644 index 0000000..628497e --- /dev/null +++ b/webextension/background.js @@ -0,0 +1,99 @@ +"use strict" + +var port = browser.runtime.connect({name: "connection-to-legacy"}); + + +// +// Handle addon settings +// + +function saveSettings(name, value) +{ + let settingsObject = {}; + settingsObject[name] = value; + + browser.storage.local.set(settingsObject).then(error => + { + if(error) + { + return false; + } + + return true; + }); +} + +function getSettingsError(name) +{ + console.log("VTR WebExt setting '"+ name +"' not saved."); +} + +function getSettings(name) +{ + return new Promise(function (fulfill, reject) + { + browser.storage.local.get(name).then(results => + { + if (!results[name]) + { + getSettingsError(name); + } + + fulfill(results[name]); + }); + }); +} + + +// +// Communication with the legacy part of the addon +// + +function sdk_replyHandler(message) +{ + if(message.type == "settings.post") + { + // the legacy part sent settings, save them + saveSettings(message.name, message.value); + } +} + +port.onMessage.addListener(sdk_replyHandler); + +function sdk_sendMsg(message) +{ + browser.runtime.sendMessage(message).then(reply => + { + if (reply) { + sdk_replyHandler(reply); + } + }); +} + + +// Get all settings from the legacy part 10secs after startup +setTimeout(function(){ + sdk_sendMsg({type: "settings.get", name: "right"}); + sdk_sendMsg({type: "settings.get", name: "hideInFullscreen"}); + sdk_sendMsg({type: "settings.get", name: "theme"}); + sdk_sendMsg({type: "settings.get", name: "tabtoolbarPosition"}); + sdk_sendMsg({type: "settings.get", name: "toggleDisplayHotkey"}); + sdk_sendMsg({type: "settings.get", name: "width"}); + sdk_sendMsg({type: "settings.get", name: "debug"}); +}, 10000); + +/*setInterval(function(){ + + getSettings("theme").then(value => { + console.log("received theme setting: " + value); + }); + + getSettings("toggleDisplayHotkey").then(value => { + console.log("received toggleDisplayHotkey setting: " + value); + }); + + getSettings().then(value => { + console.log(value); + }); + +}, 4000);*/ diff --git a/webextension/manifest.json b/webextension/manifest.json new file mode 100644 index 0000000..2749f4c --- /dev/null +++ b/webextension/manifest.json @@ -0,0 +1,9 @@ +{ + "name": "Vertical Tabs Reloaded", + "version": "0.8.0-alpha", + "manifest_version": 2, + "permissions": ["storage"], + "background": { + "scripts": ["background.js"] + } +}