fixed google translate

This commit is contained in:
example 2019-01-04 16:19:25 +08:00
parent 62999674be
commit 02a29c4727

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name Translate // @name Translate
// @namespace http://tampermonkey.net/ // @namespace http://tampermonkey.net/
// @version 3.3 // @version 3.4
// @description 划词翻译调用“必应词典必应翻译、有道词典有道翻译、Google Translate谷歌翻译、Microsoft Translator必应在线翻译、金山词霸、海词词典、百度翻译、Oxford Learner's Dictionaries、Oxford Dictionaries、Merriam-Webster”网页翻译 // @description 划词翻译调用“必应词典必应翻译、有道词典有道翻译、Google Translate谷歌翻译、Microsoft Translator必应在线翻译、金山词霸、海词词典、百度翻译、Oxford Learner's Dictionaries、Oxford Dictionaries、Merriam-Webster”网页翻译
// @author https://github.com/barrer // @author https://github.com/barrer
// @match http://*/* // @match http://*/*
@ -70,16 +70,14 @@
new Drag(backAndForwardDiv); new Drag(backAndForwardDiv);
} }
}; };
var iconArray = [ var iconArray = [{
{
name: 'Bing 词典', name: 'Bing 词典',
image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAABgFBMVEX///8NhIQNgoIWiYn8/v4RhYUcjIwUh4cOhIQhjo4pkpLt9vb7/f3z+fkwlpZYqqrV6urq9fWr1dUlkJCYy8uEwcHi8PA1mJjd7u49nJx7vLz5/PxAnp72+/vg7+9dra05mpq329twtrYtlJSgz89IoqKNxMTw9/d1urrn8/NBoaFps7P0+voZiorI4+PC4OBOpKTJ5OS12dnN5eW63Ny83d2a09Pl9fXl8vJksLCBv7+Lzc1UqKjF4uIPhYXZ6+uk0dGRx8eFyclfsbENgYGx19cRg4MOgoLQ5+cei4sOgIDy+Pjd8fFSp6dcq6vZ7Owtk5Nwvb294uLS6Oi+3t4MhIQumJjL6em8398Oh4dNoqI7nJwhkZFarq5hra1Wqqo7l5cPhIQOfX1VqalhuLim3d16xcUslpZUqqofkJAajIwmlJS95+c4paWy2NiWzc0PiIgch4e929u139+z398nm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/4J6X758hXfJ+CigB2SvAQW5LQMoya0kwCAhVFAkpE0uaA4wBqjE/9fBnU54kgCGAtpznkC6CiewOgAHAu2O+5GoibUrA6wBDAKmWeI8EqSLPcrrZpwDgPNL2/Pdjjfp3PuOR8/EJAHYAKeaJAzuA7B4r0//UarxwIQTADEC2pk94uIjNIyArlxcEbGZgmyQWJwFYAQj5KbgJwApAUKofx4EdYHvwlQbADCC7ZhYA2AHE3AAAOwCZ9TWAvkEBjRVdSoGRghtoIKLmhsBYIfc9pI57tAaG8wbzlAsPSSx5wYyG91YJxfjnlRMwqVDsQ7/Do/joBzCxv4rYV+u1bxpgapG60s+JrxIB0ytEsbdWFfddsCJPWsSbI1xi3AHWxC+5br7/OKxMgHW19prYNcU9B5aWXPZ3AXAPCmB5owkOOyeoMQ1sSMsp2ClxfwRsyhcWCOriMj6wr1C62S4QlOkG2Jlj/E/hGjDfTHvA7k7uS9dfKYaBQc7g5VdJwV9KAptOM4Isc1O7wCxvRR20Ytv5F1aNwb4yuFDOAAAAAElFTkSuQmCC',
host: ['cn.bing.com'], host: ['cn.bing.com'],
popup: function (text) { popup: function (text) {
popupCenter('https://cn.bing.com/dict/search?q=' + encodeURIComponent(text), null, 800, screen.height); popupCenter('https://cn.bing.com/dict/search?q=' + encodeURIComponent(text), null, 800, screen.height);
}, },
custom: function (text) { custom: function (text) {}
}
}, },
{ {
name: '有道词典', name: '有道词典',
@ -88,8 +86,7 @@
popup: function (text) { popup: function (text) {
popupCenter('https://dict.youdao.com/w/eng/' + encodeURIComponent(text), null, 800, screen.height); popupCenter('https://dict.youdao.com/w/eng/' + encodeURIComponent(text), null, 800, screen.height);
}, },
custom: function (text) { custom: function (text) {}
}
}, },
{ {
name: 'Google Translate', name: 'Google Translate',
@ -103,7 +100,6 @@
source.value = text; source.value = text;
tiggerEvent(source, 'input'); tiggerEvent(source, 'input');
tiggerEvent(source, 'keyup'); tiggerEvent(source, 'keyup');
document.querySelector('#gt-submit').click();
} }
}, },
{ {
@ -129,8 +125,7 @@
popup: function (text) { popup: function (text) {
popupCenter('http://www.iciba.com/' + encodeURIComponent(text), null, 800, screen.height); popupCenter('http://www.iciba.com/' + encodeURIComponent(text), null, 800, screen.height);
}, },
custom: function (text) { custom: function (text) {}
}
}, },
{ {
name: '海词词典', name: '海词词典',
@ -139,8 +134,7 @@
popup: function (text) { popup: function (text) {
popupCenter('https://dict.cn/' + encodeURIComponent(text), null, 800, screen.height); popupCenter('https://dict.cn/' + encodeURIComponent(text), null, 800, screen.height);
}, },
custom: function (text) { custom: function (text) {}
}
}, },
{ {
name: '百度翻译', name: '百度翻译',
@ -149,8 +143,7 @@
popup: function (text) { popup: function (text) {
popupCenter('https://fanyi.baidu.com/#en/zh/' + encodeURIComponent(text), null, 1024, screen.height); popupCenter('https://fanyi.baidu.com/#en/zh/' + encodeURIComponent(text), null, 1024, screen.height);
}, },
custom: function (text) { custom: function (text) {}
}
}, },
{ {
name: 'Oxford Learner\'s Dictionaries', name: 'Oxford Learner\'s Dictionaries',
@ -188,9 +181,10 @@
document.querySelector('.s-frm').submit(); document.querySelector('.s-frm').submit();
} }
} }
], hostCustomMap = {}; ],
hostCustomMap = {};
iconArray.forEach(function (obj) { iconArray.forEach(function (obj) {
obj.host.forEach(function (host) {// 赋值DOM加载后的自定义方法Map obj.host.forEach(function (host) { // 赋值DOM加载后的自定义方法Map
hostCustomMap[host] = obj.custom; hostCustomMap[host] = obj.custom;
}); });
}); });
@ -201,7 +195,7 @@
} }
if (window.location.host in hostCustomMap) { if (window.location.host in hostCustomMap) {
document.onkeydown = escExit; document.onkeydown = escExit;
dataTransfer.backAndForward();// add back and forward button dataTransfer.backAndForward(); // add back and forward button
} }
log('url: ' + window.location.href); log('url: ' + window.location.href);
@ -215,8 +209,8 @@
img.setAttribute('alt', obj.name); img.setAttribute('alt', obj.name);
img.setAttribute('title', obj.name); img.setAttribute('title', obj.name);
img.addEventListener('mouseup', function () { img.addEventListener('mouseup', function () {
if (iconDrag.elementOriginalLeft == parseInt(icon.style.left) if (iconDrag.elementOriginalLeft == parseInt(icon.style.left) &&
&& iconDrag.elementOriginalTop == parseInt(icon.style.top))// 没有拖动鼠标抬起的时候触发点击事件 iconDrag.elementOriginalTop == parseInt(icon.style.top)) // 没有拖动鼠标抬起的时候触发点击事件
dataTransfer.beforePopup(obj.popup); dataTransfer.beforePopup(obj.popup);
}); });
img.setAttribute('style', '' + img.setAttribute('style', '' +
@ -247,7 +241,7 @@
document.documentElement.appendChild(icon); document.documentElement.appendChild(icon);
// 鼠标事件:防止选中的文本消失 // 鼠标事件:防止选中的文本消失
document.addEventListener('mousedown', function (e) { document.addEventListener('mousedown', function (e) {
if (e.target == icon || (e.target.parentNode && e.target.parentNode == icon)) {// 点击了翻译图标 if (e.target == icon || (e.target.parentNode && e.target.parentNode == icon)) { // 点击了翻译图标
e.preventDefault(); e.preventDefault();
} }
}); });
@ -260,7 +254,7 @@
}); });
// 鼠标事件:防止选中的文本消失;显示、隐藏翻译图标 // 鼠标事件:防止选中的文本消失;显示、隐藏翻译图标
document.addEventListener('mouseup', function (e) { document.addEventListener('mouseup', function (e) {
if (e.target == icon || (e.target.parentNode && e.target.parentNode == icon)) {// 点击了翻译图标 if (e.target == icon || (e.target.parentNode && e.target.parentNode == icon)) { // 点击了翻译图标
e.preventDefault(); e.preventDefault();
return; return;
} }
@ -308,11 +302,11 @@
/**触发事件*/ /**触发事件*/
function tiggerEvent(el, type) { function tiggerEvent(el, type) {
if ('createEvent' in document) {// modern browsers, IE9+ if ('createEvent' in document) { // modern browsers, IE9+
var e = document.createEvent('HTMLEvents'); var e = document.createEvent('HTMLEvents');
e.initEvent(type, false, true);// event.initEvent(type, bubbles, cancelable); e.initEvent(type, false, true); // event.initEvent(type, bubbles, cancelable);
el.dispatchEvent(e); el.dispatchEvent(e);
} else {// IE 8 } else { // IE 8
var e = document.createEventObject(); var e = document.createEventObject();
e.eventType = type; e.eventType = type;
el.fireEvent('on' + e.eventType, e); el.fireEvent('on' + e.eventType, e);