User:Digwuren/tagwpe.js

// // Script for rapid tagging of articles, originally by DLX.

$(WPEButton);

function WPEButton { if ((document.URL.indexOf(':', 8) != -1) && (document.URL.indexOf('Talk:') == -1)) return; if (document.URL.indexOf('/wiki/') == -1) return; mw.util.addPortletLink(chooseBox('tWP:E', TwinkleConfig.toolboxButtons), "javascript:wpetag", "Tag WP:E", "", "", ""); }

function wpetag { Status.init(document.getElementById('bodyContent')); Status.status('Checking for redirect: ' + wgPageName);

b=sajax_init_object; b.open("GET", "http://en.wikipedia.org/w/api.php?action=query&titles=" + wgPageName + "&format=json", false); b.onreadystatechange = function { if(b.readyState != 4) return; w = b.responseText.slice(b.responseText.indexOf('"title":"') + 9, b.responseText.length - 5);       if (w.indexOf("\u") == -1) wgPageName = w;        Status.status('Page evaluated to: ' + wgPageName);    };    b.send("");

if (wgPageName.indexOf('Talk:') == -1) wgPageName = 'Talk:' + wgPageName; a=sajax_init_object; a.open("GET", mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=" + wgPageName + "&action=raw", true); a.onreadystatechange = function { if (a.readyState != 4) return; textReceived(a.statusText, a.responseText, wgPageName); };   a.send(""); }

function textReceived(sStatus, sText, oPage) { Status.status('Page contents received: ' + oPage);

if (sText.indexOf('Image:Flag of Estonia.svg|thumb|center')!= -1) { Status.status('Already tagged, but with the old template. Consider replacing it manually.'); if (TagWPEConfig.OpenAfter == true) { window.location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', oPage.replace('_', ' ')); }       return; }

if (sText.indexOf("{{"+"WikiProject Estonia") != -1) { Status.status('Already tagged!'); if (TagWPEConfig.OpenAfter == true) window.location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', oPage.replace('_', ' ')); return; }   if (sText == "") { Status.status("Talk page doesn't exist, ready to tag."); }

var xmlhttp = sajax_init_object; xmlhttp.overrideMimeType('text/xml'); Status.status("Grabbing edit form..."); xmlhttp.open('GET', 'http://en.wikipedia.org/w/index.php?title=' + oPage + '&action=submit', true); xmlhttp.onreadystatechange = function { if (xmlhttp.readyState != 4) return; formReceived(xmlhttp.responseXML, sText, oPage); };   xmlhttp.send("") }

function formReceived(pg, sText, oPage) { form = pg.getElementById('editform');

if (!form) { Status.error("Couldn't grab element 'editform' -- aborting, this could indicate failed response from the server"); return; } else { Status.status('Got the edit form'); }

text = "\n" + sText;

var summary = 'Added to WikiProject Estonia'; var postData = { 'wpMinoredit': form.wpMinoredit.checked, 'wpWatchthis': form.wpWatchthis.checked, 'wpStarttime': form.wpStarttime.value, 'wpEdittime': form.wpEdittime.value, 'wpAutoSummary': form.wpAutoSummary.value, 'wpEditToken': form.wpEditToken.value, 'wpSummary': summary, 'wpTextbox1': text };

Status.status('Submitting the form...');

var xmlhttp = sajax_init_object; xmlhttp.overrideMimeType('text/xml'); xmlhttp.open('POST', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=" + oPage + "&action=submit", true); xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xmlhttp.onload = function { Status.status('Form submitted'); if (TagWPEConfig.OpenAfter == true) { window.location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', oPage.replace('_', ' ')); Status.status('Loading the talk page...'); }   }    xmlhttp.send(QueryString.create(postData)); } //