-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontent_script.ts
156 lines (145 loc) · 4.06 KB
/
content_script.ts
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
type SupportLang = "zh" | "en";
const translator = document.createElement("div");
translator.setAttribute("id", "translatorExtensionContainer");
translator.classList.add("translatorExtension");
const translatorTip = document.createElement("div");
translatorTip.classList.add("translatorExtension");
const globalStyle = document.createElement("style");
globalStyle.innerHTML = `
.translatorExtension {
font-family: Segoe UI, "Segoe UI Web (West European)", -apple-system,
BlinkMacSystemFont, Roboto, Helvetica Neue, sans-serif;
display: none;
padding: 4px;
position:absolute;
z-index:999999999;
max-width: 30vw;
line-height: 1.5em;
border: 1px solid #000;
background: #fff;
color: #000;
font-size: 15px;
}
.translatorExtensionTheNewUI {
background: transparent;
border:1px solid;
backdrop-filter: blur(18px);
color: inherit;
font-size: inherit;
}
`;
document.head.appendChild(globalStyle);
setStyle(translatorTip, {
position: "fixed",
display: "flex",
justifyContent: "center",
bottom: "-40vh",
left: "0",
right: "0",
margin: "0 auto",
width: "100%",
transition: "all 80ms",
visibility: "hidden",
});
chrome.runtime.sendMessage({ type: "ui" }, (resp) => {
if (resp) {
translator.classList.add("translatorExtensionTheNewUI");
translatorTip.classList.add("translatorExtensionTheNewUI");
}
});
document.body.appendChild(translator);
document.body.appendChild(translatorTip);
chrome.storage.onChanged.addListener(({ UISwitch }) => {
if (UISwitch) {
if (UISwitch.newValue) {
translator.classList.add("translatorExtensionTheNewUI");
translatorTip.classList.add("translatorExtensionTheNewUI");
} else {
translator.classList.remove("translatorExtensionTheNewUI");
translatorTip.classList.remove("translatorExtensionTheNewUI");
}
}
});
document.addEventListener("mouseup", (evt) => {
if ((evt.target as HTMLElement).id !== "translatorExtensionContainer") {
chrome.runtime.sendMessage(
{
type: "check",
},
(resp) => {
if (resp) {
const text = document.getSelection()?.toString();
if (text?.trim()) {
const [originLanguage, targetLanguage] = getLangOriginAndTarget(
text,
);
chrome.runtime.sendMessage({
type: "deepl",
text: text.replaceAll("/", "\\/"),
from: originLanguage,
to: targetLanguage,
}, (resp) => {
setStyle(translator, {
display: "block",
top: `${evt.pageY}px`,
left: `${evt.pageX}px`,
});
translator.textContent = resp;
});
}
}
},
);
}
});
document.addEventListener("mousedown", (evt) => {
if ((evt.target as HTMLElement).id !== "translatorExtensionContainer") {
translator.innerHTML = "";
setStyle(translator, {
display: "none",
});
}
});
document.addEventListener("keyup", (evt) => {
if (
(evt.ctrlKey && evt.keyCode === 18) ||
(evt.altKey && evt.keyCode === 17)
) {
chrome.runtime.sendMessage(
{
type: "toggleGlobal",
},
(resp) => {
const text = resp ? "划词翻译已开启" : "划词翻译已关闭";
translatorTip.innerText = text;
setStyle(translatorTip, {
bottom: "8px",
visibility: "visible",
});
const st = setTimeout(() => {
setStyle(translatorTip, {
bottom: "-40vh",
visibility: "hidden",
});
clearTimeout(st);
}, 2000);
},
);
}
});
function isChinese(text: string): boolean {
return /[\u4E00-\u9FA5\uF900-\uFA2D]/.test(text);
}
function setStyle(e: HTMLElement, s: Partial<CSSStyleDeclaration>): void {
for (const k in s) {
const v = s[k];
if (typeof v === "string") {
e.style[k] = v;
}
}
}
function getLangOriginAndTarget(text: string): [SupportLang, SupportLang] {
const o: SupportLang = isChinese(text) ? "zh" : "en";
const t: SupportLang = o === "en" ? "zh" : "en";
return [o, t];
}