UNPKG

1.97 kBJavaScriptView Raw
1class Options {
2 constructor () {
3 this.https = false;
4 this.host = null;
5 this.port = 35729;
6
7 this.snipver = null;
8 this.ext = null;
9 this.extver = null;
10
11 this.mindelay = 1000;
12 this.maxdelay = 60000;
13 this.handshake_timeout = 5000;
14
15 var pluginOrder = [];
16
17 Object.defineProperty(this, 'pluginOrder', {
18 get () { return pluginOrder; },
19 set (v) { pluginOrder.push.apply(pluginOrder, v.split(/[,;]/)); }
20 });
21 }
22
23 set (name, value) {
24 if (typeof value === 'undefined') {
25 return;
26 }
27
28 if (!isNaN(+value)) {
29 value = +value;
30 }
31
32 this[name] = value;
33 }
34}
35
36Options.extract = function (document) {
37 for (const element of Array.from(document.getElementsByTagName('script'))) {
38 var src = element.src;
39 var srcAttr = element.getAttribute('src');
40 var lrUrlRegexp = /^([^:]+:\/\/([^/:]+)(?::(\d+))?\/|\/\/|\/)?([^/].*\/)?z?livereload\.js(?:\?(.*))?$/;
41 // ^proto:// ^host ^port ^// ^/ ^folder
42 var lrUrlRegexpAttr = /^(?:(?:([^:/]+)?:?)\/{0,2})([^:]+)(?::(\d+))?/;
43 // ^proto ^host/folder ^port
44
45 var m = src.match(lrUrlRegexp);
46 var mm = srcAttr.match(lrUrlRegexpAttr);
47
48 if (m && mm) {
49 const [, , host, port, , params] = m;
50 const [, , , portFromAttr] = mm;
51 const options = new Options();
52
53 options.https = element.src.indexOf('https') === 0;
54
55 options.host = host;
56 options.port = port
57 ? parseInt(port, 10)
58 : portFromAttr
59 ? parseInt(portFromAttr, 10)
60 : options.port;
61
62 if (params) {
63 for (const pair of params.split('&')) {
64 var keyAndValue;
65
66 if ((keyAndValue = pair.split('=')).length > 1) {
67 options.set(keyAndValue[0].replace(/-/g, '_'), keyAndValue.slice(1).join('='));
68 }
69 }
70 }
71
72 return options;
73 }
74 }
75
76 return null;
77};
78
79exports.Options = Options;