This repository has been archived by the owner on Feb 24, 2018. It is now read-only.
forked from darrinhenein/VerticalTabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
259 lines (220 loc) · 6.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* ***** BEGIN LICENSE BLOCK *****
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* ***** END LICENSE BLOCK ***** */
"use strict";
// SDK
var preferencesService = require("sdk/preferences/service");
var windows = require("sdk/windows");
var windowUtils = require("sdk/window/utils");
var { viewFor } = require("sdk/view/core");
// WebExtension
const webExtension = require("sdk/webextension");
var webextPort;
var webextPreferences = {};
var tabsAnimatePrefBackup = false;
// Modules
var { VerticalTabsReloaded } = require("./lib/verticaltabs.js");
var GLOBAL_SCOPE = this;
//
// WebExtenions Communication
//
// Send message to WebExtension
function webext_sendMsg(message)
{
webextPort.postMessage(message);
}
var sdk_inited = false;
// Handle messages from WebExtension
function webext_replyHandler(message)
{
if(message.type == "settings.migrate")
{
if(preferencesService.get("[email protected]") != undefined)
{
webext_sendChangedSetting("compact");
webext_sendChangedSetting("debug");
webext_sendChangedSetting("hideInFullscreen");
webext_sendChangedSetting("right");
webext_sendChangedSetting("tabtoolbarPosition");
webext_sendChangedSetting("theme");
webext_sendChangedSetting("toggleDisplayHotkey");
webext_sendChangedSetting("width");
preferencesService.reset("[email protected]");
}
else
{
// Delete all settings
var prefKeys = preferencesService.keys("extensions.@verticaltabsreloaded.");
for (var i = 0; i < prefKeys.length; i++)
{
preferencesService.reset(prefKeys[i]);
}
}
}
if(message.type == "settings.post")
{
// Get settings from WebExt
debugOutput(message.name + " new value SDK: " + message.value);
observPrefs(message.name);
}
if(message.type == "settings.post-all")
{
// Get all settings from WebExt
webextPreferences = message.value;
if(sdk_inited == "prepared")
{
sdk_inited = true;
sdk_init();
}
else if(sdk_inited == false)
{
sdk_inited = "prepared";
}
observPrefs("");
}
if(message.type == "settings.toggleDrawInTitlebar")
{
// Toggle function of browser.tabs.drawInTitlebar for preference page
if(preferencesService.get("browser.tabs.drawInTitlebar", true))
{
preferencesService.set("browser.tabs.drawInTitlebar", false);
}
else
{
preferencesService.set("browser.tabs.drawInTitlebar", true);
}
}
if(message.type == "event.fullscreen")
{
let windowID = windowUtils.getOuterId(windowUtils.getToplevelWindow(windowUtils.getFocusedWindow()));
GLOBAL_SCOPE["vt"+windowID].changeFullscreenMode(message.value);
}
if(message.type == "event.toggleTabbrowser")
{
let windowID = windowUtils.getOuterId(windowUtils.getToplevelWindow(windowUtils.getFocusedWindow()));
GLOBAL_SCOPE["vt"+windowID].toggleDisplayState();
}
}
// Send setting to WebExtension
function webext_sendSetting(settingName, value)
{
webext_sendMsg({
type: "settings.post",
name: settingName,
value: value
});
}
// Changed addon preferences, send to WebExtension
function webext_sendChangedSetting(settingName)
{
webext_sendSetting(settingName, preferencesService.get("extensions.@verticaltabsreloaded." + settingName));
}
function observPrefs(settingName)
{
for (let window of windows.browserWindows)
{
let lowLevelWindow = viewFor(window);
let windowID = windowUtils.getOuterId(lowLevelWindow);
debugOutput("observPrefs: " + settingName);
GLOBAL_SCOPE["vt"+windowID].onPreferenceChange(settingName, webextPreferences);
}
}
//
// End of WebExtenions Communication
//
function initialize_window(window)
{
let lowLevelWindow = viewFor(window);
let windowID = windowUtils.getOuterId(lowLevelWindow);
GLOBAL_SCOPE["vt"+windowID] = new VerticalTabsReloaded(lowLevelWindow, webextPort, webextPreferences);
}
function deinitialize_window(window)
{
let lowLevelWindow = viewFor(window);
let windowID = windowUtils.getOuterId(lowLevelWindow);
GLOBAL_SCOPE["vt"+windowID].unload();
delete GLOBAL_SCOPE["vt"+windowID];
}
function sdk_init()
{
// Initialize VerticalTabsReloaded object for each window.
for (let window of windows.browserWindows)
{
initialize_window(window);
}
windows.browserWindows.on('open', function(window)
{
initialize_window(window);
});
windows.browserWindows.on('close', function(window)
{
deinitialize_window(window)
});
}
// Entry point of the add-on
exports.main = function (options, callbacks) {
// WebExtension startup + communication
webExtension.startup().then(api =>
{
const {browser} = api;
browser.runtime.onMessage.addListener((msg, sender, sendResponse) =>
{
webext_replyHandler(msg);
});
browser.runtime.onConnect.addListener((port) =>
{
webextPort = port; // make it global
//debugOutput(options.loadReason);
if (options.loadReason == "install")
{
preferencesService.set("browser.tabs.drawInTitlebar", false);
}
// Back up browser.tabs.animate (FF54-) or toolkit.cosmeticAnimations.enabled (FF55+) pref before overwriting it
if(preferencesService.has("toolkit.cosmeticAnimations.enabled"))
{
tabsAnimatePrefBackup = preferencesService.get("toolkit.cosmeticAnimations.enabled");
preferencesService.set("toolkit.cosmeticAnimations.enabled", false);
}
else
{
tabsAnimatePrefBackup = preferencesService.get("browser.tabs.animate");
preferencesService.set("browser.tabs.animate", false);
}
if(sdk_inited == "prepared")
{
sdk_inited = true;
sdk_init();
}
else if(sdk_inited == false)
{
sdk_inited = "prepared";
}
});
});
}
exports.onUnload = function (reason)
{
//debugOutput("onUnload:" + reason);
for (let window of windows.browserWindows)
{
deinitialize_window(window);
}
if(preferencesService.has("toolkit.cosmeticAnimations.enabled"))
{
preferencesService.set("toolkit.cosmeticAnimations.enabled", tabsAnimatePrefBackup);
}
else
{
preferencesService.set("browser.tabs.animate", tabsAnimatePrefBackup);
}
}
function debugOutput(output)
{
webext_sendMsg({
type: "debug.log",
value: output
});
}