User:Hoo man/spi helper.js
Appearance
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:Hoo man/spi helper. |
/*
SPI helper by User:Hoo_man
revision 1;
2010-07-19
Attention: still needs testing!!
*/
//config
var loadMessage = 'Retrieving data...'; //message shown while data gets transfered to the server
//error messages:
var error = [];
error['retrieve'] = 'Error: unable to retrieve source';
error['changePage'] = 'The following error occured while updating the page: $1';
//other
var changeWindow = [];
changeWindow['title'] = 'Set the status of the SPI template to:';
changeWindow['button'] = 'Change Status';
menuTitle = 'Change Status';
editSummary = 'Changed Status to: $1';
//do not change things below here, unless you know what you are doing!
var layout = [];
var page = [];
var source = '';
var status = '';
var template = '';
var newStatus = '';
var editToken = '';
if (window.XMLHttpRequest) {
var http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
var http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
window.onload = init;
//main function, checks whether the page is a Sockpuppet page or not, and gets the raw code
function init() {
//are we on a subpage of Wikipedia:Sockpuppet investigations?
page['sockpuppet_investigations_subpage'] = /Wikipedia:Sockpuppet_investigations\/.*/i.test(wgPageName);
if(page['sockpuppet_investigations_subpage'] && wgAction == 'view') {
addPortletLink('p-cactions', 'javascript:openChangeStatusWindow();', menuTitle, 'ca-spiStatus');
//get the raw source
var http = http_request;
http.open("GET", wgServer + wgScript + '?title=' + encodeURIComponent(wgPageName) + "&action=raw&oldid=" + wgCurRevisionId, true);
http.send(null);
http.onreadystatechange = function() {
if(http.readyState == 4) {
if(http.status == 200) {
// succes ;-)
source = http.responseText;
tool();
}else{
alert(error['retrieve']);
}
}
}
}
}
//analyses the current template on the page
function tool() {
//remove html comments
tmp = source.replace(/<!(?:--[\s\S]*?--\s*)?>\s*/g, '');
//test for existing templates...
template = tmp.match(/{{SPI case status\|?(.*)}}/ig);
if(!template) {
//template is fully missing, we have to add it
template = 'no';
}else{
//there already is a template, which we can replace...
status = /\|(.*)}}/i.exec(template[0]);
oldTemplate = template[0];
template = 'yes';
if(!status[1]) {
//template exists, but without arguments
status = 'open';
}else{
status = status[1];
}
}
}
//opens the change status window
function openChangeStatusWindow() {
if(!document.getElementById('statusWindow')) {
layout['statusWindow'] = document.createElement('div');
layout['statusWindow'].id = 'statusWindow';
layout['statusWindow'].style.position = 'fixed';
layout['statusWindow'].style.left = '50%';
layout['statusWindow'].style.top = '150px';
layout['statusWindow'].style.zIndex = '2';
layout['statusWindow'].style.background = 'white';
layout['statusWindow'].style.width = '250px';
layout['statusWindow'].style.fontSize = '12px';
layout['statusWindow'].style.border = 'lightblue 2px solid';
var tmp = '<img style="position: absolute; top: 0px; right: 0px; height: 20px; width: 20px;" src="/media/wikipedia/commons/1/1d/WikEd_close_toolbar.png" onClick="closeChangeStatusWindow()"></img>' + changeWindow['title'] + '<br><form name="changeStatus"><table>';
if(status != 'open') {
tmp = tmp + '<tr><td>Open:</td><td><input type="radio" name="newStatus" value="open" /></td></tr>'
}
if(status != 'CU' && status != 'checkuser' && status != 'CUrequest' && status != 'request') {
tmp = tmp + '<tr><td>Request:</td><td><input type="radio" name="newStatus" value="request" /></td></tr>'
}
if(status != 'decline' && status != 'declined') {
tmp = tmp + '<tr><td>Declined:</td><td><input type="radio" name="newStatus" value="declined" /></td></tr>'
}
if(status != 'CUdecline' && status != 'CUdeclined') {
tmp = tmp + '<tr><td>Checkuser declined:</td><td><input type="radio" name="newStatus" value="CUdecline" /></td></tr>'
}
if(status != 'endorse' && status != 'endorsed') {
tmp = tmp + '<tr><td>Endorsed:</td><td><input type="radio" name="newStatus" value="endorsed" /></td></tr>'
}
if(status != 'hold') {
tmp = tmp + '<tr><td>On hold:</td><td><input type="radio" name="newStatus" value="hold" /></td></tr>'
}
if(status != 'moreinfo') {
tmp = tmp + '<tr><td>More Information:</td><td><input type="radio" name="newStatus" value="moreinfo" /></td></tr>'
}
if(status != 'inprogress') {
tmp = tmp + '<tr><td>Inprogress:</td><td><input type="radio" name="newStatus" value="inprogress" /></td></tr>'
}
if(status != 'checked' && status != 'completed') {
tmp = tmp + '<tr><td>Checked:</td><td><input type="radio" name="newStatus" value="checked" /></td></tr>'
}
if(status != 'relist' && status != 'relisted') {
tmp = tmp + '<tr><td>Relist:</td><td><input type="radio" name="newStatus" value="relist" /></td></tr>'
}
if(status != 'close') {
tmp = tmp + '<tr><td>Close:</td><td><input type="radio" name="newStatus" value="close" /></td></tr>'
}
tmp = tmp + '</table><input type="button" onClick="prepare()" value="' + changeWindow['button'] + '" /></form>'
layout['statusWindow'].innerHTML = tmp;
document.getElementsByTagName('body')[0].appendChild(layout['statusWindow']);
}else{
layout['statusWindow'].style.display = 'block';
}
}
//closes the change status window
function closeChangeStatusWindow() {
layout['statusWindow'].style.display = 'none';
}
//get called after a user clicked on Change Status... generates the new source code
function prepare() {
//get choosen Status
for (i=0; i<document.changeStatus.newStatus.length; i++) {
if (document.changeStatus.newStatus[i].checked) {
newStatus = document.changeStatus.newStatus[i].value;
}
}
if(template == 'yes') { //there is already a template, which we just have to replace
if(newStatus != 'open') {
var newSource = source.replace(oldTemplate, '{{SPI case status|' + newStatus + '}}')
}else{
var newSource = source.replace(oldTemplate, '{{SPI case status}}')
}
}else{ //we add a new templte to the top of the page
if(newStatus != 'open') {
var newSource = '{{SPI case status|' + newStatus + '}}\n' + source;
}else{
var newSource = '{{SPI case status}}\n' + source;
}
}
applyChanges(newSource);
}
//applys the changes to the page
function applyChanges(text) {
in_process('start', loadMessage);
//first we get a token...
getToken();
var summary = editSummary.replace('$1', newStatus);
var params = 'action=edit&title=' + encodeURIComponent(wgPageName) + '&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(text) + '&format=xml&minor=1&token=' + encodeURIComponent(editToken);
http_request.open("POST", wgServer + wgScriptPath + '/api.php', false);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
http_request.setRequestHeader("Content-length", params.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(params);
if(http_request.readyState == 4) {
if(http_request.status == 200) {
// success!?
in_process('stop');
response = http_request.responseXML;
if(response.getElementsByTagName('edit')[0]) {
//succes!
var result = http_request.responseXML.getElementsByTagName('edit')[0].getAttribute('result');
}else{
//something went wrong :(
var result = http_request.responseXML.getElementsByTagName('error')[0].getAttribute('info');
}
if(result == "Success") {
//success, so we reload the page
window.location.href = window.location.href;
}else{
alert(error['changePage'].replace('$1', result));
}
}else{
alert(error['retrieve']);
}
}
}
//gets the current edit Token and stores it into the global var editToken
function getToken() {
if(editToken == '') {
http_request.open("GET", wgServer + wgScriptPath + '/api.php?action=query&prop=info&intoken=edit&titles=' + encodeURIComponent(wgPageName) + '&format=xml', false);
http_request.send(null);
if(http_request.readyState == 4) {
if(http_request.status == 200) {
editToken = http_request.responseXML.getElementsByTagName('page')[0].getAttribute('edittoken');
}else{
alert(error['retrieve']);
}
}
}
}
//will show an animation while data gets send
function in_process(startStop, message) {
if(startStop == 'start') {
if(!document.getElementById('in_process')) {
layout['in_process'] = document.createElement('div');
layout['in_process'].id = 'in_process';
layout['in_process'].innerHTML = message;
layout['in_process'].style.position = 'fixed';
layout['in_process'].style.left = '50%';
layout['in_process'].style.top = '300px';
layout['in_process'].style.border = '2px solid red';
layout['in_process'].style.background = 'white';
layout['in_process'].style.zIndex = '4';
layout['in_process'].style.width = '300px';
layout['in_process'].style.height = '75px';
layout['in_process'].style.fontSize = '15px';
document.getElementsByTagName('body')[0].appendChild(layout['in_process']);
}else{
layout['in_process'].innerHTML = message;
layout['in_process'].style.display = 'block';
}
}else{
layout['in_process'].style.display = 'none';
}
}