-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathindex.js
467 lines (430 loc) · 13.9 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import '@/js/dom-init';
import {CodeMirror, loadCmTheme, THEME_KEY} from '@/cm';
import compareVersion from '@/js/cmpver';
import {UCD} from '@/js/consts';
import {$, $$, $$remove, $create, $createLink} from '@/js/dom';
import {configDialog, messageBox, showSpinner} from '@/js/dom-util';
import {fetchTemplate, t, tBody} from '@/js/localization';
import {API} from '@/js/msg';
import * as prefs from '@/js/prefs';
import {styleCodeEmpty} from '@/js/sections-util';
import {isLocalhost} from '@/js/urls';
import {clipString, debounce, deepEqual, sessionStore, tryURL} from '@/js/util';
import {closeCurrentTab} from '@/js/util-webext';
import DirectDownloader from './direct-downloader';
import PortDownloader from './port-downloader';
import './install-usercss.css';
const CFG_SEL = '#message-box.config-dialog';
let cfgShown = true;
let cm;
/** @type {FileSystemHandle} */
let fsh;
/** @type {FileSystemObserver | boolean} */
let fso;
/** @type function(?options):Promise<?string> */
let getData;
let initialUrl;
let installed;
let installedDup;
let liveReload;
let liveReloadEnabled = false;
let sectionsPromise;
let tabId;
let vars;
// "History back" in Firefox (for now) restores the old DOM including the messagebox,
// which stays after installing since we don't want to wait for the fadeout animation before resolving.
document.on('visibilitychange', () => {
$$remove('#message-box:not(.config-dialog)');
if (installed) liveReload();
});
tBody();
setTimeout(() => !cm && showSpinner($('#header')), 200);
(async function init() {
if (location.hash) {
history.replaceState(null, '',
location.pathname + '?updateUrl=' + encodeURIComponent(location.hash.slice(1)));
}
const params = new URLSearchParams(location.search);
fsh = window.fsh;
tabId = params.has('tabId') ? Number(params.get('tabId')) : -1;
initialUrl = fsh ? fsh._url : params.get('updateUrl');
/** @type {Promise<?string>} */
let firstGet;
if (fsh) {
let oldCode = null;
getData = async () => {
const code = await (await fsh.getFile()).text();
if (oldCode !== code) return (oldCode = code);
};
firstGet = getData();
} else if (!initialUrl) {
if (history.length > 1) history.back();
else closeCurrentTab();
} else if (tabId < 0) {
getData = DirectDownloader(initialUrl);
firstGet = API.usercss.getInstallCode(initialUrl)
.then(code => code || getData())
.catch(getData);
} else if (!__.MV3) {
getData = PortDownloader();
firstGet = getData({force: true});
}
const hasFileAccessP = browser.extension.isAllowedFileSchemeAccess();
const tplP = fetchTemplate('/edit.html', 'styleSettings');
tplP.then(el => {
el.firstChild.remove(); // update URL
el.lastChild.remove(); // buttons
$('#styleSettings').append(el);
});
let dup, style, error, sourceCode;
try {
sourceCode = await firstGet;
({dup, style} = await API.usercss.build({sourceCode, checkDup: true, metaOnly: true}));
sectionsPromise = API.usercss.buildCode(style);
} catch (e) {
error = e;
}
liveReload = initLiveReload();
const [hasFileAccess] = await Promise.all([
hasFileAccessP,
prefs.ready,
]);
if (!style && sourceCode == null) {
messageBox.alert(isNaN(error) ? `${error}` : 'HTTP Error ' + error, 'pre');
return;
}
const theme = prefs.get(THEME_KEY);
loadCmTheme(theme);
cm = CodeMirror($('.main'), {
value: sourceCode || style.sourceCode,
readOnly: true,
colorpicker: true,
theme,
});
window.on('resize', adjustCodeHeight);
if (error) {
showBuildError(error);
}
if (!style) {
return;
}
const data = style[UCD];
const dupData = dup && dup[UCD];
const versionTest = dup && compareVersion(data.version, dupData.version);
updateMeta(style, dup);
if (dup) {
($(`[name="ss-scheme"][value="${dup.preferScheme}"]`) || {}).checked = true;
}
for (let type of ['in', 'ex']) {
const el = $('#ss-' + (type += 'clusions'));
const list = dup && dup[type] || [];
el.value = list.join('\n') + (list[0] ? '\n' : '');
el.rows = list.length + 2;
el.onchange = () => {
style[type] = el.value.split(/\n/).map(s => s.trim()).filter(Boolean);
};
}
// update UI
if (versionTest < 0) {
$('h1').after($create('.warning', t('versionInvalidOlder')));
}
$('button.install').onclick = () => {
shouldShowConfig();
(!dup ?
Promise.resolve(true) :
messageBox.confirm($create('span', t('styleInstallOverwrite', [
data.name + (dup.customName ? ` (${dup.customName})` : ''),
dupData.version,
data.version,
])))
).then(ok => ok &&
API.usercss.install(style)
.then(install)
.catch(err => messageBox.alert(t('styleInstallFailed', err.message || err), 'pre'))
);
};
// set updateUrl
const checker = $('.set-update-url input[type=checkbox]');
const updateUrl = tryURL(style.updateUrl || initialUrl || dup && dup.updateUrl);
if (!updateUrl) {
checker.disabled = true;
} else if (dup && dup.updateUrl === updateUrl.href) {
checker.checked = true;
// there is no way to "unset" updateUrl, you can only overwrite it.
checker.disabled = true;
} else if (updateUrl.protocol !== 'file:' || hasFileAccess) {
checker.checked = true;
style.updateUrl = updateUrl.href;
}
checker.onchange = () => {
style.updateUrl = checker.checked ? updateUrl.href : null;
};
checker.onchange();
$('.set-update-url p').textContent = clipString(updateUrl.href || '', 300);
// set prefer scheme
$('#ss-scheme').onchange = e => {
style.preferScheme = e.target.value;
};
if (!initialUrl || isLocalhost(initialUrl)) {
$('.live-reload input').onchange = liveReload;
} else {
$('.live-reload').remove();
}
})();
function updateMeta(style, dup = installedDup) {
installedDup = dup;
const data = style[UCD];
const dupData = dup && dup[UCD];
const versionTest = dup && compareVersion(data.version, dupData.version);
cm.setPreprocessor(data.preprocessor);
const installButtonLabel = t(
installed ? 'installButtonInstalled' :
!dup ? 'installButton' :
versionTest > 0 ? 'installButtonUpdate' : 'installButtonReinstall'
);
document.title = `${installButtonLabel} ${data.name}`;
$('.install').textContent = installButtonLabel;
$('.install').classList.add(
installed ? 'installed' :
!dup ? 'install' :
versionTest > 0 ? 'update' :
'reinstall');
if (dup && dup.updateUrl) {
$('.set-update-url').title = t('installUpdateFrom', dup.updateUrl).replace(/\S+$/, '\n$&');
}
$('.meta-name').textContent = data.name;
$('.meta-version').textContent = data.version;
$('.meta-description').textContent = data.description;
$$('#ss-scheme input').forEach(el => {
el.checked = el.value === (style.preferScheme || 'none');
});
replaceChildren($('.meta-author'), makeAuthor(data.author), true);
replaceChildren($('.meta-license'), data.license, true);
replaceChildren($('.external-link'), makeExternalLink());
getAppliesTo(style).then(list =>
replaceChildren($('.applies-to'), list.map(s => $create('li', s))));
Object.assign($('.configure-usercss'), {
hidden: !data.vars,
onclick: openConfigDialog,
});
if (!data.vars) {
cfgShown = false;
$$remove(CFG_SEL);
} else if (!deepEqual(data.vars, vars)) {
vars = data.vars;
// Use the user-customized vars from the installed style
for (const [dk, dv] of Object.entries(dup && dupData.vars || {})) {
const v = vars[dk];
if (v && v.type === dv.type) {
v.value = dv.value;
}
}
}
if (shouldShowConfig()) {
openConfigDialog();
}
$('#header').dataset.arrivedFast = performance.now() < 500;
$('#header').classList.add('meta-init');
$('#header').classList.remove('meta-init-error');
setTimeout(() => $$remove('.lds-spinner'), 1000);
showError('');
requestAnimationFrame(adjustCodeHeight);
if (dup) enablePostActions();
function makeAuthor(text) {
const match = text && text.match(/^(.+?)(?:\s+<(.+?)>)?(?:\s+\((.+?)\))?$/);
if (!match) {
return text;
}
const [, name, email, url] = match;
const elems = [];
if (email) {
elems.push($createLink(`mailto:${email}`, name));
} else {
elems.push($create('span', name));
}
if (url) {
elems.push($createLink(url, $create('i.i-external')));
}
return elems;
}
function makeExternalLink() {
const urls = [
data.homepageURL && [data.homepageURL, t('externalHomepage')],
data.supportURL && [data.supportURL, t('externalSupport')],
];
return (data.homepageURL || data.supportURL) && (
$create('div', [
$create('h3', t('externalLink')),
$create('ul', urls.map(args => args &&
$create('li',
$createLink(...args)
)
)),
]));
}
async function openConfigDialog() {
configDialog(style);
}
}
function showError(err) {
$('.warnings').textContent = '';
$('.warnings').classList.toggle('visible', Boolean(err));
document.body.classList.toggle('has-warnings', Boolean(err));
err = Array.isArray(err) ? err : [err];
if (err[0]) {
let i;
if ((i = err[0].index) >= 0 ||
(i = err[0].offset) >= 0) {
cm.jumpToPos(cm.posFromIndex(i));
cm.setSelections(err.map(e => {
const pos = e.index >= 0 && cm.posFromIndex(e.index) || // usercss meta parser
e.offset >= 0 && {line: e.line - 1, ch: e.col - 1}; // csslint code parser
return pos && {anchor: pos, head: pos};
}).filter(Boolean));
cm.focus();
}
$('.warnings').appendChild(
$create('.warning', [
t('parseUsercssError'),
'\n',
...err.map(e => e.message ? $create('pre', e.message) : e || 'Unknown error'),
]));
}
adjustCodeHeight();
}
function showBuildError(error) {
$('#header').classList.add('meta-init-error');
console.error(error);
showError(error);
}
function install(style) {
installed = style;
$$remove('.warning');
$('button.install').disabled = true;
$('button.install').classList.add('installed');
$('#live-reload-install-hint').hidden = !liveReloadEnabled;
$('.set-update-url').title = style.updateUrl ?
t('installUpdateFrom', style.updateUrl) : '';
$$('.install-disable input').forEach(el => (el.disabled = true));
document.body.classList.add('installed');
enablePostActions();
updateMeta(style);
if (liveReloadEnabled) liveReload();
}
function enablePostActions() {
const {id} = installed || installedDup;
sessionStore.justEditedStyleId = id;
$('#edit').search = `?id=${id}`;
$('#delete').onclick = async () => {
if (await messageBox.confirm(t('deleteStyleConfirm'), 'danger center', t('confirmDelete'))) {
await API.styles.remove(id);
if (tabId < 0 && history.length > 1) {
history.back();
} else {
closeCurrentTab();
}
}
};
}
async function getAppliesTo(style) {
if (sectionsPromise) {
try {
style.sections = (await sectionsPromise).sections;
} catch (error) {
showBuildError(error);
return [];
} finally {
sectionsPromise = null;
}
}
let numGlobals = 0;
const res = [];
const TARGETS = ['urls', 'urlPrefixes', 'domains', 'regexps'];
for (const section of style.sections) {
const targets = [].concat(...TARGETS.map(_ => section[_]).filter(Boolean));
res.push(...targets);
numGlobals += !targets.length && !styleCodeEmpty(section);
}
res.sort();
if (!res.length || numGlobals) {
res.push(t('appliesToEverything'));
}
return [...new Set(res)];
}
function adjustCodeHeight() {
// Chrome-only bug (apparently): it doesn't limit the scroller element height
const scroller = cm.display.scroller;
const prevWindowHeight = adjustCodeHeight.prevWindowHeight;
if (scroller.scrollHeight === scroller.clientHeight ||
prevWindowHeight && window.innerHeight !== prevWindowHeight) {
adjustCodeHeight.prevWindowHeight = window.innerHeight;
cm.setSize(null, $('.main').offsetHeight - $('.warnings').offsetHeight);
}
}
function initLiveReload() {
const DELAY = 500;
let timer = 0;
let sequence = Promise.resolve();
return e => {
if (e) liveReloadEnabled = e.target.checked;
if (!installed && !installedDup) return;
if (liveReloadEnabled) start({force: true}); else stop();
$('.install').disabled = liveReloadEnabled;
Object.assign($('#live-reload-install-hint'), {
hidden: !liveReloadEnabled,
textContent: t(`liveReloadInstallHint${tabId >= 0 ? 'FF' : ''}`),
});
};
async function check(opts) {
try {
update(await getData(opts));
} catch (err) {
console.warn(t('liveReloadError', err));
}
timer ??= setTimeout(check, DELAY);
}
async function start(opts) {
if (fsh
&& (fso || (fso = global.FileSystemObserver) && (fso = new fso(() => debounce(check, 20))))) {
try {
await fso.observe(fsh);
timer = false; // disables polling
} catch {
timer = null;
}
}
check(opts);
}
function stop() {
if (timer) {
timer = clearTimeout(timer);
} else if (fso) {
fso.disconnect();
}
}
function update(code) {
if (code == null) return;
sequence = sequence.catch(console.error).then(() => {
const {id} = installed || installedDup;
const scrollInfo = cm.getScrollInfo();
const cursor = cm.getCursor();
cm.setValue(code);
cm.setCursor(cursor);
cm.scrollTo(scrollInfo.left, scrollInfo.top);
return API.usercss.install({id, sourceCode: code})
.then(updateMeta)
.catch(showError);
});
}
}
function shouldShowConfig() {
// TODO: rewrite message-box to support multiple instances or find an existing tiny library
const prev = cfgShown;
cfgShown = $(CFG_SEL) != null;
return prev && !cfgShown;
}
function replaceChildren(el, children, toggleParent) {
if (el.firstChild) el.textContent = '';
if (children) el.append(...Array.isArray(children) ? children : [children]);
if (toggleParent) el.parentNode.hidden = !el.firstChild;
}