User:Fox/replace.js

// Does what it says on the tin. Adds a portlet link to the tab bar which allows the user to replace a phrase with another.
// Code by [[User:Fox]], fixed extensively by [[User:Tom Morris]], with help from [[User:Quanticle]].

addOnloadHook(function() {
    // parse URL parameters into an object
    var urlParameters = (function() {
        var urlParameters = new Object();
        var asReadInUrlParameters;
        var asReadInUrlParameter;

        asReadInUrlParameters = location.search.substring(1, location.search.length).split("&");
        for (i = 0; i < asReadInUrlParameters.length; i++) {
            asReadInUrlParameter = asReadInUrlParameters[i].split("=");
            urlParameters[decodeURIComponent(asReadInUrlParameter[0])] = decodeURIComponent(asReadInUrlParameter[1]);
        }
        
        return urlParameters;
    })();

    // handle what happens if there is a 'replace'
    if (urlParameters["action"] == "edit" && urlParameters["replace"] != undefined) {
      
        // prompt the user
        var before = prompt("What do you want to replace?", "");
        var after = prompt("What do you want to replace \"" + before + "\" with?", "");
        
        // replace text
        var match = new RegExp(before, "ig");
        var form = document.forms["editform"];
        var txt = form["wpTextbox1"].value;

        if (after.length > 0 && before.length > 0) {
            replaced = txt.replace(match, after);
        } else {
            alert("You must specify both variables!")
        }
        
        form["wpTextbox1"].value = replaced;
        // set edit summary
        form["wpSummary"].value = 'Replacing the phrase ' + before + ' with ' + after + ' ([[User:Fox/replace.js|RPL]])';
        // submit
        form.submit();
    }

    // add portlet link
    mw.util.addPortletLink("p-cactions", wgScript + "?title=" + wgPageName + "&action=edit&replace=1", "replace", "ca-replace");
});

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.