Jump to content
Wikipedia The Free Encyclopedia

User:Subh83/JavaScriptTools/selectRightclickMenu.js

From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Subh83/JavaScriptTools/selectRightclickMenu.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
 /****************************************************
 * Created by Subhrajit Bhattacharya [[User:Subh83]] *
 * Licensed under GNU-GPL v3.0 *
 *****************************************************/

 if(typeof(RightClickMenuKey)=='undefined')RightClickMenuKey='';// 'shift', 'alt', 'ctrl' or ''

 if(typeof(UserSubh83_utils1)=='undefined')importScript("User:Subh83/JavaScriptTools/utils1.js");

 functiongetSelectionMenuHTML(selText,clickElem)
 {
 selText=selText.toString();
 varencodedSelText=XEncodeURIComponent(selText);
 setCookie("lastRightClickSelText",encodedSelText);

 varpageTitle=getPageTitle();

 varsectionEditLinkNode=GetPrevEditsectionLinkElement(clickElem);
 if(sectionEditLinkNode){
 varEditLink=sectionEditLinkNode.href;
 varsecNumMatch=EditLink.match(/section=([\d]+)/);
 if(secNumMatch.length>1)
 varsecNum=secNumMatch[1];
 varWikiBlameFun="WikiBlameFromSnippet('"+XEncodeURIComponent(pageTitle)+"','"+encodedSelText+"',"+secNum+"); ";
 }
 else{
 varEditLink=(wgServer+wgScript)+"?title="+pageTitle+"&action=edit&section=0";
 varsecNum=0;
 varWikiBlameFun="WikiBlameFromSnippet('"+XEncodeURIComponent(pageTitle)+"','"+encodedSelText+"',"+secNum+"); ";
 varDiffTableNode=FindNodeUpTree(clickElem,['table']);
 if(DiffTableNode&&DiffTableNode.className.indexOf('diff')>=0){
 EditLink="";
 secNum=-1;
 WikiBlameFun="WikiBlameFromWikitext('"+XEncodeURIComponent(pageTitle)+"','"+encodedSelText+"'); ";
 }
 }

 // Generate the HTML
 varret="<div class='rightclickmenu'> "+
 "<span class='heading'>\""+((selText.length>40)?(selText.substr(0,17)+' ... '+selText.substr(selText.length-17,17)):(selText))+
 "\"</span><hr/><div class='items'>";
 if(EditLink!="")
 ret+="<div class='menuitem'><a href=\""+EditLink+"\" onClick=\"placeCursorInTextarea(25);\">Edit this text</a></div>";
 ret+="<div class='menuitem'><a href=\"javascript:void(0);\" onClick=\""+WikiBlameFun+
 "document.getElementById('selectrightclickmenu').style.display='none'; "+
 "delCookie('lastRightClickSelText');\">Trace history on WikiBlame</a></div>"+
 "<hr/>"+
 "<div class='menuitem'><a href=\"http://www.google.com/search?q="+encodedSelText+"\" target=\"_blank\">Search Google for this text</a></div>"+
 "</div></div>";

 returnret;
 }

 functionshowSelectionMenu(e)
 {
 varselText="";
 if(window.getSelection)selText=window.getSelection();
 elseif(document.getSelection)selText=document.getSelection();
 elseif(document.selection)selText=document.selection.createRange().text;
 elsereturn;

 if(!e)vare=window.event;
 varrightclick;
 if(e.which)rightclick=(e.which==3);
 elseif(e.button)rightclick=(e.button==2);

 if(e.target)targ=e.target;
 elseif(e.srcElement)targ=e.srcElement;
 if(targ.nodeType==3)// Safari bug
 targ=targ.parentNode;

 varkeyPressed=true;
 if(RightClickMenuKey==''&&(e.shiftKey||e.ctrlKey||e.altKey))keyPressed=false;
 elseif(RightClickMenuKey=='shift'&&!e.shiftKey)keyPressed=false;
 elseif(RightClickMenuKey=='ctrl'&&!e.ctrlKey)keyPressed=false;
 elseif(RightClickMenuKey=='alt'&&!e.altKey)keyPressed=false;

 varselectrightclickmenu=document.getElementById("selectrightclickmenu");
 if(!selectrightclickmenu){
 alert("Unable to locate rightclick-menu!");
 returnfalse;
 }
 if(selText!=""&&rightclick&&keyPressed&&targ.tagName.toLowerCase()!='textarea'){
 selectrightclickmenu.innerHTML=getSelectionMenuHTML(selText,targ);
 setMenuPosition(e,selectrightclickmenu);
 selectrightclickmenu.style.zIndex="10000";
 selectrightclickmenu.style.display="block";
 returntrue;
 }
 else{
 selectrightclickmenu.style.display="none";
 delCookie("lastRightClickSelText");
 returnfalse;
 }
 }

 functionplaceCursorInTextarea(retry){
 varmainTextArea=document.getElementById('wpTextbox1');

 if(!mainTextArea&&retry>0){// Support for 'secedit' (User:Supadawg/secedit.js)
 setTimeout(placeCursorInTextarea,200,retry-1);
 return;
 }

 if(mainTextArea){
 setSelectionInWikitextFromSnippet(mainTextArea,decodeURIComponent(getCookie("lastRightClickSelText")));
 delCookie("lastRightClickSelText");
 if(document.getElementById("selectrightclickmenu"))
 document.getElementById("selectrightclickmenu").style.display="none";
 }
 }

 $(function(){

 // The right-click menu 
 varbodyContent=document.getElementById('bodyContent');
 if(bodyContent){
 AppendHandlerToEvent("document.getElementById('bodyContent').onmouseup",showSelectionMenu,'pre',false,"return (!retNew);");
 AppendHandlerToEvent("document.getElementById('bodyContent').oncontextmenu",showSelectionMenu,'pre',false,"return (!retNew);");
 }

 varselectrightclickmenu=document.createElement("div");
 selectrightclickmenu.setAttribute("id","selectrightclickmenu");
 selectrightclickmenu.setAttribute("display","none");
 vartheBody=document.getElementsByTagName("body")[0];
 theBody.insertBefore(selectrightclickmenu,theBody.childNodes[0]);

 // Cursor positioning in editform 
 placeCursorInTextarea(1);

 });

AltStyle によって変換されたページ (->オリジナル) /