-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.js
59 lines (53 loc) · 1.83 KB
/
module.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
const getSelectionText = () => {
if (window.getSelection) {
return window.getSelection().toString();
}
if (document.selection && document.selection.type != "Control") {
return document.selection.createRange().text;
}
};
const copy = (text) => {
const el = document.createElement("textarea");
el.value = text;
el.setAttribute("readonly", "");
el.style.cssText = "position: absolute; left: -9999px";
document.body.appendChild(el);
el.select();
document.execCommand("copy"); // new API requests permission too frequently
document.body.removeChild(el);
};
const openSidebar = (url, q, andEscape = "%26") => {
const tar = url + encodeURI(q).replaceAll("&", andEscape);
window.open(tar, "_blank", "whale-sidebar");
};
const translate = (arg) => {
const url = {
google: "https://translate.google.com/?sl=auto&tl=ko&text=",
papago: "https://papago.naver.com/?sk=auto&tk=ko&st=",
kakaoi: "https://translate.kakao.com/?q=",
bing: "https://www.bing.com/translator?from=auto&to=ko&text=",
set: "https://papago.naver.com/?sk=auto&tk=ko&st=", // 번역기들 fallback
yandex: "https://translate.yandex.com/?lang=en-ko&text=",
flitto:
"https://ko.flitto.com/language/translation/text?src_lang_id=17&dst_lang_id=33&lang_id=33&q=",
samsung: "https://translate.samsung.com/?text=",
deepl: "https://www.deepl.com/translator#auto/ko/",
};
const andEscape = {
// & 문자에 특수 처리해야 하는 사이트
papago: "%25amp",
};
whale.storage.sync.get(
{
Service: "papago",
Copy: 0,
},
(items) => {
let q = arg ? arg : getSelectionText();
if (items.Copy) copy(q);
let service = items.Service;
openSidebar(url[service], q, andEscape[service]);
}
);
};
export { translate };