-
Notifications
You must be signed in to change notification settings - Fork 5
/
content.js
296 lines (272 loc) · 11.1 KB
/
content.js
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
var window_id = -1;
var previous_selection = "";
var flag_update_shape_of_div = false;
var flag_close_div_click = false;
var mouse_down_time = new Date().getTime();
const language_map = new Map([
["en", "English"],
["zh", "Chinese (Simplified)"],
["es", "Spanish"],
["fr", "French"],
["de", "German"],
["ja", "Japanese"],
["pt", "Portuguese"],
["ru", "Russian"],
["ar", "Arabic"],
["ko", "Korean"],
["it", "Italian"],
["nl", "Dutch"]
]);
var last_click_for_reqeust = Date.now();
// load local key
async function getLocalParameter(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const json = await response.json();
return json;
}
// creat div show
async function CreateDiv() {
let heightOfDiv = await readLocalStorage('heightOfDiv', "20%");
var div = document.createElement("div");
div.id = 'my_overlay'
div.style.position = "fixed";
div.style.display = "none";
div.style.width = "20%";
div.style.height = heightOfDiv;
div.style.top = '10px';
div.style.right = '10px';
div.style.backgroundColor = "rgba(255,255,255,1)";
div.style.color = 'rgba(0,0,0,1)';
div.style.zIndex = "999999999";
div.style.padding = "10px"; // 增加内边距
div.style.overflow = "auto"; // 允许内容滚动
div.style.resize = 'vertical'; // make div scalable
div.style.overflow = 'auto'; // make div scalable
div.style.boxShadow = "1px 1px 10px 5px gray" // 阴影
div.innerHTML = "Loading...";
document.body.insertBefore(div, document.body.firstChild);
document.getElementById("my_overlay").style.fontSize = 'medium';
}
if (document.getElementById("my_overlay") === undefined) {
(async() => {
CreateDiv();
})();
}
// show div
function openOverlay(){
if (document.getElementById("my_overlay").style.display != "block") {
document.getElementById("my_overlay").style.display = "block";
}
};
// close div
function removeOverlay(){
if (document.getElementById("my_overlay").style.display != "none") {
document.getElementById("my_overlay").style.display = "none";
document.getElementById("my_overlay").innerHTML = "Loading...";
}
};
// judge click is in range of div
function isInTargetDiv(evt) {
var rect = document.getElementById("my_overlay").getBoundingClientRect();
if (evt.clientY >= rect.top && evt.clientY <= rect.bottom &&
evt.clientX >= rect.left && evt.clientX <= rect.right) {
return true;
} else {
return false;
}
}
// try to udpate the shape of div
function updateShapeOfDiv() {
var current_height = document.getElementById("my_overlay").getBoundingClientRect().height;
let current_related_height = ((current_height / window.innerHeight * 100) | 0);
if (current_related_height > 95) {
current_related_height = 95;
}
let current_related_height_str = current_related_height + "%"
chrome.storage.local.set({ 'heightOfDiv': current_related_height_str });
}
// request self-hosted service to get translation
async function requestTranslation(selection, target_language) {
// let gitignore_path = chrome.runtime.getURL("env.gitignore");
// res_json = await getLocalParameter(gitignore_path);
// unknown_variable = atob(res_json['unknown_variable']);
// let url = "https://translation.googleapis.com/language/translate/v2?key=" + unknown_variable.split('_')[1]; // Modified
console.log("Requestting translate service...");
let url = "https://translate.informal.top";
let response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ // Modified
target: target_language,
source: "auto",
q: selection
}),
});
let response_json = await response.json();
return response_json;
}
// read local parameter
const readLocalStorage = async (key, default_value = undefined) => {
return new Promise((resolve, reject) => {
chrome.storage.local.get([key], function (result) {
if (result[key] === undefined) {
// reject();
resolve(default_value);
} else {
resolve(result[key]);
}
});
});
};
// request dictionary, Thanks https://dictionaryapi.dev/
async function get_output_from_word_translation(selection) {
let word_request_url = 'https://api.dictionaryapi.dev/api/v2/entries/en/' + selection
let response = await fetch(word_request_url, { method: "GET" });
let response_json = await response.json();
var output = "<b>" + selection + " " + response_json[0]['phonetic'] + "</b>" + "<br>"
limitation_per_partOfSpeech = 3;
for (var i = 0; i < response_json[0]['meanings'].length; i++) {
let currenct = response_json[0]['meanings'][i];
for (var j = 0; j < Math.min(limitation_per_partOfSpeech, currenct['definitions'].length); j++) {
let definition = currenct['definitions'][j]['definition'];
output += "[" + currenct['partOfSpeech'] + "] " + definition + "<br>"
}
}
output += "<a href=" + word_request_url + ">More>></a>";
return output;
}
function check_valid_selection(selection) {
// match numbers
const digits = selection.match(/[\d]/g)?.join('') || "";
// special letter
if (selection.length <= 1) return false;
spe_cnt = digits.length;
// selection.length > 0 && selection != ' '
const spe_list = ["[", "`", "!", "@", "#", "$", "%", "^", "&", "*",
"(", ")", "_", "+", "-", "=", "[", "\\", "]", "{", "}", ";", "'", ":", '"', "|", ",", ".",
"<", ">", "/", "?", "~", "]", " "];
for (const element of selection) {
if (spe_list.includes(element)) {
if(selection.length <= 3) {
return false;
}
spe_cnt += 1;
}
}
const SpeChatPercentage = (spe_cnt / selection.length);
// console.log(SpeChatPercentage, selection.length, spe_cnt, digits.length);
if (selection.length >= 3 && SpeChatPercentage > 0.6) return false;
if (selection.length >= 5 && SpeChatPercentage > 0.5) return false;
if (selection.length >= 10 && SpeChatPercentage > 0.4) return false;
if (selection.length >= 20 && SpeChatPercentage > 0.3) return false;
return true;
}
function getChineseRatio(str) {
const chineseRegex = /[\u4e00-\u9fa5]/g;
const chineseMatches = str.match(chineseRegex);
if (!chineseMatches) {
return 0;
}
const chineseCount = chineseMatches.length;
const totalCount = str.length;
return chineseCount / totalCount;
}
// trigger when mouse up
window.addEventListener('mouseup', function (evt) {
mouse_up_time = new Date().getTime();
var current_click_for_request = Date.now();
if (flag_update_shape_of_div === true) {
updateShapeOfDiv();
}
// console.log("window.getSelection(): ", window.getSelection());
// console.log("window.getSelection().tostring(): ", window.getSelection().toString());
// let {anchorNode, anchorOffset, focusNode, focusOffset} = window.getSelection();
// console.log(anchorNode);
// console.log("window.getSelection().anchorNode.innerText: ", window.getSelection().anchorNode.innerText);
// console.log("window.getSelection().anchorNode.baseURI: ", window.getSelection().anchorNode.baseURI);
let selection = window.getSelection().toString();
let baseURI = window.getSelection().anchorNode.baseURI;
let anchorNodeInnerText = window.getSelection().anchorNode.innerText;
if (anchorNodeInnerText !== undefined && baseURI.includes('www.reddit.com')) { // speical process for reddit comment
selection = anchorNodeInnerText;
}
let chinese_ratio = getChineseRatio(selection);
var seletion_flag = check_valid_selection(selection);
// console.log('mouse time diff: ', mouse_up_time, mouse_down_time, mouse_up_time - mouse_down_time);
// console.log("seletion_flag: ", seletion_flag, "selection: ", selection);
if (seletion_flag === true && isInTargetDiv(evt) === false &&
(flag_close_div_click === false // this click for close div
|| mouse_up_time - mouse_down_time >= 200 // this click for select text again, one click not double clicks
)
) {
previous_selection = selection;
(async() => {
let extensionOn = await readLocalStorage('extensionOn', true);
if (extensionOn !== true) {
return ;
}
// get language from local storage
var target_language = await readLocalStorage('selectedLanguage', 'zh');
let raw_language = 'en', translation = '';
if (target_language === 'zh' && chinese_ratio > 0.2) {
raw_language = 'zh';
} else {
let response_json = await requestTranslation(selection, target_language);
translation = response_json['translatedText'];
if (translation.endsWith(".")) {
translation = translation.substring(0, translation.length - 1);
}
raw_language = response_json['detectedLanguage']['language'];
}
// single word
if (!selection.includes(' ') && raw_language === 'en') {
var dictionaryFeature = await readLocalStorage('dictionaryFeature', false);
// process single word for dictionary
if (dictionaryFeature === true) {
openOverlay();
try {
let output = await get_output_from_word_translation(selection);
translation += "<br>" + output; // show target language && dictionary
}
catch(err) {
console.log('get localDictionaryFeature error');
}
}
}
// show
if (raw_language != target_language && current_click_for_request > last_click_for_reqeust) {
openOverlay();
// let suffix = language_map.get(raw_language) + " -> " + language_map.get(target_language)
document.getElementById("my_overlay").innerHTML = translation; // + "<br>" + suffix;
last_click_for_reqeust = current_click_for_request;
}
})();
}
});
// close div when mouse up
window.addEventListener('mousedown', function (evt) {
mouse_down_time = new Date().getTime();
if (document.getElementById("my_overlay") === null) { // creat new div when there isn't, becuase chatGPT.com will delete my div, maybe this's related to generated website
(async() => {
CreateDiv();
})();
}
if (!isInTargetDiv(evt)) {
flag_update_shape_of_div = false;
if (document.getElementById("my_overlay").style.display === "block") {
flag_close_div_click = true;
} else {
flag_close_div_click = false;
}
removeOverlay();
} else {
flag_close_div_click = false;
flag_update_shape_of_div = true;
}
});
// gray