-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
145 lines (127 loc) · 3.06 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title>Page Sandbox</title>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
<base target="_blank">
<style>
body {
padding: 0 0.5em;
}
.box {
margin-top: 1em;
}
#txtURL {
width: 100%;
height: 2em;
text-indent: 0.5em;
padding: 0.25em 0;
}
#btnGo {
width: 100%;
font-size: 1.5em;
}
#list a {
margin: 1em;
}
</style>
</head>
<body>
<div class="box">
<input id="txtURL" type="text" value="www.google.com" autofocus>
</div>
<div class="box">
<button id="btnGo">Go</button>
</div>
<div class="box">
<span>切换线路:</span>
<select id="selNode"></select>
</div>
<div class="box" id="list">
</div>
<script>
const PAGE_CONF_SET = 110
const PAGE_CONF_GET = 111
const SW_CONF_RETURN = 112
const SW_CONF_CHANGE = 113
const PAGE_READY_CHECK = 200
const SW_READY = 201
const sw = navigator.serviceWorker
sw.addEventListener('message', onSwMsg)
sendMsgToSw(PAGE_READY_CHECK)
btnGo.onclick = function() {
const text = txtURL.value.trim()
if (text) {
const url = './-----' + text
open(url, '_blank', 'noopener,noreferrer')
}
}
txtURL.onkeypress = function(e) {
if (e.keyCode === 13) {
btnGo.onclick()
}
}
txtURL.setSelectionRange(0, txtURL.value.length)
function onSwMsg(e) {
const [cmd, msg] = e.data
switch (cmd) {
case SW_CONF_RETURN:
conf = msg
showConf()
break
case SW_CONF_CHANGE:
conf = msg
updateSelected()
break
case SW_READY:
console.log('sw ready')
sendMsgToSw(PAGE_CONF_GET)
break
}
}
function onSwFail(err) {
txtURL.value = err
}
selNode.onchange = function() {
const item = this.options[this.selectedIndex]
const node = item.value
conf.node_default = node
sendMsgToSw(PAGE_CONF_SET, conf)
}
function sendMsgToSw(cmd, val) {
const ctl = sw.controller
if (!ctl) {
console.log('ctl is null')
return
}
ctl.postMessage([cmd, val])
}
function addNodeItem(id, text) {
const optEl = document.createElement('option')
optEl.id = '--' + id
optEl.text = text
optEl.value = id
selNode.appendChild(optEl)
}
function updateSelected() {
const id = conf.node_default
const item = document.getElementById('--' + id)
if (item) {
item.selected = true
} else {
console.warn('unknown node:', id)
}
}
function showConf() {
for (const [id, node] of Object.entries(conf.node_map)) {
if (!node.hidden) {
addNodeItem(id, node.label)
}
}
updateSelected()
}
</script>
</body>
</html>