-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
271 lines (235 loc) · 8.02 KB
/
options.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
260
261
262
263
264
265
266
267
268
269
270
271
// Some of code is from SSS (Swift Selection Search) settings.js/.html
// by Daniel Lobo/CanisLupus
(function(browser) {
'use strict'
const promises = chrome.runtime.getURL('').startsWith('moz-extension://');
if (browser === null) {
browser = chrome;
}
let defaultSettings;
let settings;
let errorAlert, printWarning, printLog, printInfo, printDebug;
const page = {};
function updatePage() {
// console.trace();
for (const item of page.inputs) {
loadSettingValueIntoElement(item);
}
if (settings.sectionsExpansionState !== undefined) {
const sectionsState = settings.sectionsExpansionState;
for (const sectionId in sectionsState) {
const section = document.getElementById(sectionId);
if (section) {
section.classList.toggle('collapsed-section',
!sectionsState[sectionId]);
}
}
}
}
function loadSettingValueIntoElement(item) {
const name = item.name;
if (name in settings) {
const value = settings[name];
if (item.type === 'checkbox') {
item.checked = value;
} else {
item.value = value;
}
updateOptionsVisiblity(name, value);
}
}
function updateOptionsVisiblity(name, value) {
const setVisiblity = (element, visible) => {
printDebug("setVisiblity(" + name + "): " + visible);
element.closest('.setting').classList.toggle('hidden', !visible);
};
switch (name) {
case 'autoHoverCopy': {
const visible = (value !== 'never');
setVisiblity(page.autoHoverDelay, visible);
} break;
case 'clipboardCopyTooltip':
setVisiblity(page.maxTooltip, value);
break;
case 'debugLevel': {
const visible = (value !== '0');
setVisiblity(page.debugHostPrefix, visible);
setVisiblity(page.debugDuplicate, visible);
} break;
case 'shiftLeftClick':
case 'shiftMiddleClick':
case 'maxTooltip':
case 'autoHoverDelay':
case 'modifierKeyTracking':
case 'middleClickClose':
case 'linkFormat':
case 'tabLinkFormat':
case 'debugDuplicate':
case 'debugHostPrefix':
printDebug("updateOptionsVisiblity: nothign to do.");
break;
default:
errorAlert("Bad updateOptionsVisiblity("+ name + ", " + value +")");
break;
}
}
let globalIntializingPage = true;
function initializePage() {
printLog("initializePage() debugLevel:" + settings.debugLevel);
document.getElementById('debugLevelContainer').innerHTML =
getDebugLevelSelectHtml('setting-input');
page.inputs = document.querySelectorAll("input, select, textarea");
for (const item of page.inputs) {
const name = item.name;
if (name) {
printDebug("initialize page[" + item.name + "]");
page[item.name] = item;
}
}
document.getElementById('settings').onchange = ev => {
const item = ev.target;
const name = item.name;
if (name in settings) {
let value;
if (item.type === 'checkbox') {
value = item.checked;
} else if (item.type === 'number') {
value = parseInt(item.value);
} else {
value = item.value;
}
printInfo("onchange target: " + name + " -> " + value);
updateOptionsVisiblity(name, value);
if (!globalIntializingPage) {
saveNewSettings({[name]: value});
}
}
}
globalIntializingPage = false;
for (const elem of document.querySelectorAll('.setting-reset')) {
const inputElements = elem.querySelectorAll('input');
if (inputElements.length === 0) {
continue;
}
inputElements[0].onclick = _ => {
const parent = elem.closest('.setting');
const formElement = parent.querySelector('.setting-input');
const name = formElement.name;
const value = defaultSettings[name];
printWarning("reset setting[" + name + "]=" + value);
saveNewSettings({[name]: value});
loadSettingValueIntoElement(formElement);
};
}
for (const sectionNameElement of
document.querySelectorAll('.section-name')) {
sectionNameElement.onclick = _ => {
if (!settings.sectionsExpansionState) {
settings.sectionsExpansionState = {};
}
const sectionsExpansionState = settings.sectionsExpansionState;
const parent = sectionNameElement.parentElement;
const isCollapsed =parent.classList.toggle('collapsed-section');
sectionsExpansionState[parent.id] = !isCollapsed;
saveNewSettings({sectionsExpansionState});
};
}
updatePage();
}
function saveNewSettings(items) {
printDebug("saveNewSettings(" + Object.keys(items) + ")");
for (const key in items) {
printDebug("settings[" + key + "]=" + items[key]);
settings[key] = items[key];
}
if (promises) {
browser.storage.local.set(items).
then(_ => {},
ErrorHandler("Error saving local settings."));
} else {
browser.storage.local.set(items);
}
// Print warnings when certain options are changed.
for (const name in items) {
const value = items[name];
switch (name) {
case 'middleClickClose':
if (value !== 'never') {
alert("You've just turned on the option to close a page " +
"when the middle click button is clicked anywhere " +
"on the page that is NOT a link.");
}
break;
case 'autoHoverCopy':
// if (value !== 'never' && value != 'always') {
// alert("Auto hover copy may not always gets triggered " +
// "because shift/ctrl/alt key detection can be " +
// "unreliable. Therefore it is recommended that you "+
// 'use the "Always (On)" option.');
// }
// } break;
alertAutoHoverCopyShiftLeftClickConflict();
break;
case 'shiftLeftClick':
alertAutoHoverCopyShiftLeftClickConflict();
break;
}
}
function alertAutoHoverCopyShiftLeftClickConflict() {
const autoHoverCopy = settings.autoHoverCopy;
if (settings.shiftLeftClick) {
const warning = "\n\n" +
"Basically, auto hover copy may overwrite the " +
"clipboard and replace whatever you just copied " +
"there using shift + left click.";
if (autoHoverCopy === 'shift') {
alert("Using shift as Auto hover copy modifier may not " +
"work properly with Shift+Left click." + warning);
} else if (autoHoverCopy === 'always') {
alert("Auto hover copy Always (On) may not work propely " +
"with Shift+Left click." + warning);
}
}
}
}
document.addEventListener("DOMContentLoaded", _ => {
if (defaultSettings === undefined) {
console.debug("DOMContentLoaded before content-settings");
// hack hack hack
// let 'content-settings' handler know that DOM has been loaded
defaultSettings = '';
} else {
initializePage();
}
});
function handleBackgroundResponse(response) {
settings = response.settings;
[errorAlert, printWarning, printLog, printInfo, printDebug] =
getConsolePrints("copylink o: ", settings.debugLevel,
settings.debugDuplicate);
printDebug("defaultSettings:" + defaultSettings);
printDebug("settings:" + settings);
const domLoaded = (defaultSettings === '');
defaultSettings = response.defaultSettings;
if (domLoaded) {
// sendMessage is async, so we have to check if DOMContentLoaded
// has ocurred and call initializePage() if (domLoaded) is true
// Otherwise let initializePage() be called by
// the DOMContentLoaded event handler above.
initializePage();
}
}
const message = { command: 'all-settings' };
if (promises) {
browser.runtime.sendMessage(message).
then(handleBackgroundResponse,
ErrorHandler("Error requesting options from background.js"));
} else {
browser.runtime.sendMessage(message, handleBackgroundResponse);
}
})(typeof browser === 'undefined'? null: browser);
// Must check using (typeof browser === 'undefined') rather than
// use something like (browser || chrome)
// otherwise chrome will throw an error and the extension will not load
//
// Do not use (chrome || browser) because chrome is actually defined on Firefox