-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathindex_v3.html
168 lines (149 loc) · 3.63 KB
/
index_v3.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!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')
showIcons()
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])
}
const SITE_LIST = [
['google', ''],
['youtube', ''],
['twitter', 'twitter.com/google'],
['flickr', ''],
['quora', 'www.quora.com/topic/JavaScript-programming-language'],
['twitch', 'www.twitch.tv/'],
['reddit', ''],
['wiki', 'zh.wikipedia.org/'],
['gist', 'gist.github.com/'],
['facebook', 'facebook.com/Google/'],
['blogger', ''],
]
function showIcons() {
list.innerHTML = SITE_LIST.map(v => {
let [id, url] = v
url = url || `www.${id}.com/`
return `\
<a rel="noopener noreferrer" href=./-----https://${url}>\
<img width=128 height=128 src=__sys__/assets/ico/${id}.png></a>`
}).join('')
}
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>