MediaWiki:Gadget-AutoCVU.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
* Adds a "report" link to page histories and diffs for quickly reporting a vandal to the CVU.
*/
var autoCvuSubmit = 1; // Whether to auto submit edits to the CVU page
$(document).ready(function() {
function makeLink(user) {
var a = document.createElement('a');
a.href = mw.config.get('wgScript') + '?title=GSWiki:Counter-Vandalism_Unit&action=edit§ion=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(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(window.location.href);
if (results === null) {
return '';
} else {
return decodeURIComponent(results[1].replace(/\+/g, '%20'));
}
}
var cvuEditor = getParam('cvuEditor');
var cvuPage = getParam('cvuPage');
var action = mw.config.get('wgAction')
if (action == '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 (action == 'history') {
var list = document.getElementById('pagehistory');
var items = list.getElementsByTagName('li');
for (i = 0; i < items.length; i++) {
var user = items[i].getElementsByClassName('mw-userlink')[0].innerText;
var undo = items[i].getElementsByClassName('mw-history-undo')[0];
if (undo) {
undo.appendChild(document.createTextNode(' | '));
undo.appendChild(makeLink(user));
} else {
items[i].appendChild(document.createTextNode(' ('));
items[i].appendChild(makeLink(user));
items[i].appendChild(document.createTextNode(')'));
}
}
} else if (action == 'view' && getParam('diff')) {
var user = document.getElementById('mw-diff-ntitle2').getElementsByClassName('mw-userlink')[0].innerText;
var undo = document.getElementById('mw-diff-ntitle1').firstChild;
undo.appendChild(document.createTextNode(' ('));
undo.appendChild(makeLink(user));
undo.appendChild(document.createTextNode(')'));
} else if (action == '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').querySelector('input').value = 'Reported [[Special:Contributions/' + cvuEditor + '|' + cvuEditor + ']] using the [[MediaWiki:Gadget-AutoCVU.js|report button]]';
if (window.autoCvuSubmit) {
var save = document.getElementById('wpSave').querySelector('input');
$(save).click(); // Not submit(); needed to fire events for RTE
}
}
});