运行代码 缩小
汉王
HTML代码
复制 格式化 注释 注释 清空
放大
AخA
 
1
<html>
2
<head>
3
<title>Opera网页选词</title>
4
</head>
5
<body>
6
鼠标选中文字,但很实用。
7
<script type="text/javascript">
8
// == UserScript ==
9
// Author: omgili
10
// Modified by Csineneo
11
// == /UserScript ==
12
var omgItemsArray=new Array();var g_CreatingMenu=false;var g_overIcon=false;var g_menuOn=false;var g_selectionTxt="";var g_Graphit=false;var g_ShowedAbout=false;if(typeof g_menuBKColor=='undefined')var g_menuBKColor="ffffff";if(typeof g_menuBorderColor=='undefined')var g_menuBorderColor="C0C0C0";if(typeof g_menuFontColor=='undefined')var g_menuFontColor="000000";if(typeof g_menuMouseoverColor=='undefined')var g_menuMouseoverColor="F4F4F4";function omgTrim(stringToTrim){return stringToTrim.replace(/^[\xa0\s]+|[\s\xa0]+$/g,"");}function omgToggleMenu(show){TBMenuObj=document.getElementById("omgTBMenu");if(TBMenuObj){if(show)TBMenuObj.style.visibility="visible";else{g_menuOn=false;TBMenuObj.style.visibility="hidden";}}}function omgToggleTBIcon(show,x,y){TBIconObj=document.getElementById("omgTBIcon");if(TBIconObj){if(show){TBIconObj.style.left=x+"px";TBIconObj.style.top=y+"px";TBIconObj.style.visibility="visible";}else{TBIconObj.style.visibility="hidden";}}}function omgHandleSelect(evnt){mouseY=0;mouseX=0;if(evnt){if(evnt.pageX&&evnt.pageY){mouseX=evnt.pageX;mouseY=evnt.pageY;}else{scrollOffset=omgGetScrollXY();mouseX=evnt.clientX+scrollOffset[0];mouseY=evnt.clientY+scrollOffset[1];}}if(document.selection){no_searchamabobFlag=false;if(evnt){if(evnt.srcElement){if(evnt.srcElement.id=='no_searchamabob'){no_searchamabobFlag=true;}}}if(document.selection.createRange().text&&!no_searchamabobFlag){g_selectionTxt=document.selection.createRange().text;omgToggleTBIcon(true,mouseX+18,mouseY-18);return;}}else{d=window.getSelection()+'';if(evnt){if(evnt.target){if(evnt.target.id=='no_searchamabob'){d=null;}}}if(d){if(d.length>0){g_selectionTxt=d;omgToggleTBIcon(true,mouseX+18,mouseY-18);return;}}}omgToggleTBIcon(false,0,0);omgToggleMenu(false);GraphObj=document.getElementById("omgGraphID");if(GraphObj&&!g_Graphit){document.body.removeChild(GraphObj);}g_Graphit=false;omgAboutObj=document.getElementById("omgAboutID");if(omgAboutObj&&!g_ShowedAbout){document.body.removeChild(omgAboutObj);}g_ShowedAbout=false;}function omgSearchGoogle(){var lsGURL="http://www.google.com/#hl=en&q=";var strGoogleURL=lsGURL+encodeURI(g_selectionTxt);window.open(strGoogleURL);}function omgSearchCustom(strEngine){strEngine=strEngine.replace("<term>",encodeURI(g_selectionTxt));window.open(strEngine);}function omgLimitWords(searchString,count){selectionTxtArray=searchString.split(" ");tmpSearchString="";for(i=0;i<selectionTxtArray.length;i++){if(i>=count)break;tmpSearchString+=selectionTxtArray[i]+" ";}tmpSearchString=omgTrim(tmpSearchString);return tmpSearchString;}function omgSearchOmgili(){searchString=omgLimitWords(g_selectionTxt,10);window.open("http://www.google.com/#hl=en&q=site%3Avows.cn+"+encodeURI(searchString));}function omgCloseAddSearchWindow(){omgAddResourceObj=document.getElementById("omgAddResourceID");if(omgAddResourceObj)document.body.removeChild(omgAddResourceObj);}function omgCleanResource(strObj){strObj=strObj.replace(/\|/g," ");strObj=strObj.replace(/,/g," ");strObj=strObj.replace(/\"/g," ");strObj=strObj.replace(/\'/g," ");return strObj;}function omgAddEngines(){omgURLObj=document.getElementById("omgResURL");omgNameObj=document.getElementById("omgResName");if(omgURLObj&&omgNameObj){name=omgTrim(omgNameObj.value);name=name.substr(0,35);name=omgCleanResource(name);url=omgTrim(omgURLObj.value);url=omgCleanResource(url);if(name.length&&url.length){strEngines=omgGet_Cookie("Engines");if(!strEngines)strEngines="";strEngines+=name+"|"+url+",";omgSet_Cookie('Engines',strEngines,1000,'/','','');}else{alert("Please enter the search engine name and URL.");return;}}omgCloseAddSearchWindow();}function omgGetScrollXY(){var scrOfX=0,scrOfY=0;if(typeof(window.pageYOffset)=='number'){scrOfY=window.pageYOffset;scrOfX=window.pageXOffset;}else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)){scrOfY=document.body.scrollTop;scrOfX=document.body.scrollLeft;}else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)){scrOfY=document.documentElement.scrollTop;scrOfX=document.documentElement.scrollLeft;}return[scrOfX,scrOfY];}function omgGetWinSize(){var myWidth=0,myHeight=0;if(typeof(window.innerWidth)=='number'){myWidth=window.innerWidth;myHeight=window.innerHeight;}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){myWidth=document.documentElement.clientWidth;myHeight=document.documentElement.clientHeight;}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){myWidth=document.body.clientWidth;myHeight=document.body.clientHeight;}return[myWidth,myHeight];}function omgAddResource(){WinDim=omgGetWinSize();scrollOffset=omgGetScrollXY();Obj=document.createElement("DIV");Obj.style.width=500+"px";Obj.style.height=300+"px";Obj.style.backgroundColor="#"+g_menuBKColor;Obj.style.border="1px solid #"+g_menuBorderColor;Obj.style.position="absolute";Obj.setAttribute("id","omgAddResourceID");Obj.setAttribute("name","omgAddResourceID");Obj.style.left=(WinDim[0]/2)+scrollOffset[0]-200+"px";Obj.style.top=(WinDim[1]/2)+scrollOffset[1]-125+"px";Obj.innerHTML='<center><span style="color:#'+g_menuFontColor+';font-family: arial,helvetica,clean,sans-serif;font-size: 18px;"><br><b>添加自定义搜索引擎</b></span></center><br><table align=center cellpadding=5 style="margin-left:30px;color:#'+g_menuFontColor+';font-family: arial,helvetica,clean,sans-serif;font-size: 14px;" ><tr><td><b>搜索引擎:</b></td>\
13
    <td><input type=text id=omgResName name=omgResName style="width:40ex;" value="Vows.cn 站内搜索"></td>\
14
    </tr><tr>\
15
    <td><b>搜索引擎 URL:</b></td>\
16
    <td><input type=text id=omgResURL name=omgResURL style="width:40ex;" value="http://www.google.cn/search?hl=zh-CN&q=site%3Avows.cn+<term>"></td>\
17
    </tr>\
18
    <tr><td colspan=2><br><font size=-1>* <term> 代表搜索关键词</font></td></tr>\
19
    <tr>\
20
    <td colspan=2 align=center><br><span style="font-weight:bold;text-decoration:underline;cursor:pointer;" onclick="omgAddEngines()">添加</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;text-decoration:underline;cursor:pointer;" onclick="omgCloseAddSearchWindow()">关闭</span></td>\
21
    </tr></table>';document.body.appendChild(Obj);}function omgDeleteEngine(evnt,engineName){evnt.cancelBubble=true;strEngines=omgGet_Cookie("Engines");tmpEngines="";if(strEngines){EnginesArray=strEngines.split(",");for(i=0;i<EnginesArray.length;i++){SavedItemsArray=EnginesArray[i].split("|");if(SavedItemsArray.length==2){name=omgTrim(SavedItemsArray[0]);url=omgTrim(SavedItemsArray[1]);if(name.length>0&&url.length>0){engineName=engineName.replace("搜索","");engineName=omgTrim(engineName);name=omgTrim(name);if(name==engineName)continue;tmpEngines+=name+"|"+url+",";}}}}omgSet_Cookie('Engines',tmpEngines,1000,'/','','');omgHandleSelect(null);}function omgAddItem(pos){onClickStr=" onclick=";onClickStr+=omgItemsArray[pos][1];nameStr=omgItemsArray[pos][0];if(omgItemsArray[pos][1].indexOf("omgSearchCustom")!=-1){nameStr+='<span onmouseover="this.style.color=\'#ff0000\'" onmouseout="this.style.color=\'#'+g_menuFontColor+'\'" onclick="omgDeleteEngine(event,\''+omgItemsArray[pos][0]+'\');">&nbsp; [x]</span>';}itemCode='<div '+onClickStr+' style="border-bottom:1px solid #808080;padding:2px;padding-left:5px;cursor:pointer;font-family: arial,helvetica,clean,sans-serif;font-size:12px;text-align:left;line-height:16px;" onmouseover="this.style.backgroundColor=\'#'+g_menuMouseoverColor+'\'" onmouseout="this.style.backgroundColor=\'#'+g_menuBKColor+'\'">'+nameStr+'</div>';TBMenuObj.innerHTML+=itemCode;if(pos<omgItemsArray.length-1){nextPos=pos
22
funcStr="omgAddItem("+(++pos)+")";setTimeout(funcStr,25);}else{g_menuOn=true;g_CreatingMenu=false;}}function omgLoadMenu(){omgItemsArray=new Array();omgItemsArray[0]=new Array(" Google搜索 ","omgSearchGoogle()");omgItemsArray[1]=new Array(" Vows.cn 站内搜索 ","omgSearchOmgili()");if(typeof g_customEngines!='undefined'){for(i=0;i<g_customEngines.length;i++){engineParts=g_customEngines[i].split(",");if(engineParts.length==2){omgItemsArray.push(new Array(engineParts[0],"\"omgSearchPredefined(\'"+engineParts[1]+"\')\""));}}}omgItemsArray.push(new Array(" + 添加搜索引擎...","omgAddResource()"));strEngines=omgGet_Cookie("Engines");if(strEngines){EnginesArray=strEngines.split(",");for(i=0;i<EnginesArray.length;i++){SavedItemsArray=EnginesArray[i].split("|");if(SavedItemsArray.length==2){name=omgTrim(SavedItemsArray[0]);url=omgTrim(SavedItemsArray[1]);if(name.length>0&&url.length>0){omgItemsArray.push(new Array(" 搜索 "+name,"\"omgSearchCustom(\'"+url+"\')\""));}}}}}function omgCreateMenu(x,y){if(!g_CreatingMenu&&g_menuOn==false){g_CreatingMenu=true;TBMenuObj=document.getElementById("omgTBMenu");if(TBMenuObj){omgLoadMenu();TBMenuObj.innerHTML="";TBMenuObj.style.left=x+"px";TBMenuObj.style.top=y+"px";omgToggleMenu(true);omgAddItem(0);}}}function omgDoMouseOver(obj){if(!g_overIcon){TBIconObj=document.getElementById("omgTBIcon");if(TBIconObj){TBIconObj.style.backgroundColor="#"+g_menuMouseoverColor;e=obj;var l=0;var t=0;while(e){l+=e.offsetLeft;t+=e.offsetTop;e=e.offsetParent;}omgCreateMenu(l+19,t);}}}function omgDoMouseOut(obj){TBIconObj=document.getElementById("omgTBIcon");if(TBIconObj)TBIconObj.style.backgroundColor="#"+g_menuBKColor;}function omgGet_Cookie(name){var start=document.cookie.indexOf(name+"=");var len=start+name.length+1;if((!start)&&(name!=document.cookie.substring(0,name.length))){return null;}if(start==-1)return null;var end=document.cookie.indexOf(";",len);if(end==-1)end=document.cookie.length;return unescape(document.cookie.substring(len,end));}function omgSet_Cookie(name,value,expires,path,domain,secure){var today=new Date();today.setTime(today.getTime());if(expires){expires=expires*1000*60*60*24;}var expires_date=new Date(today.getTime()+(expires));document.cookie=name+"="+escape(value)+((expires)?";expires="+expires_date.toGMTString():"")+((path)?";path="+path:"")+((domain)?";domain="+domain:"")+((secure)?";secure":"");}function omgChangeOpacity(obj,opc){if(opc){if(obj.style.MozOpacity!=null){obj.style.MozOpacity=1;}else obj.style.filter="progid:DXImageTransform.Microsoft.Shadow(color=#CACACA,direction=135,strength=3) alpha(opacity=100)";}else{if(obj.style.MozOpacity!=null){obj.style.MozOpacity=0.8;}else obj.style.filter="progid:DXImageTransform.Microsoft.Shadow(color=#CACACA,direction=135,strength=3) alpha(opacity=80)";}}function omgOnLoadSearchamabob(){TBIcon='<div align="center" name="omgTBIcon" id="omgTBIcon" style="visibility:hidden;width:18px;position: absolute;border: 1px solid #245EDC;z-index: 100;height: 18px;cursor:pointer;background-color: #'+g_menuBKColor+';font-size: 12px;color:#'+g_menuFontColor+';font-family: arial,helvetica,clean,sans-serif;line-height:16px;" onMouseOver="omgDoMouseOver(this)" onMouseOut="omgDoMouseOut(this)"><b>搜</b></div>';TBMenu='<div ID="omgTBMenu" onmouseover="omgChangeOpacity(this,1)" onmouseout="omgChangeOpacity(this,0)" name="omgTBMenu" style="visibility:hidden;position:absolute;z-index: 100;font-weight:bold;color:#'+g_menuFontColor+';font-family: arial,helvetica,clean,sans-serif;font-size: smaller;width:200px;background-color:#'+g_menuBKColor+';border:1px solid #'+g_menuBorderColor+';"></div>';Obj=document.createElement("SPAN");Obj.setAttribute("id","omgSearchamabobID");Obj.setAttribute("name","omgSearchamabobID");Obj.innerHTML=TBIcon+TBMenu;document.body.appendChild(Obj);TBMenuObj=document.getElementById("omgTBMenu");if(TBMenuObj.style.MozOpacity!=null){TBMenuObj.style.MozOpacity=0.8;}else TBMenuObj.style.filter="progid:DXImageTransform.Microsoft.Shadow(color=#CACACA,direction=135,strength=3) alpha(opacity=80)";if(typeof window.addEventListener!='undefined'){window.addEventListener("click",omgHandleSelect,false);window.addEventListener("mouseup",omgHandleSelect,false);}else if(typeof document.addEventListener!='undefined'){document.addEventListener("click",omgHandleSelect,false);document.addEventListener("mouseup",omgHandleSelect,false);}else if(typeof window.attachEvent!='undefined'){document.body.attachEvent("onclick",omgHandleSelect);document.body.attachEvent("ondblclick",omgHandleSelect);}}(function(){var ie=!!(window.attachEvent&&!window.opera);var wk=/webkit\/(\d+)/i.test(navigator.userAgent)&&(RegExp.$1<525);var fn=[];var run=function(){for(var i=0;i<fn.length;i++)fn[i]();};var d=document;d.ready=function(f){if(!ie&&!wk&&d.addEventListener)return d.addEventListener('DOMContentLoaded',f,false);if(fn.push(f)>1)return;if(ie)(function(){try{d.documentElement.doScroll('left');run();}catch(err){setTimeout(arguments.callee,0);}})();else if(wk)var t=setInterval(function(){if(/^(loaded|complete)$/.test(d.readyState))clearInterval(t),run();},0);};})();document.ready(function(){omgOnLoadSearchamabob()});
23
</script>
24
</body>
25
</html>
CSS代码
复制 格式化 注释 注释 颜色 清空
放大
CSS代码...
x
 
1
JS代码
复制 格式化 注释 注释 清空
放大
JS代码...
xxxxxxxxxx
1
 
1
名称
Js模拟Opera网站的网页选词效果
分类
网站常用
描述
JS实现网页选词代码,鼠标选中文字,会出现一个“搜”字,点击这个字,会弹出菜单,上面注明:搜Google搜索 <br>  Vows.cn 站内搜索、+ 添加搜索引擎...从Opera官方网站发现的效果,感谢分享。
收藏