// ==UserScript== // @name iciba划词翻译 // @namespace noe132.com // @author noe132 // @include http://* // @include https://* // @exclude http://www.iciba.com* // @grant GM_xmlhttpRequest // @grant GM_addStyle // @grant GM_getValue // @grant GM_setValue // @grant GM_registerMenuCommand // @icon http://tb.himg.baidu.com/sys/portrait/item/d4346e6f65313332ac06 // @version 3.2.1 // @supportURL http://tieba.baidu.com/f?kw=firefox // @contributionURL nnnnoe132@gmail.com|alipay.com // @description iciba翻译 // @downloadURL none // ==/UserScript== /* ------------------ changelog ------------------- * update INFO: * update 2017/04/11 : bug fix,重写鼠标事件定位方法 * update 2017/03/24 : 添加鼠标事件10ms延迟 * update 2017/03/24 : 用babel翻译async function,可以支持到chrome33 * update 2017/03/23 : 使用百度翻译API,iciba的程序员不更新api * update 2016/12/23 : trim查询字符串,更新版本号到3 * update 2016/09/23 : mouseClick只能左键触发 * update 2016/08/19 : 不知为何connect属性未加上 * update 2016/05/30 : 样式修改 * update 2016/05/03 : 解释处理修改 * update 2016/05/02 : 样式修改 * update 2016/05/01 : 样式修改 * update 2016/04/22 : 老版本浏览器兼容性修复 * update 2016/04/17 : 样式修改 * update 2016/04/17 : 重写 * update 2016/04/09 : 样式小改 * update 2016/03/27 : 紧急修复。。。 * update 2016/03/16 : 样式修正 * update 2016/03/05 : 样式小改 * update 2015/07/31 : 貌似之前的wordpress判断不太给力,干脆带wp-admin的都干掉 * update 2015/07/12 : 去除wp-admin/post.php页面,防止wordpress编辑文章自动添加div * update 2015/05/19 : 设置查词默认为小写 * update 2015/05/09 : 按Ctrl可以使小蓝圈隐藏起来(暂时) * update 2015/01/03 : 样式全加了 !important 提高优先级 * update 2014/12/24 : 添加设置选项(可当且仅当按住ctrl键显示翻译按钮) * update 2014/12/20 : 修正语法错误。。。 * update 2014/12/19 : 搜词BUG修复,修正点击定位~ * update 2014/12/10 : 严格模式'use strict',提高性能 * update 2014/12/04 : 贴吧图册预览修正 * update 2014/11/16 : 更换mouseClick的触发条件为mouseup * update 2014/11/14 : 添加输入框查词,修正定位不正确。 * ------------------------------------------------ */ 'use strict'; function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } var Iciba = function () { this.init(); }; Iciba.prototype.auto_active = 1; //没用~ Iciba.prototype.ctrlKey_actived = null; // init 初始化对象 插入样式 读取设置 绑定事件 Iciba.prototype.init = function () { var _this = this; GM_addStyle('#icibaCirclePointer{font-family:"Microsoft Yahei",serif,sans-serif!important;display:block!important;z-index:10000!important;overflow:hidden!important;width:20px!important;height:20px!important;border:1px solid #555!important;border-radius:100px!important;background:#ABD!important;box-shadow:0 0 3px #111!important;opacity:.7!important;transition:.05s!important;box-sizing:border-box!important}#icibaCirclePointer:hover{opacity:1!important}#icibaCirclePointer:active{border:1px solid #353535!important;background:#90a2c4!important;box-shadow:0 0 3px #222!important}#icibaResultContainer{z-index:10000!important;width:auto!important;height:auto!important;max-width:320px!important;border:none!important;background:#EEE!important;box-shadow:0 0 8px 1px #999!important;letter-spacing:0!important}#icibaResultContainer *{box-sizing:border-box!important}#iciba_search_box{display:block!important;margin:0!important;padding:0!important;width:100%!important;border-bottom:1px solid #CCC!important;font-size:0!important;letter-spacing:0!important;word-spacing:0!important}#icibaResultContainer input{position:static!important;margin:0!important;border:none!important;border-radius:0!important;box-shadow:none!important;box-sizing:border-box!important;display:inline-block!important;vertical-align:middle!important;float:none!important}#iciba_search_box>#icibaSearchInput{padding:0 7px!important;width:calc(100% - 29px)!important;height:28px!important;line-height:28px!important;background:white!important;color:#666!important;font-size:14px!important;letter-spacing:0!important;font-family:"Microsoft Yahei",serif,sans-serif!important}#icibaSearchInput:hover,#icibaSearchInput:focus{box-shadow:0 0 3px 0 #CCC inset!important;color:#222!important}#icibaSearchButton{padding:0!important;width:29px!important;height:28px!important;border-left:1px solid #CCC!important;border-radius:0!important;background-color:#EEE!important;background-image:url("data:image/png!important;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAAAEgAAABIAEbJaz4AAAAJdnBBZwAAABAAAAAQAFzGrcMAAADcSURBVCjPhdG9TgJhEIXh58M1EGEFjEohiUGk4f4vBaSy0UYRBFEIP8JaLEvWhOjbTc6bzMlMSPxNRICGpopgZejJJosTIRGCtjtLE1uxmrGeZSZEaGh5NzBH5Na9jp7ksMKNtQcL8O1R2bXYLBUKqBjv45QXJ86yoYBg96v4zr55JqzECjmhamedF4ZqWoe4runTJHcHzy60lb3aqmo6tVHJSoZEoKTjSoSdL5GShZ5ZdqhUPVcWrEzVdRXN9X3khTyXuooW+sm04BgjAyux+7TkMd4kWkaE/979AxXnQ1s0DN1UAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDEwLTAyLTExVDExOjUwOjA4LTA2OjAw1hBl+wAAACV0RVh0ZGF0ZTptb2RpZnkAMjAwNi0wNS0wNVQxMzoyMjo0MC0wNTowML/k/hoAAAAASUVORK5CYII=")!important;background-position:6px 6px!important;background-repeat:no-repeat!important}#icibaSearchButton:hover{background-color:#fafafa!important}#icibaSearchButton:active{background-color:#e5e5e5!important;box-shadow:0 0 2px #888 inset!important}#icibaResultTextBox{margin:0!important;padding:10px!important;color:#222!important;text-align:left!important;font-size:14px!important;font-family:Arial,Helvetica,sans-serif!important;background:#fbfbfb!important}.icIBahyI-new_word{display:none!important}.icIBahyI-label_list>LABEL{display:inline!important}#icibaResultTextBox div,#icibaResultTextBox ul,#icibaResultTextBox li,#icibaResultTextBox p,#icibaResultTextBox form,#icibaResultTextBox input,#icibaResultTextBox label{margin:0!important;padding:0!important;font-weight:normal!important}#icibaResultTextBox input,#icibaResultTextBox button,#icibaResultTextBox textarea{font-weight:inherit!important;font-style:inherit!important;font-size:inherit!important;font-family:Arial,Helvetica,sans-serif!important}#icibaResultTextBox ul,#icibaResultTextBox li{list-style:none!important}#icibaResultTextBox a:focus{outline:none!important}#icibaResultTextBox img{border:0!important}#icibaResultTextBox strong{font-weight:100!important;font-family:"Segoe UI",Verdana,Lucida Sans Regular,Lucida Sans Unicode,Arial,sans-serif!important}#icibaResultTextBox .icIBahyI-dictbar{font-size:12px!important}#icibaResultTextBox .icIBahyI-register,#icibaResultTextBox .icIBahyI-right_bg,#icibaResultTextBox .icIBahyI-up,#icibaResultTextBox .icIBahyI-down{float:right!important}#icibaResultTextBox .icIBahyI-register{padding-left:25px!important}#icibaResultTextBox a.icIBahyI-text_black,#icibaResultTextBox .icIBahyI-nav_list strong{color:#333!important}#icibaResultTextBox a{color:#236fd4!important;text-decoration:underline!important}#icibaResultTextBox a:hover{color:#236fd4!important}#icibaResultTextBox .icIBahyI-handwrite,#icibaResultTextBox .icIBahyI-handwrite_over,#icibaResultTextBox .icIBahyI-drop_down{position:absolute!important;width:16px!important;height:16px!important;cursor:pointer!important}#icibaResultTextBox .icIBahyI-handwrite{top:14px!important;right:130px!important;background-position:0 -70px!important}#icibaResultTextBox .icIBahyI-handwrite_over{top:14px!important;right:130px!important;background-position:-18px -70px!important}#icibaResultTextBox #icIBahyI-dict_main{padding:0!important;text-align:left!important}#icibaResultTextBox .icIBahyI-dictbar{display:block!important;padding:0!important;height:auto!important;line-height:1.2!important}#icibaResultTextBox .icIBahyI-title{position:relative!important;padding:16px 17px 0 18px!important;color:#333!important;font-weight:bold!important;font-size:24px!important;font-family:arial,sans-serif!important;line-height:32px!important;word-break:break-all!important}#icibaResultTextBox .icIBahyI-dict_title{color:#333!important;font-weight:bold!important;font-family:arial,sans-serif!important}#icibaResultTextBox .icIBahyI-prons{display:block!important;padding:0!important;height:auto!important;white-space:normal!important;font-weight:700!important}#icibaResultTextBox .icIBahyI-dictbar .icIBahyI-fl{vertical-align:middle!important;font-size:12px!important;line-height:normal!important}#icibaResultTextBox .icIBahyI-dictbar .icIBahyI-fl *{font-size:12px!important}#icibaResultTextBox .icIBahyI-ico_sound{text-decoration:none!important;border:none!important;display:inline-block!important;overflow:hidden!important;margin:0!important;width:16px!important;height:16px!important;background:url("data:image/png!important;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACK0lEQVQ4jZWTQUuUURSGn3Pu/e6opM5kjYRUk2mZOWC0KqL8AS1cBBUUOf9Af4LbIJRo0yJs0V4hWhTUuC+SAslSclxESMM06cw4OvN9t8VUSgrZ2b8Pz3sOR7z3nLr6ZFpEh1F9LKpjH2euF9nnKICoGb505QKp7tSIdW6579r0yP8BjGW9XAfbTk9vX7ytPT7Vf+Nptv/ms8F9AgxhJHwv1Vgt1mnv6CJ55MSQdU1zZ289nxi4/SL+rwqEHkQVL8J6tUYNS2dXN4lDyVFjg+X0nZd71to28IKoIEYRo3iFSm2L5tZ2jp1MxVva2qbSmWw2nckO7gKghnoEYhQ1AkbZioSNOnxbr/O1GBLvTHK85+iQi7m5dCY7kR55Fd9RQQkjwYtQ2vSUqp4QRQOLa7YYZyhWIkp1S+9AioOH46OIjABYABFDedOzthFhnSFwBusMNjAYq4g0dJuadLumGv4AUKFa86hRbNAIBzHbAAWKc0oyEVBZq/B5cZVqpTr+/tHlyR0GShAI9VAxgWKdwcUMsWZLR8JxIAaLC3mKhfKsj3zm3cOLud9LbBgg2EBRIoxRrFXaWgOSHY5Cocqn+R85H4Zjb+8Pzvx9Rvsrj7WChoILlK5kDB+FfJjPs1ljXNROvr57es//aAC8x1hIxAISccuXlTzlSjgrxmbe3DuT2yu4C9ASEyqVMksLpZwYOzb34Pwu3b1GATx+dmUpR361MO7D8Nx+wwA/Ad9dudwSn5Q6AAAAAElFTkSuQmCC")!important;transition:.1s!important;vertical-align:middle!important}#icibaResultTextBox .icIBahyI-ico_sound:hover{opacity:.75!important}#icibaResultTextBox .icIBahyI-ico_sound:active{opacity:.9!important}#icibaResultTextBox .icIBahyI-eg,#icibaResultTextBox .icIBahyI-us{padding-top:0!important;font-family:"lucida sans unicode",arial!important;display:block!important}#icibaResultTextBox .icIBahyI-new_word{float:left!important;padding-top:2px!important}#icibaResultTextBox .icIBahyI-new_word a{display:block!important;padding:3px 10px 0 20px!important;width:35px!important;height:17px!important;background-position:-274px 0!important;color:#999!important;font-weight:100!important;font-family:"Microsoft Yahei"!important}#icibaResultTextBox .icIBahyI-new_word a.icIBahyI-new_word2{background-position:-274px -19px!important}#icibaResultTextBox .icIBahyI-usually,#icibaResultTextBox .icIBahyI-prons{color:#666!important;font-weight:100!important}#icibaResultTextBox .icIBahyI-simple{margin:5px 0 5px 0!important}#icibaResultTextBox .icIBahyI-group_prons{clear:both!important;margin:0!important}#icibaResultTextBox .icIBahyI-group_prons .icIBahyI-second{float:none!important;padding-top:0!important}#icibaResultTextBox .icIBahyI-group_prons .icIBahyI-second .icIBahyI-eg{height:24px!important}#icibaResultTextBox .icIBahyI-group_pos{clear:both!important;overflow:hidden!important;color:#222!important;font-size:14px!important}#icibaResultTextBox .icIBahyI-group_pos *{font-size:14px!important}#icibaResultTextBox .icIBahyI-group_pos p:not(:last-of-type){margin-bottom:5px!important}#icibaResultTextBox .icIBahyI-label_list{float:left!important;padding-bottom:9px!important;width:87%!important;text-align:left!important}#icibaResultTextBox .icIBahyI-cn .icIBahyI-group_pos{color:#333!important}#icibaResultTextBox .icIBahyI-group_pos strong{color:#8d8d8d!important}#icibaResultTextBox .icIBahyI-margin_top{padding-top:6px!important}#icibaResultTextBox .icIBahyI-part_list a,#icibaResultTextBox .icIBahyI-tab_list li a,#icibaResultTextBox .icIBahyI-new_word a,#icibaResultTextBox .icIBahyI-part_main h3 a{text-decoration:none!important}#icibaResultTextBox .icIBahyI-tab_list{display:none!important}#icibaResultTextBox .icIBahyI-title{padding:13px 10px 1px!important;font-size:14px!important}#icibaResultTextBox .icIBahyI-group_pos{font-size:14px!important}#icibaResultTextBox .icIBahyI-group_pos .icIBahyI-fl{padding:0 6px 0 0!important;color:#999!important}#icibaResultTextBox .icIBahyI-group_pos span{vertical-align:baseline!important;line-height:normal!important;float:none!important;display:inline!important;padding:0!important}#icibaResultTextBox .icIBahyI-synon{color:#333!important;line-height:28px!important}#icibaResultTextBox .icIBahyI-suggest,#icibaResultTextBox .icIBahyI-suggest2{padding:0 0 3px 0!important;color:#333!important;word-wrap:break-word!important;font-size:14px!important;line-height:22px!important}#icibaResultTextBox .icIBahyI-suggest ul{padding:7px 0 4px!important;line-height:155%!important}#icibaResultTextBox .icIBahyI-suggest li{padding-left:14px!important;background-position:-134px -304px!important}#icibaResultTextBox .icIBahyI-suggest p{line-height:250%!important}#icibaResultTextBox .icIBahyI-suggest .icIBahyI-pr a{padding-right:3px!important}#icibaResultTextBox .icIBahyI-suggest p.icIBahyI-pr{margin-top:3px!important;line-height:18px!important}#icibaResultTextBox .icIBahyI-suggest .icIBahyI-pr a{display:inline-block!important;padding-right:10px!important}#icibaResultTextBox .icIBahyI-footer{padding:0!important;width:auto!important;color:#999!important;text-align:right!important;font-size:12px!important;line-height:18px!important}#icibaResultTextBox .icIBahyI-footer a{border:none!important;text-decoration:none!important;padding:0 3px 0 0!important;background:none!important;color:#236fd4!important;text-decoration:none!important;font-size:12px!important;line-height:18px!important}#icibaResultTextBox .icIBahyI-footer a:hover{color:#236fd4!important}#icibaResultTextBox .icIBahyI-text_blue{color:#236fd4!important}#icibaResultTextBox .icIBahyI-list li{padding:0 15px!important}#icibaResultTextBox .icIBahyI-li_dt,.icIBahyI-li_dd{display:inline!important;line-height:24px!important}#icibaResultTextBox .icIBahyI-li_dt{padding-right:5px!important;color:#232323!important}#icibaResultTextBox .icIBahyI-li_dd{color:#777!important}#ICIBA_TOO_LONG{padding-top:10px!important;padding-left:10px!important;font-size:12px!important}'); GM_registerMenuCommand('iciba翻译设置', this.openSetting); _this.loadSetting(); _this.eventBinding(); }; // loadSetting 读取设置 Iciba.prototype.loadSetting = function () { // Ctrl键触发 var _this = this; if (GM_getValue('ctrlKey_actived') === undefined) { _this.ctrlKey_actived = 0; } else { _this.ctrlKey_actived = parseInt(GM_getValue('ctrlKey_actived')); } GM_setValue('ctrlKey_actived', _this.ctrlKey_actived); }; // eventBinding 绑定事件 Iciba.prototype.eventBinding = function () { var _this = this; window.addEventListener('mouseup', function (e) { setTimeout(() => { _this._mouseClick(e, _this); }, 10); }, false); window.addEventListener('keydown', function (e) { _this._keyDown(e, _this); }, false); }; // openSetting 设置对话框 Iciba.prototype.openSetting = function () { var _this = this; _this.ctrlKey_actived = confirm('按住ctrl键(当且仅当)开启翻译?') ? 1 : 0; GM_setValue('ctrlKey_actived', _this.ctrlKey_actived); }; // showIcibaCirclePointer 显示并定位小圆点 Iciba.prototype.showIcibaCirclePointer = function (e) { var _this = this; let { top, left } = _this.getPosition(e); _this.icibaCirclePointer = document.createElement('div'); _this.icibaCirclePointer.id = 'icibaCirclePointer'; _this.icibaCirclePointer.style.position = 'absolute'; _this.icibaCirclePointer.style.top = top + 7 + 'px'; _this.icibaCirclePointer.style.left = left + 5 + 'px'; _this.icibaCirclePointer.setAttribute('keyword', window.getSelection().toString().toLowerCase().trim()); _this.icibaCirclePointer.addEventListener('click', function (e) { _this.showContainer(e, _this); }, false); document.body.appendChild(_this.icibaCirclePointer); }; // removeCirclePointer 去除小圆点 Iciba.prototype.removeCirclePointer = function () { var _this = this; if (_this.icibaCirclePointer) { document.body.removeChild(_this.icibaCirclePointer); } _this.icibaCirclePointer = null; }; // showContainer 显示并定位查词框 Iciba.prototype.showContainer = function (e, _this) { var word = _this.icibaCirclePointer.getAttribute('keyword'); _this.removeCirclePointer(); _this.createContainer(); _this.containerSetPosition(e); _this.containerLoadData(word); // getData(word,e,bodyClientHeight,bodyClientWidth,windowInnerHeight,windowInnerWidth,htmlClientHeight,htmlClientWidth); }; // createContainer 创建查词框 Iciba.prototype.createContainer = function () { var _this = this; _this.icibaResultContainer = document.createElement('div'); _this.icibaResultContainer.id = 'icibaResultContainer'; _this.icibaResultContainer.style.position = 'absolute'; _this.icibaResultContainer.innerHTML = '\ \
'; document.body.appendChild(_this.icibaResultContainer); _this.icibaResultTextBox = document.getElementById('icibaResultTextBox'); _this.icibaSearchInput = document.getElementById('icibaSearchInput'); _this.icibaSearchButton = document.getElementById('icibaSearchButton'); _this.icibaSearchInput.addEventListener('keypress', function (e) { _this.searchFromInputBox(e, _this); }, false); _this.icibaSearchButton.addEventListener('click', function (e) { _this.searchFromInputBox(e, _this); }, false); }; // removeContainer 去除查词框 Iciba.prototype.removeContainer = function () { var _this = this; if (_this.icibaResultContainer) { document.body.removeChild(_this.icibaResultContainer); _this.icibaResultContainer = null; _this.icibaResultTextBox = null; _this.icibaSearchInput = null; _this.icibaSearchButton = null; } }; // containerSetPosition 定位查词框 Iciba.prototype.containerSetPosition = function (e) { var _this = this; let { de, top, left } = _this.getPosition(e); if (de[de.base].clientHeight - top < 200) { // TODO using bottom position _this.icibaResultContainer.style.top = 'auto'; _this.icibaResultContainer.style.bottom = de[de.base].clientHeight - top + 'px'; } else { _this.icibaResultContainer.style.top = top + 'px'; _this.icibaResultContainer.style.bottom = 'auto'; } if (de[de.base].clientWidth - left < 300) { // using right position _this.icibaResultContainer.style.left = 'auto'; _this.icibaResultContainer.style.right = de[de.base].clientWidth - left + 'px'; } else { _this.icibaResultContainer.style.left = left + 'px'; _this.icibaResultContainer.style.right = 'auto'; } }; // containerLoadData 获取数据查词 Iciba.prototype.containerLoadData = function (word) { var _this = this; _this.icibaResultTextBox.innerHTML = 'Loading......'; _this.icibaSearchInput.value = word; var get_iciba_result = function () { return new Promise(function (rs, rj) { GM_xmlhttpRequest({ method: 'GET', referer: 'http://www.iciba.com/', url: 'http://open.iciba.com/huaci/dict.php?word=' + word, timeout: 10000, ontimeout: function () { rj({ status: 1, message: '网络超时!' }); }, onerror: function () { rj({ status: 1, message: '网络错误!' }); }, onload: function (response) { if (response.status != 200) { rj({ status: 1, message: '网络错误!' }); } var text = response.responseText.replace(/\\/g, ''); if (text.indexOf('api.fanyi.baidu.com') !== -1) { rs({ status: 0, translation: 1, data: text }); return; } text = text.match(/dict\.innerHTML=\'(.*)\'/)[1]; text = text.replace(/icIBahyI-'ico_sound'/g, '"icIBahyI-ico_sound"'); rs({ status: 0, translation: 0, data: text }); } }); }); }; var get_lang_detect = function (word) { var lang_detect_formData = new FormData(); lang_detect_formData.append('query', word); return new Promise(function (rs, rj) { GM_xmlhttpRequest({ method: 'POST', referer: 'http://fanyi.baidu.com', url: 'http://fanyi.baidu.com/langdetect', data: lang_detect_formData, timeout: 5000, ontimeout: function () { rj({ status: 1, message: '网络超时!' }); }, onerror: function () { rj({ status: 1, message: '网络错误!' }); }, onload: function (response) { if (response.status != 200) { rj({ status: 1, message: '网络错误!' }); } var result = JSON.parse(response.responseText); if (result.error === 0) { rs(result.lan); } else { rj({ status: 1, message: '翻译文本语言未知!' }); } } }); }); }; var get_translation_result = function (lang_detect, target_lang) { var translation_formData = new FormData(); translation_formData.append('from', lang_detect); translation_formData.append('to', target_lang); translation_formData.append('query', word); translation_formData.append('source', 'txt'); return new Promise(function (rs, rj) { GM_xmlhttpRequest({ method: 'POST', referer: 'http://fanyi.baidu.com', url: 'http://fanyi.baidu.com/transapi', data: translation_formData, timeout: 5000, ontimeout: function () { rj({ status: 1, message: '网络超时!' }); }, onerror: function () { rj({ status: 1, message: '网络错误!' }); }, onload: function (response) { if (response.status != 200) { rj({ status: 1, message: '网络错误!' }); } var result = JSON.parse(response.responseText); if (result.type === 2 && result.status === 0) { rs(result.data[0].dst); } else { rj({ status: 1, message: '翻译出错!' }); } } }); }); }; var get_translation = (() => { var _ref = _asyncToGenerator(function* (word) { try { var iciba_result = yield get_iciba_result(); if (iciba_result.translation === 0) { _this.icibaResultTextBox.innerHTML = iciba_result.data; var playbtn = document.querySelectorAll('.icIBahyI-ico_sound'); if (playbtn.length != 0) { for (var i = 0; i < playbtn.length; i++) { playbtn[i].setAttribute('mp3', playbtn[i].getAttribute('onclick').match(/asplay_hanci\('(.*)'\)/)[1]); playbtn[i].removeAttribute('onclick'); playbtn[i].addEventListener('click', _this.playSound, false); } } } else { var lang_detect = yield get_lang_detect(word); var target_lang = lang_detect === 'zh' ? 'en' : 'zh'; var translation_result = yield get_translation_result(lang_detect, target_lang); _this.icibaResultTextBox.innerHTML = translation_result; } } catch (e) { var message = e.message; _this.icibaResultTextBox.innerHTML = message; } }); return function get_translation(_x) { return _ref.apply(this, arguments); }; })(); get_translation(word); }; // get_google_translate_token 获取google translate token Iciba.prototype.get_google_translate_token = function (word) { /* eslint-disable */ function sM(a) { var b; if (null !== yr) b = yr;else { b = wr(String.fromCharCode(84)); var c = wr(String.fromCharCode(75)); b = [b(), b()]; b[1] = c(); b = (yr = window[b.join(c())] || "") || ""; } var d = wr(String.fromCharCode(116)), c = wr(String.fromCharCode(107)), d = [d(), d()]; d[1] = c(); c = "&" + d.join("") + "="; d = b.split("."); b = Number(d[0]) || 0; for (var e = [], f = 0, g = 0; g < a.length; g++) { var l = a.charCodeAt(g); 128 > l ? e[f++] = l : (2048 > l ? e[f++] = l >> 6 | 192 : (55296 == (l & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (l = 65536 + ((l & 1023) << 10) + (a.charCodeAt(++g) & 1023), e[f++] = l >> 18 | 240, e[f++] = l >> 12 & 63 | 128) : e[f++] = l >> 12 | 224, e[f++] = l >> 6 & 63 | 128), e[f++] = l & 63 | 128); } a = b; for (f = 0; f < e.length; f++) a += e[f], a = xr(a, "+-a^+6"); a = xr(a, "+-3^+b+-f"); a ^= Number(d[1]) || 0; 0 > a && (a = (a & 2147483647) + 2147483648); a %= 1E6; return c + (a.toString() + "." + (a ^ b)); } var yr = null; var wr = function (a) { return function () { return a; }; }, xr = function (a, b) { for (var c = 0; c < b.length - 2; c += 3) { var d = b.charAt(c + 2), d = "a" <= d ? d.charCodeAt(0) - 87 : Number(d), d = "+" == b.charAt(c + 1) ? a >>> d : a << d; a = "+" == b.charAt(c) ? a + d & 4294967295 : a ^ d; } return a; }; return sM(word); /* eslint-enable */ }; // conflictsResolve 不同网站的冲突解决 Iciba.prototype.conflictsResolve = function () { if (window.location.href.indexOf('http://tieba.baidu.com/photo/p?kw=') === 1) { GM_addStyle('.af_container{position:relative;}'); } }; // searchFromInputBox 查词框取词事件 Iciba.prototype.searchFromInputBox = function (e, _this) { if (e.target === _this.icibaSearchInput) { if (e.keyCode != 13) { return; } } var keyword = _this.icibaSearchInput.value; _this.containerLoadData(keyword); }; // playSound 发声 Iciba.prototype.playSound = function (e) { var audio = document.createElement('audio'); var source = document.createElement('source'); source.type = 'audio/mpeg'; source.src = e.target.getAttribute('mp3'); source.autoplay = 'autoplay'; source.controls = 'controls'; audio.appendChild(source); audio.play(); }; // getPosition 计算鼠标事件对于元素应去的top left值 Iciba.prototype.getPosition = function (e) { // e.clienX e.clientY 是相对于浏览器view port的位置(当前窗口) // e.pageX e.pageY 是相对于当前页面的位置(页面最左上角) // position:absolute 以父元素content-box的左上角定位 // html 和 body 都为 static 时用 页面最左上角定位(需要考虑html的margin-top) // html 和 body 似乎不会发生margin-collapse // margin collapse 只发生在 margin-top 和 margin-bottom var de = { page: { clientHeight: document.documentElement.scrollHeight, clientWidth: document.documentElement.scrollHeight, offsetTop: e.pageY, offsetLeft: e.pageX }, body: { rect: document.body.getBoundingClientRect(), clientHeight: document.body.clientHeight, clientWidth: document.body.clientWidth, clientTop: document.body.clientTop, // top border width clientLeft: document.body.clientLeft, // left border width position: document.defaultView.getComputedStyle(document.body)['position'] }, html: { rect: document.documentElement.getBoundingClientRect(), // coordinates relative to the viewport origin, of the top of the rectangle box clientHeight: document.documentElement.clientHeight, // inner height of an element in pixels, including padding but not the horizontal scrollbar clientWidth: document.documentElement.clientWidth, // inner width of an element in pixels. It includes padding but not the vertical scrollbar clientTop: document.documentElement.clientTop, // top border width clientLeft: document.documentElement.clientLeft, // left border width position: document.defaultView.getComputedStyle(document.documentElement)['position'] }, window: { scrollY: window.scrollY, // number of pixels that the document has already been scrolled vertically. scrollX: window.scrollX, // number of pixels that the document has already been scrolled horizontally. InnerHeight: window.innerHeight, // viewport height InnerWidth: window.innerWidth }, base: 'page' }; // formula from jquery.offset de.body.offsetTop = e.pageY - (de.body.rect.top + de.window.scrollY + de.body.clientTop); de.body.offsetLeft = e.pageX - (de.body.rect.left + de.window.scrollX + de.body.clientLeft); de.html.offsetTop = e.pageY - (de.html.rect.top + de.window.scrollY + de.html.clientTop); de.html.offsetLeft = e.pageX - (de.html.rect.left + de.window.scrollX + de.html.clientLeft); if (de.html.position !== 'static') { de.base = 'html'; } else if (de.body.position !== 'static') { de.base = 'body'; } return { top: de[de.base].offsetTop, left: de[de.base].offsetLeft, de: de }; }; // _keyDown keydown 事件处理函数 Iciba.prototype._keyDown = function (e, _this) { if (e.key === 'Control' && e.keyCode === 17) { if (!_this.ctrlKey_actived) { _this.removeCirclePointer(); } } }; // _isInsideOf 判断是否在内部 Iciba.prototype._isInsideOf = function (e, target) { // when target is not exist if (!target) { return false; } var e_target = e.target; while (e_target != target && e_target) { e_target = e_target.parentNode; } if (e_target === target) { return true; } else { return false; } }; // _mouseClick mousedown 事件处理函数 Iciba.prototype._mouseClick = function (e, _this) { // console.log('pageX:' + e.pageX + ',pageY:' + e.pageY + ',clientX:' + e.clientX + ',clientY:' + e.clientY) // ignore when click on icibaCirclePointer if (e.target.id === 'icibaCirclePointer') { return; } if (e.button != 0) { return; } // ignore when click insideof icibaResultContainer if (_this._isInsideOf(e, _this.icibaResultContainer)) { return; } // Ctrl键触发 if (_this.ctrlKey_actived) { if (!(e.ctrlKey === true && e.shiftKey === false && e.altKey === false)) { _this.removeCirclePointer(); _this.removeContainer(); return; } } // remove all things if (_this.icibaResultContainer) { _this.removeContainer(); } if (_this.icibaCirclePointer) { _this.removeCirclePointer(); } // 显示iciba_icon if (window.getSelection().toString().length >= 150) { return; // ignore when selection is too loing } if (window.getSelection().toString().length !== 0) { _this.showIcibaCirclePointer(e); return; } // 去除iciba_icon if (window.getSelection().toString().length === 0) { _this.removeCirclePointer(); } return; }; { new Iciba(); }