Skip to content
This repository has been archived by the owner on Feb 24, 2018. It is now read-only.

Commit

Permalink
Add embedded WebExtension + sync settings to new storage (darrinhenei…
Browse files Browse the repository at this point in the history
  • Loading branch information
Croydon committed Jan 6, 2017
1 parent 2ff88ca commit 9913686
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 35 deletions.
116 changes: 83 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -161,3 +210,4 @@ function debugOutput(output) {
console.log(output);
}
}

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
"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,
"multiprocess": true
},
"engines":
{
"firefox": "45.0 - 51.0"
"firefox": "45.0 - 53.0"
},
"hasEmbeddedWebExtension": true,
"preferences-branch": "@verticaltabsreloaded",
"preferences": [{
"name": "right",
Expand Down
99 changes: 99 additions & 0 deletions webextension/background.js
Original file line number Diff line number Diff line change
@@ -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);*/
9 changes: 9 additions & 0 deletions webextension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Vertical Tabs Reloaded",
"version": "0.8.0-alpha",
"manifest_version": 2,
"permissions": ["storage"],
"background": {
"scripts": ["background.js"]
}
}

0 comments on commit 9913686

Please sign in to comment.