User:Subh83/JavaScriptTools/selectRightclickMenu.js

/**************************************************** * Created by Subhrajit Bhattacharya [[User:Subh83]] * * Licensed under GNU-GPL v3.0

/****************************************************
* 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");

function getSelectionMenuHTML(selText, clickElem)
{
    selText = selText.toString();
    var encodedSelText = XEncodeURIComponent(selText);
    setCookie("lastRightClickSelText", encodedSelText);

    var pageTitle = getPageTitle();

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

    // Generate the HTML
    var ret = "<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>";

    return ret;
}

function showSelectionMenu(e)
{
    var selText = "";
    if (window.getSelection) selText = window.getSelection();
    else if (document.getSelection) selText = document.getSelection();
    else if (document.selection)  selText = document.selection.createRange().text;
    else return;

    if (!e) var e = window.event;
    var rightclick;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);

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

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

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

function placeCursorInTextarea(retry) {
    var mainTextArea = 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 
    var bodyContent = 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);" );
    }

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

    // Cursor positioning in editform 
    placeCursorInTextarea(1);

});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.