User:XXN/me2.js

/* Script to Append/Prepend/(Replace all page text) for each page its own value TODO: to merge the function into the main script function removeBlanks(arr) { var ret = []; var i, len; for (i = 0, len = arr.length; i < len; i++) { var s = arr[i]; s = s.trim; if (s) { ret.push(s); }		}		return ret; }

function doMassEdit2 { var articles = document.getElementById("wpMassEdit2Pages").value.split("\n"); articles = removeBlanks(articles); if (!articles.length) { return; }   var text1 = document.getElementById("wpMassEdit2Text1").value.split("\n"); text1 = removeBlanks(text1); if (!text1.length) { return; }   var wpMassEdit2Type = document.getElementById("wpMassEdit2Type").value, wpMassEdit2Summary = document.getElementById("wpMassEdit2Summary").value, edited = 0, failed = new Array, error = new Array; var wpMassEdit2Minor = document.getElementById("wpMassEdit2Minor").checked; for(i=0;i 0 && text01.length > 0) { var req = new XMLHttpRequest; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?format=json&action=query&prop=info&intoken=edit&titles=" + encodeURIComponent(article), false); req.send(null); var response = eval("(" + req.responseText + ")").query.pages; for(var index in response) { var info = response[index]; var edittoken = info.edittoken; var postdata = "format=json" + "&action=edit&watchlist=nochange" + "&title=" + encodeURIComponent(article) + "&summary=" + encodeURIComponent(wpMassEdit2Summary) + "&token=" + encodeURIComponent(edittoken); if(wpMassEdit2Minor) postdata += "&minor=1"; else postdata += "&notminor=1"; if(wpMassEdit2Type == "prepend"){ postdata += "&prependtext=" + encodeURIComponent(text01 + "\n"); }		else if(wpMassEdit2Type == "append"){ postdata += "&appendtext=" + encodeURIComponent("\n" + text01); }		else if(wpMassEdit2Type == "replacepage"){ postdata += "&text=" + encodeURIComponent(text01); }               var req = new XMLHttpRequest; req.open("POST", mw.config.get('wgScriptPath') + "/api.php", false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", postdata.length); req.send(postdata); if(eval("(" + req.responseText + ")").edit ) { //If edited, update the count and the button. edited++; document.getElementById("wpMassEdit2Submit").value = "(" + edited + ")"; } else { //If not edited, add the title to the "failed" array and a description of the error to the "error" array. failed.push(article); error.push(eval("(" + req.responseText + ")").error.info); }           }        }        if(!articles[i+1]) { document.getElementById("wpMassEdit2Submit").value = "Done (" + edited + ")"; if(failed.length > 0) { var linkedList = ""; for(x=0; x" + failed[x] + ": " + error[x] + ""; //Links the titles in the "failed" array }               document.getElementById("wpMassEdit2FailedContainer").innerHTML += ' Failed edits:' + linkedList + ''; }       }    } }

function gettext(title){ var req = sajax_init_object; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(title), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; if (pageid == "-1") { delete req; return ''; }	var pagetext = response['query']['pages'][pageid]['revisions'][0]['*']; return pagetext; } function masseditform2 { var bodyContent = (mw.config.get('skin') == "cologneblue" ? "article" : "bodyContent"); document.getElementsByTagName("h1")[0].textContent = "XXN's mass edit tool V.2"; document.title = "XXN's mass-editing tool V.2"; document.getElementById(bodyContent).innerHTML = 'For Wikipedia and other wikis

'       + '' + 'If you abuse this tool, it\'s your fault, not mine.' + ' '       + '

'           + 'Pages to edit (one on each line, please): '               + ' ' + 'Text 1 (to Append/Prepend for each its own value, or to Replace all page text): '               + ' ' + '

'               + '  Edit type: ' + ' ' + ' Prepend text 1 ' + ' Append text 1 ' + ' Replace all page text with text 1 ' + ' '            + '  Edit summary: ' + '  ' + ' Mark edit as minor: ' + '  ' + '  ' + ' '; }

if(mw.config.get('wgNamespaceNumber') == -1 && mw.config.get('wgTitle').toLowerCase == "massedit2") { masseditform2; };