-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinjection.html
126 lines (104 loc) · 2.95 KB
/
injection.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
<style>
html,
body {
padding: 0;
margin: 0;
}
</style>
<script>
// 参考 [ninja33/mdx-server](https://github.com/ninja33/mdx-server)
document.addEventListener("DOMContentLoaded", () => {
const audio_type = {
mp3: "audio/mpeg",
mp4: "audio/mp4",
wav: "audio/wav",
spx: "audio/ogg",
ogg: "audio/ogg",
};
function audio_content_type(ext) {
return audio_type[ext] || "audio/mpeg";
}
const getAudioEl = (() => {
let audioEl;
return () => {
if (audioEl) return audioEl;
audioEl = document.querySelector("audio");
if (!audioEl) audioEl = document.createElement("audio");
return audioEl;
};
})();
// 修复sound链接发音问题
function fixSound() {
const soundElements = document.querySelectorAll('a[href^="sound://"]');
soundElements.forEach((el) => {
el.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
const href = el.getAttribute("href");
const src = href.substring("sound:/".length);
const audio = getAudioEl();
audio.setAttribute("src", src);
const type = audio_content_type(href.slice(-3));
audio.setAttribute("type", type);
try {
audio.play();
} catch (err) {
console.error(err);
}
});
});
}
// 修复entry链接跳转问题
function fixEntry() {
const entryElements = document.querySelectorAll('a[href^="entry://"]');
entryElements.forEach((el) => {
el.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
const href = el.getAttribute("href");
window.location = href.substring("entry:/".length);
});
});
}
const parentWin = window.parent || window.top;
// 双击跳转查词
document.addEventListener("dblclick", () => {
const select = document.getSelection().toString().trim();
if (/^\w+$/g.test(select)) {
parentWin.postMessage({ select }, { targetOrigin: "*" });
}
});
const left = [",", "<", ",", "《", "ArrowLeft"];
const right = [".", ">", "。", "》", "ArrowRight"];
// 模拟浏览器历史,快捷键前进后退
document.addEventListener("keyup", (e) => {
let jump = undefined;
if (left.includes(e.key)) jump = -1;
else if (right.includes(e.key)) jump = 1;
if (jump === undefined) return;
e.preventDefault();
e.stopPropagation();
parentWin.postMessage({ jump }, { targetOrigin: "*" });
});
// TODO: 等待词典其他的js文件执行结束? 是否存在优化空间
setTimeout(() => {
fixSound();
fixEntry();
}, 500);
});
</script>
<!--
<a
id="jump-link"
target="_blank"
href=""
>跳转</a>
<style>
#jump-link {
position: fixed;
right: 4px;
top: 50vh;
z-index: 100;
}
</style>
-->