Misplaced Pages

User:Σ/Testing facility/Baz.js

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Σ | Testing facility
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:Σ/Testing facility/Baz.
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 Misplaced Pages:Bypass your cache.
// <nowiki>
/**
 * autocvu.js
 *
 * Adds a "report" link to page histories and diffs for quickly reporting a vandal to the CVU.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
$(document).ready(function() {
	function makeLink(user) {
		var a = document.createElement('a');
		a.href = wgScript + '?title=RuneScape:Counter-Vandalism_Unit&action=edit&section=1&cvuEditor=' + encodeURIComponent(user) + '&cvuPage=' + encodeURIComponent(mw.config.get('wgPageName'));
		a.title = 'Report this edit to the CVU.';
		a.appendChild(document.createTextNode('report'));
		return a;
	}
 
	// http://www.netlobo.com/url_query_string_javascript.html
	function getParam(name) {
		name = name.replace(//, '\\\]/, '\\\]');
		var regex = new RegExp('' + name + '=(*)');
  		var results = regex.exec(window.location.href);
		if (results == null) {
			return '';
		} else {
			return decodeURIComponent(results.replace(/\+/g, '%20'));
		}
	}
 
	var cvuEditor = getParam('cvuEditor');
	var cvuPage = getParam('cvuPage');
 
	if (wgAction == 'rollback') {
		var page = document.getElementById('bodyContent');
		var p = document.createElement('p');
		p.style.fontWeight = 'bold';
		p.appendChild(document.createTextNode('You can '));
		p.appendChild(makeLink(getParam('from')));
		p.appendChild(document.createTextNode(' this user to the CVU.'));
		page.insertBefore(p, page.firstChild);
	} else if (wgAction == 'history') {
		var list = document.getElementById('pagehistory');
		var items = list.getElementsByTagName('li');
		for (i = 0; i < items.length; i++) {
			var user = getElementsByClassName(items, 'a', 'mw-userlink').innerHTML;
			var undo = getElementsByClassName(items, 'span', 'mw-history-undo');
 
			if (undo) {
				undo.appendChild(document.createTextNode(' | '));
				undo.appendChild(makeLink(user));
			} else {
				items.appendChild(document.createTextNode(' ('));
				items.appendChild(makeLink(user));
				items.appendChild(document.createTextNode(')'));
			}
		}
	} else if (wgAction == 'view' && getParam('diff')) {
		var user = getElementsByClassName(document.getElementById('mw-diff-ntitle2'), 'a', 'mw-userlink').innerHTML;
 
		var undo = document.getElementById('mw-diff-ntitle1').firstChild;
		undo.appendChild(document.createTextNode(' ('));
		undo.appendChild(makeLink(user));
		undo.appendChild(document.createTextNode(')'));
	} else if (wgAction == 'edit' && cvuEditor && cvuPage) {
		var lineSep = (navigator.appVersion.indexOf('MSIE') != -1) ? '\r\n' : '\n';
		var obj = document.getElementById('wpTextbox1');
		var tpl = '{' + '{cvuid|' + cvuEditor + '|' + cvuPage.replace(/_/g, ' ') + '}' + '}\n';
		var pos1, pos2;
		var rteEnabled = (typeof(window.RTEInstanceId) != 'undefined');
 
		obj.value = obj.value.replace('{{cvuid|insert vandal}}' + lineSep, '');
 
		if (rteEnabled) {
			pos1 = obj.value.indexOf('type="comment" />' + lineSep + '</p>');
		} else {
			pos1 = obj.value.indexOf('-->');
		}
 
		if (pos1 == -1) {
			alert('Auto CVU was unable to find the reference point.');
			return;
		}
 
		if (rteEnabled) {
			pos2 = obj.value.indexOf('<img', pos1);
		} else {
			pos2 = obj.value.indexOf('{{cvuid', pos1);
		}
 
		if (pos2 == -1) {
			alert('Auto CVU was unable to find the template insertion point.');
			return;
		}
 
		obj.value = obj.value.substring(0, pos2) + tpl + obj.value.substring(pos2);
		document.getElementById('wpSummary').value += ' Reported using ]';
 
		if (window.autoCvuSubmit) {
			$('#wpSave').click(); // Not submit(); needed to fire events for RTE
		}
	}
});
// </nowiki>