User:Colchicum/tagwpa.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
// Script for rapid tagging of articles, originally by [[User:DLX|DLX]].
 
 addOnloadHook(WPAButton);
 
 function WPAButton() {  
     if ((document.URL.indexOf(':', 8) != -1) && (document.URL.indexOf('Talk:') == -1)) return;  
     if (document.URL.indexOf('/wiki/') == -1) return;
     addPortletLink(chooseBox('tWP:A', TwinkleConfig.toolboxButtons), "javascript:wpatag()", "Tag WP:A", "", "", "");
 }
 
 function wpatag() {
     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", wgServer + 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 Albania.svg|thumb|center')!= -1) {
         Status.status('Already tagged, but with the old template. Consider replacing it manually.');
         if (TagWPAConfig.OpenAfter == true) {
             window.location = wgServer + wgArticlePath.replace('$1', oPage.replace('_', ' '));
         }
         return;
     }
 
     if (sText.indexOf("{{WikiProject Albania") != -1) {
         Status.status('Already tagged!');
         if (TagWPAConfig.OpenAfter == true) window.location = wgServer + 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 = "{{WikiProject Albania}}\n" + sText;
 
     var summary = 'Added to [[Wikipedia:WikiProject Albania]]';
     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' , wgServer + 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 (TagWPAConfig.OpenAfter == true) {
             window.location = wgServer + wgArticlePath.replace('$1', oPage.replace('_', ' '));
             Status.status('Loading the talk page...');
         }
     }
     xmlhttp.send(QueryString.create(postData));
 }