-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
225 lines (195 loc) · 7.29 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
"use strict";
/* global utils */
var sidebarPorts = {};
function manage_installation(details)
{
utils.options.get_options_file().then(() =>
{
if(details.reason == "install")
{
// FIXME
// browser.tabs.create({url: "install-notes.html"});
// browser.sidebarAction.open();
}
if(details.reason == "update")
{
utils.log.debug("VTR update!");
utils.options.get_setting("theme").then(value =>
{
if(value == "none")
{
utils.options.save_setting("theme", "dark"); // This is likely irrelvant, but could prevent some race conditions
utils.options.restore_default_setting_of("theme");
}
if(value == "windows" || value == "linux")
{
utils.options.save_setting("theme", "feather");
}
});
// Migrate toggleDisplayHotkey from legacy (pre-WebExtension format) to WebExtension FF60+ format
utils.options.get_setting("toggleDisplayHotkey").then(value =>
{
// While this only replaces the - with + it triggers an option change which in turns triggers the default
// hotkey validation check in utils/options.js; if it's invalid we reset to default hotkey
utils.options.save_setting("toggleDisplayHotkey", value.replace(/-/g, "+"));
});
utils.options.get_setting("runtime.vtr.installedVersion").then((installedVersion) =>
{
let previousVersion;
if(installedVersion == undefined)
{
previousVersion = details.previousVersion.split(".");
if(typeof browser.sidebarAction != "undefined" && typeof browser.sidebarAction.open != "undefined")
{
browser.sidebarAction.open();
}
browser.tabs.create({url: "notes/index.html"});
}
else
{
previousVersion = installedVersion.split(".");
}
utils.log.debug(previousVersion);
let major = parseInt(previousVersion[0], 10);
let minor = parseInt(previousVersion[1], 10);
let patch;
if(previousVersion[2].includes("a"))
{
patch = -1; // lets make alpha versions comparable in a more easy way
}
else
{
patch = parseInt(previousVersion[2], 10);
}
if(installedVersion != undefined && (major < 0 || (major == 0 && minor < 13) || (major == 0 && minor == 13 && patch < 0)))
{
if(typeof browser.sidebarAction != "undefined" && typeof browser.sidebarAction.open != "undefined")
{
browser.sidebarAction.open();
}
browser.tabs.create({url: "notes/index.html"});
}
});
}
let manifest = browser.runtime.getManifest();
utils.log.debug("manifest version" + manifest.version);
utils.options.save_setting("runtime.vtr.installedVersion", manifest.version);
utils.log.debug("manage_installation called");
});
}
// Set up listener
browser.runtime.onInstalled.addListener(manage_installation);
browser.runtime.onConnect.addListener((port) =>
{
sidebarPorts[port.name] = port;
if(port.sender.id == browser.runtime.id)
{
port.onDisconnect.addListener((port) =>
{
delete sidebarPorts[port.name];
let portInfo = port.name.split("-");
if(portInfo[0] == "sidebarAction")
{
utils.windows.setSidebarOpenedStatus(portInfo[1], false);
}
});
port.onMessage.addListener((message) =>
{
if(message["message"]["type"] == "sidebarAction")
{
utils.windows.setSidebarOpenedStatus(message["message"]["windowID"], true);
}
});
}
});
browser.storage.onChanged.addListener(utils.options.on_options_change);
browser.windows.onCreated.addListener((window) =>
{
utils.windows.add(window.id);
});
browser.windows.onRemoved.addListener((windowID) =>
{
utils.windows.remove(windowID);
});
browser.windows.getAll({windowTypes: ["normal", "popup"]}).then((windowInfoArray) =>
{
for (let windowInfo of windowInfoArray)
{
utils.windows.add(windowInfo.id);
}
});
browser.windows.onFocusChanged.addListener((windowID) =>
{
utils.windows.setCurrentWindow(windowID);
});
browser.windows.getCurrent().then((currentWindow) =>
{
utils.windows.setCurrentWindow(currentWindow.id);
});
/* browser.commands.onCommand.addListener((command) =>
{
if (command == "toggleTabbrowser")
{
// FIREFIX: FIXME: Firefox dones't count hotkeys as "user input" therefore dones't allow us here to toggle the sidebar... stupid.
if(utils.windows.getSidebarOpenedStatus(utils.windows.getCurrentWindow()) == false)
{
utils.windows.setSidebarOpenedStatus(utils.windows.getCurrentWindow(), true);
browser.sidebarAction.open();
}
else
{
utils.windows.setSidebarOpenedStatus(utils.windows.getCurrentWindow(), false);
browser.sidebarAction.close();
}
}
}); */
browser.browserAction.onClicked.addListener(() =>
{
if(utils.windows.getSidebarOpenedStatus(utils.windows.getCurrentWindow()) == false)
{
utils.windows.setSidebarOpenedStatus(utils.windows.getCurrentWindow(), true);
// FIXME: Create "does function exists utils module" to tackle different browsers+versions
if(typeof browser.sidebarAction == "undefined" || typeof browser.sidebarAction.open == "undefined")
{
return;
}
browser.sidebarAction.open();
}
else
{
utils.windows.setSidebarOpenedStatus(utils.windows.getCurrentWindow(), false);
if(typeof browser.sidebarAction == "undefined" || typeof browser.sidebarAction.close == "undefined")
{
return;
}
browser.sidebarAction.close();
}
});
if(typeof browser.runtime.getBrowserInfo == "undefined")
{
// This likely means we are running in Opera, set same setting as in Firefox Nightly
utils.options.set_alpha_settings();
}
else
{
browser.runtime.getBrowserInfo().then((browserInfo) =>
{
let version = browserInfo.version;
// Enforce debugging, hidden settings and experiment flag to true for Firefox Nightly
if(version.includes("a"))
{
utils.options.set_alpha_settings();
}
// Enforce debugging and hidden settings for Firefox Beta
if(version.includes("b"))
{
utils.options.set_beta_settings();
}
});
}
// FIXME: Once we are removing FF 60 (ESR) support and the legacy hotkey import, we can just use utils.options.update_hotkey() here
// Setting hotkey for toggeling the sidebar
utils.options.get_setting("toggleDisplayHotkey").then((value) =>
{
browser.commands.update({"name": "_execute_sidebar_action", "shortcut": value});
});