-
Notifications
You must be signed in to change notification settings - Fork 191
/
options.js
79 lines (62 loc) · 1.92 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
class Options {
constructor () {
this.https = false;
this.host = null;
this.port = 35729;
this.snipver = null;
this.ext = null;
this.extver = null;
this.mindelay = 1000;
this.maxdelay = 60000;
this.handshake_timeout = 5000;
var pluginOrder = [];
Object.defineProperty(this, 'pluginOrder', {
get () { return pluginOrder; },
set (v) { pluginOrder.push.apply(pluginOrder, v.split(/[,;]/)); }
});
}
set (name, value) {
if (typeof value === 'undefined') {
return;
}
if (!isNaN(+value)) {
value = +value;
}
this[name] = value;
}
}
Options.extract = function (document) {
for (const element of Array.from(document.getElementsByTagName('script'))) {
var src = element.src;
var srcAttr = element.getAttribute('src');
var lrUrlRegexp = /^([^:]+:\/\/([^/:]+)(?::(\d+))?\/|\/\/|\/)?([^/].*\/)?z?livereload\.js(?:\?(.*))?$/;
// ^proto:// ^host ^port ^// ^/ ^folder
var lrUrlRegexpAttr = /^(?:(?:([^:/]+)?:?)\/{0,2})([^:]+)(?::(\d+))?/;
// ^proto ^host/folder ^port
var m = src.match(lrUrlRegexp);
var mm = srcAttr.match(lrUrlRegexpAttr);
if (m && mm) {
const [, , host, port, , params] = m;
const [, , , portFromAttr] = mm;
const options = new Options();
options.https = element.src.indexOf('https') === 0;
options.host = host;
options.port = port
? parseInt(port, 10)
: portFromAttr
? parseInt(portFromAttr, 10)
: options.port;
if (params) {
for (const pair of params.split('&')) {
var keyAndValue;
if ((keyAndValue = pair.split('=')).length > 1) {
options.set(keyAndValue[0].replace(/-/g, '_'), keyAndValue.slice(1).join('='));
}
}
}
return options;
}
}
return null;
};
exports.Options = Options;