Jump to content

User:Certes/replace.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Certes (talk | contribs) at 20:14, 7 May 2022 (Find and replace, first attempt). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/*** Find and replace ***/

// Replaces text in the edit window re1from → re1to, etc.
// Uses URL parameters ?action=edit&withJS=<this script>&re1from=...&re1to=...&re1flags=...[&re2from=...&re2to=...&re2flags=...]...
// By [[en:w:User:Certes]]

(function() {
	var first = true;
	mw.hook('wikipage.editform').add(function($editForm) {
		if (first) { // Don't repeat the replaces on preview etc.
			var refrom = [];
			var reto = [];
			var recount = 0;
			for (var i = 1; i <= 10; i++) { // Look for re1from etc.
				var rf = mw.util.getParamValue('re' + String(i) + 'from');
				if (rf) {
					refrom[recount] = new RegExp(rf), mw.util.getParamValue('re' + String(i) + 'flags'));
					reto[recount] = mw.util.getParamValue('re' + String(i) + 'to');
					recount ++;
				}
			}
			var text = $editForm.html();
			for (var i = 0; i < recount; i++) {
				text = text.replace(refrom[i], reto[i]);
			}
			$editForm.html() = text;
			first = false;
		}
	}
})();