ScreenTranslator/translators/bing.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-03-21 01:44:46 +07:00
var lastText = '';
var active = window.location.href !== "about:blank";
function checkFinished() {
if (!active) return;
let area = document.querySelector('#tta_output_ta')
if (!area)
return;
let text = area.value.trim();
if (text === lastText || text === lastText + ' ...' || text === '' || text === '...')
return;
console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
lastText = text;
active = false;
proxy.setTranslated(text);
2015-10-10 03:33:49 +07:00
}
2020-03-21 01:44:46 +07:00
function translate(text, from, to) {
console.log('start translate', text, from, to)
2021-01-05 21:03:11 +07:00
if (text.trim().length == 0) {
proxy.setTranslated('');
return;
}
2020-03-21 01:44:46 +07:00
active = true;
if (window.location.href.indexOf('bing.com/translator') !== -1
&& window.location.href.indexOf('&to=' + to + '&') !== -1) {
2021-01-05 21:03:11 +07:00
var input = document.querySelector('textarea#tta_input_ta');
if (input.value == text) {
console.log('using cached result');
lastText = '';
return;
}
input.value = text;
input.dispatchEvent(new Event("input", { bubbles: true, cancelable: true }));
2018-10-17 23:33:35 +07:00
return;
}
2020-04-03 22:38:59 +07:00
let url = 'https://www.bing.com/translator/?from=auto&to=' + to + '&text=' + encodeURIComponent(text);
2020-03-21 01:44:46 +07:00
console.log("setting url", url);
2020-04-03 22:38:59 +07:00
window.location = url;
2020-03-21 01:44:46 +07:00
2015-10-10 03:33:49 +07:00
}
2020-03-21 01:44:46 +07:00
function init() {
proxy.translate.connect(translate);
setInterval(checkFinished, 300);
2015-10-10 03:33:49 +07:00
}