-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBingContentScript.js
100 lines (86 loc) · 3.06 KB
/
BingContentScript.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
/*
Javascript that is injected into bing translator
gets text to translate and returns translation
*/
var main = (function () {
"use strict";
// this will be called when message listener fires
function onMessageListener(message, sender, sendResponse) { // eslint-disable-line no-unused-vars
if (message.messageType == "SyosetuGoogle-TextToTranslate") {
translateText(message);
}
};
function addMessageListener() {
try {
// note, this will throw if not running as an extension.
if (!chrome.runtime.onMessage.hasListener(onMessageListener)) {
chrome.runtime.onMessage.addListener(onMessageListener);
}
} catch (chromeError) {
alert(chromeError);
}
}
function setTranslationOptions() {
setBingLanguage("select#tta_srcsl", "ja");
setBingLanguage("select#tta_tgtsl", "en");
}
function setBingLanguage(selector, value){
let select = document.querySelector(selector);
let options = [...select.getElementsByTagName("option")];
for(let i = 0; i < options.length; ++i){
if(options[i].value === value){
select.selectedIndex = i;
return;
}
}
}
function translateText(message) {
let oldText = fetchTranslatedTextFromBing();
insertTextTotranslateIntoBing(message.toTranslate);
waitForTranslation(oldText, message);
}
function waitForTranslation(oldText, message) {
return sleep(1000).then(function () {
let newText = fetchTranslatedTextFromBing();
if ((newText !== oldText) && (newText !== " ...")
&& (newText !== (oldText + " ..."))) {
clearTranslatedTextFromBing();
return returnTranslatedText(newText, message.textID);
} else {
return waitForTranslation(oldText, message);
}
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function insertTextTotranslateIntoBing(toTranslate) {
let textArea = window.document.querySelector("textarea#tta_input_ta");
textArea.value = "";
textArea.focus();
document.execCommand("insertText", false /*no UI*/, toTranslate);
}
function getTranslatedTextArea() {
return window.document.querySelector("textarea#tta_output_ta");
}
function fetchTranslatedTextFromBing() {
return getTranslatedTextArea().value;
}
function clearTranslatedTextFromBing() {
getTranslatedTextArea().value = "";
}
function doPrep() {
addMessageListener();
setTranslationOptions();
}
function returnTranslatedText(translated, textID) {
let message = {
messageType: "SyosetuGoogle-TranslatedText",
translated: translated,
textID: textID
};
chrome.runtime.sendMessage(message);
}
doPrep();
returnTranslatedText(null, null); // sentinal to start process
})();