forked from kzahel/web-server-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (93 loc) · 3.78 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
window.reload = chrome.runtime.reload
function addinterfaces() {
var version = getchromeversion()
if (version >= 44) {
chrome.system.network.getNetworkInterfaces( function(result) {
if (result) {
var wport = document.getElementById('choose-port').value;
console.log("port found: " + wport);
var contLocal = document.getElementById('local-interface');
if (typeof contLocal !== 'undefined') {
while (contLocal.firstChild) {
contLocal.removeChild(contLocal.firstChild);
}
var a = document.createElement('a')
a.target = "_blank";
var href = 'http://127.0.0.1:' + wport;
a.innerText = href;
a.href = href;
contLocal.appendChild(a);
} else{
console.log("not contLocal!");
}
var cont = document.getElementById('other-interfaces')
if (typeof cont !== 'undefined') {
while (cont.firstChild) {
cont.removeChild(cont.firstChild);
}
for (var i=0; i<result.length; i++) {
console.log('network interface:',result[i])
if (result[i].prefixLength == 24) {
var a = document.createElement('a')
a.target = "_blank";
var href = 'http://' + result[i].address + ':' + wport;
a.innerText = href;
a.href = href;
cont.appendChild(a);
}
}
} else{
console.log("not cont!");
}
}
})
}
}
chrome.runtime.getBackgroundPage( function(bg) {
console.log('got bg page')
window.bg = bg;
document.getElementById('status').innerText = 'OK'
addinterfaces()
function choosefolder() {
chrome.fileSystem.chooseEntry({type:'openDirectory'}, onchoosefolder)
}
function onchoosefolder(entry) {
if (entry) {
window.entry = entry
bg.entry = entry
bg.haveentry(entry)
var retainstr = chrome.fileSystem.retainEntry(entry)
var d = {'retainstr':retainstr}
chrome.storage.local.set(d)
document.getElementById('curfolder').innerText = d['retainstr']
document.getElementById('status').innerText = 'OK'
console.log('set retainstr!')
}
}
document.getElementById('choose-folder').addEventListener('click', choosefolder)
function onRestart() {
var input = document.getElementById('choose-port');
if (!input) return;
var wport = input.value;
console.log("port found: " + wport);
addinterfaces()
if (bg) {
bg.restart(parseInt(wport));
}
}
document.getElementById('restart').addEventListener('click', onRestart)
chrome.storage.local.get('retainstr',function(d) {
if (d['retainstr']) {
chrome.fileSystem.restoreEntry(d['retainstr'], function(entry) {
if (entry) {
window.entry = entry
bg.entry = entry
bg.haveentry(entry)
} else {
document.getElementById('status').innerText = 'DIRECTORY MISSING. CHOOSE AGAIN.'
}
})
document.getElementById('curfolder').innerText = d['retainstr']
}
})
})