// ==UserScript==
// @name iciba划词翻译
// @namespace noe132.com
// @author noe132
// @include http://*
// @include https://*
// @exclude http://www.iciba.com*
// @exclude /.*wp-admin.*/
// @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 2.20
// @description iciba翻译
// ==/UserScript==
// release version
// last update : 2016/03/05
/*
update INFO:
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/4 : 贴吧图册预览修正
update 2014/11/16 : 更换mouseClick的触发条件为mouseup
update 2014/11/14 : 添加输入框查词,修正定位不正确。
流程说明: // 防止我自己忘记~
鼠标事件触发后,check for ctrlKey_actived
then 显示按钮mouseClick();
点击按钮后,showResultBox();
getData();
setData();
*/
"use strict";
// 添加样式
GM_addStyle('#iciba_click_box{z-index:10000;overflow:hidden;width:20px;height:20px;border:1px solid #555;border-radius:100px;background:#ABD;box-shadow:0 0 3px #111;opacity:0.7;-moz-transition:0.05s;}#iciba_click_box:hover{opacity:1;}#iciba_click_box:active{border:1px solid #353535;background:#90A2C4;box-shadow:0 0 3px #222;}#iciba_wrap{z-index:10000;width:auto;height:auto;max-width:300px;border:1px solid gray;background:#EEE;box-shadow:0 0 3px #999;}#iciba_search_box{display:block;margin:0;padding:0;width:100%;border-bottom:1px solid gray;font-size:0;}#iciba_wrap input{position:static !important;margin:0 !important;border:none !important;border-radius:0 !important;box-shadow:none !important;}#iciba_search_box > #iciba_input{display:inline-block;display:-moz-box;box-sizing:border-box;padding:0 5px;width:calc(100% - 26px);height:25px;background:white;color:#111;vertical-align:middle;font-size:13px;line-height:25px;-moz-transition:0.1s;}#iciba_search_box > #iciba_input:hover,#iciba_search_box > #iciba_input:focus{box-shadow:0 0 3px #BBB inset;}#iciba_search_box > #iciba_searchbtn{display:inline-block;box-sizing:border-box;padding:0;width:26px;height:25px;border-left:1px solid gray;border-radius:0;background-color:#E5E5E5;background-image:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAAAEgAAABIAEbJaz4AAAAJdnBBZwAAABAAAAAQAFzGrcMAAADcSURBVCjPhdG9TgJhEIXh58M1EGEFjEohiUGk4f4vBaSy0UYRBFEIP8JaLEvWhOjbTc6bzMlMSPxNRICGpopgZejJJosTIRGCtjtLE1uxmrGeZSZEaGh5NzBH5Na9jp7ksMKNtQcL8O1R2bXYLBUKqBjv45QXJ86yoYBg96v4zr55JqzECjmhamedF4ZqWoe4runTJHcHzy60lb3aqmo6tVHJSoZEoKTjSoSdL5GShZ5ZdqhUPVcWrEzVdRXN9X3khTyXuooW+sm04BgjAyux+7TkMd4kWkaE/979AxXnQ1s0DN1UAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDEwLTAyLTExVDExOjUwOjA4LTA2OjAw1hBl+wAAACV0RVh0ZGF0ZTptb2RpZnkAMjAwNi0wNS0wNVQxMzoyMjo0MC0wNTowML/k/hoAAAAASUVORK5CYII=");background-position:5px 5px;background-repeat:no-repeat;vertical-align:middle;-moz-transition:0.1s;}#iciba_search_box > #iciba_searchbtn:hover{background-color:#F0F0F0;}#iciba_search_box > #iciba_searchbtn:active{background-color:#DDD;box-shadow:0 0 3px #999 inset;}#iciba_text_box{margin:0;padding:7px 9px 9px 9px;color:black;font-size:13px;}#iciba_text_box a{color:#35C;}.icIBahyI-new_word{display:none;}.icIBahyI-label_list > LABEL{display:inline;}#iciba_text_box{text-align:left;font-size:14px;font-family:Arial,Helvetica,sans-serif;}#iciba_text_box div,#iciba_text_box ul,#iciba_text_box li,#iciba_text_box p,#iciba_text_box form,#iciba_text_box input,#iciba_text_box label{margin:0;padding:0;font-weight:normal;font-size:14px;}#iciba_text_box input,#iciba_text_box button,#iciba_text_box textarea{font-weight:inherit;font-style:inherit;font-size:inherit;font-family:Arial,Helvetica,sans-serif;}#iciba_text_box{background:#fbfbfb;}#iciba_text_box ul,#iciba_text_box li{list-style:none;}#iciba_text_box a{color:#666;text-decoration:none;}#iciba_text_box a:hover{color:#4372b6;text-decoration:underline;}#iciba_text_box a:focus{outline:none;}#iciba_text_box img{border:0;}#iciba_text_box strong{font-weight:100;font-family:"Segoe UI",Verdana,Lucida Sans Regular,Lucida Sans Unicode,Arial,sans-serif;}#iciba_text_box .icIBahyI-bg_main{background:#fff;}#iciba_text_box .icIBahyI-usually,#iciba_text_box .icIBahyI-prons,#iciba_text_box .icIBahyI-tab_list,#iciba_text_box .icIBahyI-group_inf{font-size:12px;}#iciba_text_box .icIBahyI-fl,#iciba_text_box .icIBahyI-nav_list li,#iciba_text_box .icIBahyI-left_bg,#iciba_text_box .icIBahyI-usually label,#iciba_text_box .icIBahyI-usually span,#iciba_text_box .icIBahyI-tab_list li,#iciba_text_box .icIBahyI-group_inf li,#iciba_text_box .icIBahyI-eg,#iciba_text_box .icIBahyI-us{float:left;}#iciba_text_box .icIBahyI-register,#iciba_text_box .icIBahyI-right_bg,#iciba_text_box .icIBahyI-up,#iciba_text_box .icIBahyI-down{float:right;}#iciba_text_box .icIBahyI-register{padding-left:25px;}#iciba_text_box a.icIBahyI-text_black,#iciba_text_box .icIBahyI-nav_list strong{color:#333;}#iciba_text_box a{color:#236fd4;text-decoration:underline;}#iciba_text_box a:hover{color:#236fd4;}#iciba_text_box .icIBahyI-handwrite,#iciba_text_box .icIBahyI-handwrite_over,#iciba_text_box .icIBahyI-drop_down{position:absolute;width:16px;height:16px;cursor:pointer;}#iciba_text_box .icIBahyI-handwrite{top:14px;right:130px;background-position:0 -70px;}#iciba_text_box .icIBahyI-handwrite_over{top:14px;right:130px;background-position:-18px -70px;}#iciba_text_box #icIBahyI-dict_main{padding:0;text-align:left;}#iciba_text_box .icIBahyI-dictbar{display:block;padding:0 0 4px 0;height:auto;}#iciba_text_box .icIBahyI-title{position:relative;padding:16px 17px 0 18px;color:#333;font-weight:bold;font-size:24px;font-family:arial,sans-serif;line-height:32px;word-break:break-all;}#iciba_text_box .icIBahyI-prons{display:inline-block;padding:0;height:auto;white-space:nowrap;font-weight:700;word-break:break-all;}#iciba_text_box .icIBahyI-dict_title{color:#333;font-weight:bold;font-family:arial,sans-serif;}#iciba_text_box .icIBahyI-ico_sound{float:left;display:block;overflow:hidden;margin:1px 0 -2px 4px;width:16px;height:16px;background:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACK0lEQVQ4jZWTQUuUURSGn3Pu/e6opM5kjYRUk2mZOWC0KqL8AS1cBBUUOf9Af4LbIJRo0yJs0V4hWhTUuC+SAslSclxESMM06cw4OvN9t8VUSgrZ2b8Pz3sOR7z3nLr6ZFpEh1F9LKpjH2euF9nnKICoGb505QKp7tSIdW6579r0yP8BjGW9XAfbTk9vX7ytPT7Vf+Nptv/ms8F9AgxhJHwv1Vgt1mnv6CJ55MSQdU1zZ289nxi4/SL+rwqEHkQVL8J6tUYNS2dXN4lDyVFjg+X0nZd71to28IKoIEYRo3iFSm2L5tZ2jp1MxVva2qbSmWw2nckO7gKghnoEYhQ1AkbZioSNOnxbr/O1GBLvTHK85+iQi7m5dCY7kR55Fd9RQQkjwYtQ2vSUqp4QRQOLa7YYZyhWIkp1S+9AioOH46OIjABYABFDedOzthFhnSFwBusMNjAYq4g0dJuadLumGv4AUKFa86hRbNAIBzHbAAWKc0oyEVBZq/B5cZVqpTr+/tHlyR0GShAI9VAxgWKdwcUMsWZLR8JxIAaLC3mKhfKsj3zm3cOLud9LbBgg2EBRIoxRrFXaWgOSHY5Cocqn+R85H4Zjb+8Pzvx9Rvsrj7WChoILlK5kDB+FfJjPs1ljXNROvr57es//aAC8x1hIxAISccuXlTzlSjgrxmbe3DuT2yu4C9ASEyqVMksLpZwYOzb34Pwu3b1GATx+dmUpR361MO7D8Nx+wwA/Ad9dudwSn5Q6AAAAAElFTkSuQmCC");-moz-transition:0.1s;}#iciba_text_box .icIBahyI-ico_sound:hover{opacity:0.75;}#iciba_text_box .icIBahyI-ico_sound:active{opacity:0.9;}#iciba_text_box .icIBahyI-eg,#iciba_text_box .icIBahyI-us,#iciba_text_box .icIBahyI-group_pos strong,#iciba_text_box .icIBahyI-group_inf li{padding-right:10px;}#iciba_text_box .icIBahyI-eg,#iciba_text_box .icIBahyI-us{padding-top:2px;font-family:"lucida sans unicode",arial;}#iciba_text_box .icIBahyI-new_word{float:left;padding-top:2px;}#iciba_text_box .icIBahyI-new_word a{display:block;padding:3px 10px 0 20px;width:35px;height:17px;background-position:-274px 0;color:#999!important;font-weight:100;font-family:"Microsoft Yahei";}#iciba_text_box .icIBahyI-new_word a.icIBahyI-new_word2{background-position:-274px -19px;}#iciba_text_box .icIBahyI-usually,#iciba_text_box .icIBahyI-prons{color:#666;}#iciba_text_box .icIBahyI-group_prons .icIBahyI-second{float:none;padding-top:0;}#iciba_text_box .icIBahyI-group_prons .icIBahyI-second .icIBahyI-eg{height:24px;}#iciba_text_box .icIBahyI-mb9 p{margin-bottom:9px;}#iciba_text_box .icIBahyI-group_pos{clear:both;overflow:hidden;color:#232323;line-height:20px;}#iciba_text_box .icIBahyI-group_pos p{clear:both;}#iciba_text_box .icIBahyI-label_list{float:left;padding-bottom:9px;width:87%;text-align:left;}#iciba_text_box .icIBahyI-cn .icIBahyI-group_pos{color:#333;}#iciba_text_box .icIBahyI-group_pos strong{color:#8d8d8d;}#iciba_text_box .icIBahyI-group_prons{clear:both;padding-top:7px;}#iciba_text_box .icIBahyI-margin_top{padding-top:6px;}#iciba_text_box .icIBahyI-part_list a,#iciba_text_box .icIBahyI-tab_list li a,#iciba_text_box .icIBahyI-new_word a,#iciba_text_box .icIBahyI-part_main h3 a{text-decoration:none;}#iciba_text_box .icIBahyI-tab_list{overflow:hidden;padding:0;height:1px;border-bottom:1px solid #fff;background:#c8c8c8;}#iciba_text_box .icIBahyI-title{padding:13px 10px 1px;font-size:14px;line-height:18px;}#iciba_text_box .icIBahyI-prons{font-weight:100;}#iciba_text_box .icIBahyI-group_pos{font-size:14px;line-height:20px;}#iciba_text_box .icIBahyI-group_pos p{float:left;padding:0 0 10px 0;}#iciba_text_box .icIBahyI-label_list{float:none;display:inline;padding:0 0 9px 9px;width:100%;}#iciba_text_box .icIBahyI-group_pos .icIBahyI-fl{float:none;display:inline;color:#666 !important;}#iciba_text_box .icIBahyI-synon{color:#333;line-height:28px;}#iciba_text_box .icIBahyI-suggest,#iciba_text_box .icIBahyI-suggest2{padding:0 0 3px 0;color:#333;word-wrap:break-word;font-size:14px;line-height:22px;}#iciba_text_box .icIBahyI-suggest ul{padding:7px 0 4px;line-height:155%;}#iciba_text_box .icIBahyI-suggest li{padding-left:14px;background-position:-134px -304px;}#iciba_text_box .icIBahyI-suggest p{line-height:250%;}#iciba_text_box .icIBahyI-suggest .icIBahyI-pr a{padding-right:3px;}#iciba_text_box .icIBahyI-suggest p.icIBahyI-pr{margin-top:3px;line-height:18px;}#iciba_text_box .icIBahyI-suggest .icIBahyI-pr a{display:inline-block;padding-right:10px;}#iciba_text_box .icIBahyI-footer{padding:0;width:auto;color:#999;text-align:right;font-size:12px;line-height:18px;}#iciba_text_box .icIBahyI-footer a{padding:0 3px 0 0;background:none;color:#236fd4;text-decoration:none !important;font-size:12px;line-height:18px;}#iciba_text_box .icIBahyI-footer a:hover{color:#236fd4;}#iciba_text_box .icIBahyI-text_blue{color:#236fd4;}#iciba_text_box .icIBahyI-list li{padding:0 15px;}#iciba_text_box .icIBahyI-li_dt,.icIBahyI-li_dd{display:inline;line-height:24px;}#iciba_text_box .icIBahyI-li_dt{padding-right:5px;color:#232323;}#iciba_text_box .icIBahyI-li_dd{color:#777;}#ICIBA_TOO_LONG{padding-top:10px;padding-left:10px;font-size:12px;}');
var auto_active = 1; //没用~
var ctrlKey_actived;
GM_registerMenuCommand("iciba翻译设置", openSetting);
function openSetting(){
ctrlKey_actived = confirm("按住ctrl键(当且仅当)开启翻译?")?1:0;
GM_setValue("ctrlKey_actived",ctrlKey_actived);
}
// Ctrl键触发
if(GM_getValue("ctrlKey_actived") == undefined){
ctrlKey_actived = 0;
}else{
ctrlKey_actived = parseInt(GM_getValue("ctrlKey_actived"));
}
GM_setValue("ctrlKey_actived",ctrlKey_actived);
window.addEventListener("mouseup",mouseClick,false);
window.addEventListener("keydown",function(e){
if(e.key === "Control" && e.keyCode === 17){
iciba_click_box.style.display = "none";
}
},false)
//document.body.style.position = "relative";
var iciba_click_box = document.createElement("div");
iciba_click_box.id = "iciba_click_box";
iciba_click_box.style.display = "none";
iciba_click_box.style.position = "absolute";
iciba_click_box.addEventListener("click",showResultBox,false);
document.body.appendChild(iciba_click_box);
var iciba_wrap = document.createElement("div");
iciba_wrap.id = "iciba_wrap";
iciba_wrap.style.display = "none";
iciba_wrap.style.position = "absolute";
iciba_wrap.innerHTML = ' \
<div id="iciba_search_box"><input id="iciba_input" type="text" /><input id="iciba_searchbtn" type="button" /></div><div id="iciba_text_box"></div>';
document.body.appendChild(iciba_wrap);
var iciba_text_box = document.getElementById("iciba_text_box");
var iciba_input = document.getElementById("iciba_input");
var iciba_searchbtn = document.getElementById("iciba_searchbtn");
iciba_input.addEventListener("keypress",searchFromInputBox,false);
iciba_searchbtn.addEventListener("click",searchFromInputBox,false);
if(window.location.href.indexOf("http://tieba.baidu.com/photo/p?kw=") == 1){
GM_addStyle('.af_container{position:relative;}');
}
function searchFromInputBox(e){
if(e.target == iciba_input){
if(e.keyCode != 13){
return;
}
}
var keyword = iciba_input.value;
changeData(keyword);
}
// mousedown 事件处理函数
function mouseClick(e){
// console.log('pageX:' + e.pageX + ',pageY:' + e.pageY + ',clientX:' + e.clientX + ',clientY:' + e.clientY)
// ignore when click on iciba_click_box
if(e.target == iciba_click_box){
return;
}
// ignore when click insideof iciba_wrap
if(IsInsideOf(e,iciba_wrap)){
return;
}
// Ctrl键触发
if(ctrlKey_actived){
if(!(e.ctrlKey == true && e.shiftKey == false && e.altKey == false)){
iciba_click_box.style.display = "none";
iciba_wrap.style.display = "none";
return;
}
}
// iciba_wrap displays
if(iciba_wrap.style.display == "block"){
// Hide iciba_wrap
iciba_wrap.style.display = "none";
}
// check iciba_icon
// 显示iciba_icon
if(window.getSelection().toString().length >= 150){
return;
}
if(window.getSelection().toString().length != 0){
var mouseX = e.pageX;
if(document.documentElement.clientWidth - document.body.clientWidth != 0){
mouseX -= (document.documentElement.clientWidth - document.body.clientWidth) / 2;
}
var mouseY = e.pageY;
iciba_click_box.style.top = mouseY + 8 + "px";
iciba_click_box.style.left = mouseX + "px";
iciba_click_box.setAttribute("keyword",window.getSelection().toString().toLowerCase());
setTimeout(function(){iciba_click_box.style.display = "block";},50);
return;
}
// 取消显示iciba_icon
if(window.getSelection().toString().length == 0){
iciba_click_box.style.display = "none";
}
return;
}
function showResultBox(e){
var word = iciba_click_box.getAttribute("keyword");
iciba_click_box.style.display = "none";
var bodyheight = document.body.clientHeight;
var bodywidth = document.body.clientWidth;
var htmlheight = document.documentElement.clientHeight;
var htmlwidth = document.documentElement.clientWidth;
var screenheight = window.innerHeight;
var screenwidth = window.innerWidth;
getData(word,e,bodyheight,bodywidth,screenheight,screenwidth,htmlheight,htmlwidth);
}
function IsInsideOf(e,target){
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;
}
}
function getData(keyword,event,bodyheight,bodywidth,screenheight,screenwidth,htmlheight,htmlwidth){
iciba_wrap.style.display = "block";
var body_style_position = document.defaultView.getComputedStyle(document.body)["position"];
var html_style_position = document.defaultView.getComputedStyle(document.documentElement)["position"];
var e = {
"pageX":event.pageX,
"pageY":event.pageY,
"clientX":event.clientX,
"clientY":event.clientY,
}
if(document.documentElement.clientWidth - document.body.clientWidth != 0){
e.pageX -= (document.documentElement.clientWidth - document.body.clientWidth) / 2;
}
if(body_style_position != "static"){
if(screenwidth - e.clientX < 300){
iciba_wrap.style.left = "auto";
iciba_wrap.style.right = bodywidth - e.pageX + "px";
}else{
iciba_wrap.style.left = e.pageX + "px";
iciba_wrap.style.right = "auto";
}
if(screenheight - e.clientY < 200){
iciba_wrap.style.top = "auto";
iciba_wrap.style.bottom = bodyheight - e.pageY + "px";
}else{
iciba_wrap.style.top = e.pageY + "px";
iciba_wrap.style.bottom = "auto";
}
}else if(html_style_position != "static"){
if(screenwidth - e.clientX < 300){
iciba_wrap.style.left = "auto";
iciba_wrap.style.right = htmlwidth - e.pageX + "px";
}else{
iciba_wrap.style.left = e.pageX + "px";
iciba_wrap.style.right = "auto";
}
if(screenheight - e.clientY < 200){
iciba_wrap.style.top = "auto";
iciba_wrap.style.bottom = htmlheight - e.pageY + "px";
}else{
iciba_wrap.style.top = e.pageY + "px";
iciba_wrap.style.bottom = "auto";
}
}else{
if(screenwidth - e.clientX < 300){
iciba_wrap.style.left = "auto";
iciba_wrap.style.right = screenwidth - e.pageX + "px";
}else{
iciba_wrap.style.left = e.pageX + "px";
iciba_wrap.style.right = "auto";
}
if(screenheight - e.clientY < 200){
iciba_wrap.style.top = "auto";
iciba_wrap.style.bottom = screenheight - e.pageY + "px";
}else{
iciba_wrap.style.top = e.pageY + "px";
iciba_wrap.style.bottom = "auto";
}
}
iciba_text_box.innerHTML = "Loading......";
iciba_input.value = keyword;
GM_xmlhttpRequest({
method:"GET",
referer:"http://www.iciba.com/",
url:"http://open.iciba.com/huaci/dict.php?word=" + keyword,
onload:function(response){
var text = response.responseText.replace(/\\/g,"");
text = text.match(/dict\.innerHTML=\'.*\'/) + "";
text = text.replace(/dict\.innerHTML='/g,"");
text = text.replace(/icIBahyI-"ico_sound"/g,'"icIBahyI-ico_sound"');
text = text.replace(/div\>'/g,"div>");
text = text.replace(/\<([\u4E00-\u9FA5]{1,5})\>/g,"<$1>")
text = text.replace(/\<>/g,"");
text = text.replace(/hidefocus="[a-z]{4,5}"/g,"");
//text = text.replace(/ title="点击发音"/g,"");
//text = text.replace(/ title="真人发音"/g,"");
setData(text);
}
});
}
function setData(result){
iciba_text_box.innerHTML = result;
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").replace("asplay_hanci('","").replace("');",""));
playbtn[i].removeAttribute("onclick");
playbtn[i].addEventListener("click",playSound,false);
}
}
}
function changeData(keyword){
iciba_text_box.innerHTML = "Loading......";
GM_xmlhttpRequest({
method:"GET",
referer:"http://www.iciba.com/",
url:"http://open.iciba.com/huaci/dict.php?word=" + keyword,
onload:function(response){
var text = response.responseText.replace(/\\/g,"");
text = text.match(/dict\.innerHTML=\'.*\'/) + "";
text = text.replace(/dict\.innerHTML='/g,"");
text = text.replace(/icIBahyI-"ico_sound"/g,'"icIBahyI-ico_sound"');
text = text.replace(/div\>'/g,"div>");
text = text.replace(/\<([\u4E00-\u9FA5]{1,5})\>/g,"<$1>")
text = text.replace(/\<>/g,"");
text = text.replace(/hidefocus="[a-z]{4,5}"/g,"");
//text = text.replace(/ title="点击发音"/g,"");
//text = text.replace(/ title="真人发音"/g,"");
setData(text);
}
});
}
function playSound(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();
}