iciba划词翻译

iciba翻译

当前为 2014-12-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        iciba划词翻译
// @namespace   noe132.com
// @author  	noe132
// @include     http://*
// @include     https://*
// @exclude     http://www.iciba.com*
// @grant       GM_xmlhttpRequest
// @grant       GM_addStyle
// @icon		http://tb.himg.baidu.com/sys/portrait/item/d4346e6f65313332ac06
// @version		2.08
// @description iciba翻译
// ==/UserScript==

// beta version 
// last update : 2014/12/19

/*
update INFO:
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 : 添加输入框查词,修正定位不正确。


流程说明:
鼠标事件触发后,显示按钮mouseClick();
点击按钮后,showResultBox();
getData();
setData();
*/
"use strict";
var auto_active = 1;

document.body.addEventListener("mouseup",mouseClick,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);

GM_addStyle('#iciba_click_box{height:20px;width:20px;background:#ABD;border:1px solid #555;box-shadow:0 0 3px #111;border-radius:100px;z-index:1000;overflow:hidden;-moz-transition:.05s;opacity:.7}#iciba_click_box:hover{opacity:1}#iciba_click_box:active{background:#90a2c4;border:1px solid #353535;box-shadow:0 0 3px #222}#iciba_wrap{border:1px solid gray;height:auto;width:auto;min-width:260px;max-width:360px;background:#EEE;z-index:1000;box-shadow:0 0 3px #999}#iciba_search_box{border-bottom:1px solid gray;margin:0;padding:0;display:block;width:100%;font-size:0}#iciba_search_box>#iciba_input{width:-moz-calc(100% - 44px);height:26px;line-height:28px;margin:0;padding:2px 7px 0 7px;border:0;-moz-transition:.1s;display:inline-block;box-sizing:content-box;display:-moz-box;font-size:15px;vertical-align:middle}#iciba_search_box>#iciba_input:hover,#iciba_search_box>#iciba_input:focus{box-shadow:0 0 3px #BBB inset}#iciba_search_box>#iciba_searchbtn{vertical-align:middle;box-sizing:content-box;display:inline-block;width:29px;height:28px;margin:0;padding:0;border:0;background-color:#e5e5e5;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAAAEgAAABIAEbJaz4AAAAJdnBBZwAAABAAAAAQAFzGrcMAAADcSURBVCjPhdG9TgJhEIXh58M1EGEFjEohiUGk4f4vBaSy0UYRBFEIP8JaLEvWhOjbTc6bzMlMSPxNRICGpopgZejJJosTIRGCtjtLE1uxmrGeZSZEaGh5NzBH5Na9jp7ksMKNtQcL8O1R2bXYLBUKqBjv45QXJ86yoYBg96v4zr55JqzECjmhamedF4ZqWoe4runTJHcHzy60lb3aqmo6tVHJSoZEoKTjSoSdL5GShZ5ZdqhUPVcWrEzVdRXN9X3khTyXuooW+sm04BgjAyux+7TkMd4kWkaE/979AxXnQ1s0DN1UAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDEwLTAyLTExVDExOjUwOjA4LTA2OjAw1hBl+wAAACV0RVh0ZGF0ZTptb2RpZnkAMjAwNi0wNS0wNVQxMzoyMjo0MC0wNTowML/k/hoAAAAASUVORK5CYII=);background-repeat:no-repeat;background-position:7px 7px;border-left:1px solid gray;-moz-transition:.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{font-size:13px;padding:7px 9px 9px 9px;margin:0;color:black}#iciba_text_box a{color:#35C}.icIBahyI-new_word{display:none}#iciba_text_box{font-family:Arial,Helvetica,sans-serif;font-size:14px;text-align:left}#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-size:14px}#iciba_text_box input,#iciba_text_box button,#iciba_text_box textarea{font-family:Arial,Helvetica,sans-serif;font-size:inherit;font-style:inherit;font-weight:inherit}#iciba_text_box{background:#fbfbfb;_width:278px;_overflow:hidden}#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:0}#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{text-decoration:underline;color:#236fd4}#iciba_text_box a:hover{color:#236fd4}#iciba_text_box .icIBahyI-handwrite,#iciba_text_box .icIBahyI-handwrite_over,#iciba_text_box .icIBahyI-drop_down{width:16px;height:16px;cursor:pointer;position:absolute}#iciba_text_box .icIBahyI-handwrite{background-position:0 -70px;top:14px;right:130px}#iciba_text_box .icIBahyI-handwrite_over{background-position:-18px -70px;top:14px;right:130px}#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{line-height:32px;position:relative;font-size:24px;color:#333;font-family:arial,sans-serif;font-weight:bold;padding:16px 17px 0 18px;word-break:break-all}#iciba_text_box .icIBahyI-prons{white-space:nowrap;word-break:break-all;font-weight:700;padding:0;height:auto;display:inline-block}#iciba_text_box .icIBahyI-dict_title{color:#333;font-weight:bold;font-family:arial,sans-serif}#iciba_text_box .icIBahyI-ico_sound{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);display:block;float:left;height:16px;margin:1px 0 -2px 4px;overflow:hidden;width:16px;-moz-transition:.1s}#iciba_text_box .icIBahyI-ico_sound:hover{opacity:.75}#iciba_text_box .icIBahyI-ico_sound:active{opacity:.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{font-family:"lucida sans unicode",arial;padding-top:2px}#iciba_text_box .icIBahyI-new_word{padding-top:2px;float:left}#iciba_text_box .icIBahyI-new_word a{padding:3px 10px 0 20px;height:17px;font-weight:100;color:#999!important;display:block;background-position:-274px 0;width:35px;font-family:"瀹嬩綋"}#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{color:#232323;line-height:20px;clear:both;overflow:hidden}#iciba_text_box .icIBahyI-group_pos p{clear:both}#iciba_text_box .icIBahyI-label_list{float:left;text-align:left;width:87%;padding-bottom:9px}#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{height:1px;overflow:hidden;padding:0;background:#c8c8c8;border-bottom:1px solid #fff}#iciba_text_box .icIBahyI-title{font-size:14px;padding:13px 10px 1px;line-height:18px}#iciba_text_box .icIBahyI-prons{font-weight:100}#iciba_text_box .icIBahyI-group_pos{line-height:20px;font-size:14px}#iciba_text_box .icIBahyI-group_pos p{padding-bottom:9px}#iciba_text_box .icIBahyI-label_list{width:80%;padding:0 0 9px 5px}#iciba_text_box .icIBahyI-group_pos .icIBahyI-fl{color:#666!important}#iciba_text_box .icIBahyI-synon{color:#333;line-height:28px}#iciba_text_box .icIBahyI-suggest,#iciba_text_box .icIBahyI-suggest2{color:#333;padding:0 0 3px 0;word-wrap:break-word;font-size:14px;line-height:22px}#iciba_text_box .icIBahyI-suggest ul{line-height:155%;padding:7px 0 4px}#iciba_text_box .icIBahyI-suggest li{background-position:-134px -304px;padding-left:14px}#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{line-height:18px;margin-top:3px}#iciba_text_box .icIBahyI-suggest .icIBahyI-pr a{padding-right:10px;display:inline-block}#iciba_text_box .icIBahyI-footer{text-align:right;color:#999;width:auto;padding:0;font-size:12px;line-height:18px}#iciba_text_box .icIBahyI-footer a{font-size:12px;line-height:18px;text-decoration:none!important;color:#236fd4;padding:0 3px 0 0}#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{color:#232323;padding-right:5px}#iciba_text_box .icIBahyI-li_dd{color:#777}#ICIBA_TOO_LONG{padding-top:10px;padding-left:10px;font-size:12px}');

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);
}

function mouseClick(e){
	// 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;
	}
	
	// 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;
		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());
		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,e,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"];
	
	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,"&#60;$1&#62;")
			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,"&#60;$1&#62;")
			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();
}