-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
502 lines (481 loc) · 18.9 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页</title>
<style>
.log{
white-space: pre;
overflow: auto;
}
.comment{
color: green;
opacity: 0.3;
}
.title{
position: sticky;
top: 0;
}
</style>
</head>
<body>
<div>
<div id="time"></div>
<button onclick="refreshLoc()">loc</button>
<button onclick="switchLoc()">switch</button>
<div id="loc" class="log"></div>
<div id="floc" class="log"></div>
<hr>
<div id="localstorage" class="log">localStorage:<ul></ul></div>
<hr>
<input type="checkbox" name="awaking" id="awaking">
<label for="awaking">唤醒</label>
<button id="reawaking" style="display: none;">重新唤醒</button>
<div id="links">
<a href="spaceship.html">小火箭</a>
<a href="waterMark.html">水印制作</a>
<a href="bili.html">bilibili</a>
<a href="百人万元模拟_ECharts.html">百人万元模拟_ECharts</a>
</div>
<input type="checkbox" name="sos" id="sos">
<label for="sos">SOS Light</label>
<span class="comment">(need photo privilege cause the torch api is binded on it)</span>
<div id="sos_log" class="log"></div>
</div>
<script>
o_console_log = console.log;
console.log = function () {
o_console_log.apply(null, [new Date().toLocaleString(), ...arguments]);
};
show_time = () => document
.getElementById('time').innerText = new Date().toLocaleString();
show_time();
setInterval(show_time, 1000);
// 显示localStorage
function rm(key) {
localStorage.removeItem(key);
document.getElementById(key).remove();
}
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
let value_str = localStorage.getItem(key);
try {
let value_obj = JSON.parse(value_str);
value_str = JSON.stringify(value_obj, null, 4);
console.log(key, value_obj);
} catch (error) {
console.log(key, value_str);
}
document.querySelector('#localstorage>ul').innerHTML += (
`<li id='${key}'>${key}: ${value_str} <button onclick="rm('${key}')">删除</button></li>`
)
}
// 唤醒状态切换
awaking = document.getElementById('awaking');
reawaking = document.getElementById('reawaking');
async function awake(){
try {
wakeLock = await navigator.wakeLock.request('screen');
wakeLock.onrelease = () => {
if (awaking.checked) {
console.log('发生非主动释放')
reawaking.style.display = 'block';
}
};
console.log('唤醒成功');
if (reawaking.style.display == 'block') {
reawaking.style.display = 'none';
}
} catch (error) {
awaking.checked = false;
console.log('唤醒失败');
}
}
awaking.addEventListener('change', () => {
if (awaking.checked) {
awake();
} else {
wakeLock.release();
console.log('释放成功');
}
});
reawaking.onclick = awake;
awaking.click();
// 返回页面后恢复唤醒状态
window.addEventListener('visibilitychange', async () => {
console.log(document.visibilityState);
if (document.visibilityState === 'visible') {
if(reawaking.style.display === 'block') reawaking.click();
}
});
</script>
<script>
function jsonStringifyWithComments(obj, infoNode, space = 2) {
const INDENT = ' '.repeat(space);
function stringifyWithComments(value, valueInfoNode, indent = '') {
let comment = '';
if (valueInfoNode && valueInfoNode.__node_info){
comment = `<span class="comment"> // ${valueInfoNode.__node_info}</span>`;
}
if (typeof value === 'object' && value !== null) {
let isArray = Array.isArray(value);
let items = [];
let nextValueInfoNode = {...valueInfoNode, ...{__node_info: null}};
for (let key in value) {
!isArray && (nextValueInfoNode = valueInfoNode[key]);
let itemValue = stringifyWithComments(value[key], nextValueInfoNode, indent + INDENT);
if (isArray) {
items.push(`${indent + INDENT}${itemValue}`);
} else {
items.push(`${indent + INDENT}"${key}": ${itemValue}`);
}
}
if (items.length === 0) {
return isArray ? '[]' : '{}' + comment;
}
if (isArray) {
return `[${comment}\n${items.join('\n')}\n${indent}]`;
} else {
return `{${comment}\n${items.join('\n')}\n${indent}}`;
}
} else if (typeof value === 'string') {
return `"${value},"${comment}`;
} else {
return String(value) + comment;
}
}
return stringifyWithComments(obj, infoNode);
}
infoNode = {
"regeocode": {
"__node_info": "包含详细的地理编码信息",
"addressComponent": {
"__node_info": "描述当前位置的行政区域信息",
"province": {
"__node_info": "省或直辖市名称,例如 '北京市'"
},
"city": {
"__node_info": "市级行政区名称,可能与省同名或者为空,取决于省的类型"
},
"citycode": {
"__node_info": "城市的电话区号"
},
"district": {
"__node_info": "区级行政区域名称"
},
"adcode": {
"__node_info": "行政区域代码"
},
"township": {
"__node_info": "乡镇或街道名称(此字段可选,不是所有请求都有)"
},
"towncode": {
"__node_info": "乡镇/街道编码"
},
"neighborhood": {
"__node_info": "描述社区的相关信息",
"type": {
"__node_info": "社区类型,例如 '住宅区' 或 '商业区'"
}
},
"building": {
"__node_info": "描述建筑物的相关信息",
"type": {
"__node_info": "建筑物类型,例如 '写字楼' 或 '商场'"
}
},
"streetNumber": {
"__node_info": "描述门牌号的相关信息",
"street": {
"__node_info": "街道名称"
},
"number": {
"__node_info": "门牌号码"
},
"location": {
"__node_info": "门牌所在的经纬度坐标"
},
"direction": {
"__node_info": "门牌所在位置相对于请求位置的方向"
},
"distance": {
"__node_info": "门牌到请求位置的距离"
}
},
"seaArea": {
"__node_info": "海域名称(若适用)"
},
"businessAreas": {
"__node_info": "描述商圈的相关信息",
"businessArea": {
"__node_info": "商圈名称"
},
"location": {
"__node_info": "商圈中心的经纬度"
},
"name": {
"__node_info": "商圈中的主要建筑物或地标"
}
}
},
"roads": {
"__node_info": "描述道路的相关信息",
"distance": {
"__node_info": "道路到请求位置的距离"
},
"direction": {
"__node_info": "道路所在方向"
},
"location": {
"__node_info": "道路的经纬度坐标"
}
},
"roadinters": {
"__node_info": "描述道路交叉口的相关信息",
"location": {
"__node_info": "交叉口的经纬度坐标"
},
"distance": {
"__node_info": "交叉口到请求位置的距离"
},
"direction": {
"__node_info": "交叉口所在方向"
},
"first_id": {
"__node_info": "第一条道路的ID"
},
"first_name": {
"__node_info": "第一条道路的名称"
},
},
"pois": {
"__node_info": "描述兴趣点 (POI) 的相关信息",
"id": {
"__node_info": "POI 的唯一标识符"
},
"name": {
"__node_info": "POI 的名称"
},
"type": {
"__node_info": "POI 的类型,例如 '餐厅' 或 '银行'"
},
"tel": {
"__node_info": "POI 的联系电话"
},
"distance": {
"__node_info": "POI 到请求位置的距离"
},
"direction": {
"__node_info": "POI 所在方向"
},
"address": {
"__node_info": "POI 的详细地址"
},
"location": {
"__node_info": "POI 的经纬度坐标"
},
"businessarea": {
"__node_info": "POI 所在的商圈"
}
},
"aois": {
"__node_info": "描述兴趣区域 (AOI) 的相关信息",
"id": {
"__node_info": "AOI 的唯一标识符"
},
"name": {
"__node_info": "AOI 的名称"
},
"adcode": {
"__node_info": "AOI 所在的行政区域代码"
},
"location": {
"__node_info": "AOI 的中心经纬度"
},
"area": {
"__node_info": "AOI 的面积"
},
"distance": {
"__node_info": "AOI 到请求位置的距离"
},
"type": {
"__node_info": "AOI 的类型,例如 '商业' 或 '住宅'"
}
}
}
}
</script>
<script>
function mkUrl(obj) {
let url = obj.base;
let keys = Object.keys(obj.kvs);
for (let i = 0; i < keys.length; i++) {
let k = keys[i];
let v = obj.kvs[k];
if (i == 0) {
url += '?';
}
url = url.concat(k, '=', v);
if (i != keys.length - 1) {
url += '&';
}
}
return url
}
async function gaoDeLoc(gaodeLntLatStr) {
let max_radius = 500;
let requestObj = {
base: 'https://restapi.amap.com/v3/geocode/regeo',
kvs: {
key: '22f56bdaf27f7d13215d03984880ab26',
location: gaodeLntLatStr,
radius: 0,
extensions: 'all',
}
}
let resObj = null;
while(1){
resObj = await fetch(mkUrl(requestObj)).then((res) => res.json())
console.log(`range ${requestObj.kvs.radius} get`, resObj);
if(resObj.status == 1) break;
if (requestObj.kvs.radius <= max_radius) {
requestObj.kvs.radius += 50;
} else break;
}
return resObj
}
async function gaoDeLntLat(longitude, latitude) {
longitude = longitude.toFixed(6);
latitude = latitude.toFixed(6);
let requestObj = {
base: 'https://restapi.amap.com/v3/assistant/coordinate/convert',
kvs: {
key: '22f56bdaf27f7d13215d03984880ab26',
locations: `${longitude},${latitude}`,
coordsys: 'gps',
}
}
gaodeLntLatObj = await fetch(mkUrl(requestObj)).then((res) => res.json())
console.log(gaodeLntLatObj)
return gaodeLntLatObj
}
function refreshLoc() {
navigator.geolocation.getCurrentPosition(
async (position) => {
console.log(position);
locEl.innerHTML = JSON.stringify(position, null, 4);
gdllobj = await gaoDeLntLat(position.coords.longitude, position.coords.latitude)
locEl.innerHTML += '\ngaoDeLntLat: ' + JSON.stringify(gdllobj, null, 4);
resObj = await gaoDeLoc(gdllobj.locations)
locEl.innerHTML += '\ngaoDeLoc: ' + jsonStringifyWithComments(resObj, infoNode, 4);
},
(positionError) => {
console.log(positionError.code, positionError.message)
},
{
enableHighAccuracy: true,
}
);
}
locEl = document.getElementById('loc');
flocEl = document.getElementById('floc');
flocEl.style.display = 'none';
flocEl.innerHTML = '\n'.repeat(100);
refreshLoc();
function switchLoc() {
locEl.style.display = locEl.style.display == 'none' ? 'block' : 'none';
flocEl.style.display = flocEl.style.display == 'none' ? 'block' : 'none';
}
// setInterval(refreshLoc, 5000);
</script>
<script>
sosBtn = document.getElementById('sos');
logEl = document.getElementById('sos_log');
function log(msg) {
logEl.innerHTML += msg + '\n';
}
function clearLog() {
logEl.innerHTML = '';
}
async function getLightCtl() {
// 检查navigator
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
return false;
}
medidaStream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: 'environment'
}
});
clearLog();
tracks = medidaStream.getVideoTracks();
for (let track of tracks) {
log('Track Kind: ' + track.kind);
log('Track Label: ' + track.label);
log('-'.repeat(5));
let capabilities = track.getCapabilities();
log('Capabilities:\n' + JSON.stringify(capabilities, null, 2));
log('-'.repeat(5));
let settings = track.getSettings();
log('Settings:\n' + JSON.stringify(settings, null, 2));
log('-'.repeat(5));
if (capabilities.torch) {
return {
light_on: () => track.applyConstraints({ torch: true }),
light_off: () => track.applyConstraints({ torch: false }),
light_status: () => track.getSettings().torch
}
}
}
}
function release(){
if (medidaStream) {
for (let track of medidaStream.getTracks()) {
track.stop();
}
medidaStream = null;
lightCtl = null;
sosBtn.checked = false;
}
}
async function sleep(ms) {
await new Promise(resolve => setTimeout(resolve, ms));
}
// sos signal array, [light_length, dark_length, light_length, dark_length, ...]
sosSignal = [
500, 500, 500, 500, 500, 500,
1000, 500, 1000, 500, 1000, 500,];
async function sos(lightCtl) {
if (!lightCtl) {
return;
}
lightCtl.light_off();
while (sosBtn.checked) {
for (let ms of sosSignal) {
if (!sosBtn.checked) break;
if (lightCtl.light_status()) lightCtl.light_off();
else lightCtl.light_on();
await sleep(ms);
}
}
}
lightCtl = null;
sosBtn.addEventListener('change', async function () {
if (this.checked) {
if (!lightCtl) lightCtl = await getLightCtl();
if (!lightCtl) {
alert('No torch api found');
release();
return;
}
sos(lightCtl);
} else {
lightCtl.light_off();
release();
}
});
</script>
</body>
</html>