User:Fox/replace.js
Appearance
< User:Fox
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. |
Documentation for this user script can be added at User:Fox/replace. |
// 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");
});