User:Evano1van/ProjectTagger.js

//WikiProject Tagger written based on FWDS

function defineLink(Name, Project){

mw.util.addPortletLink('pt-projtag', 'javascript:doProjTag("' + Project + '","' + Name + '")', Name, '');

}

if (mw.config.get( 'wgNamespaceNumber' ) == 0 ) {

mw.util.addPortletLink ('p-cactions', 'javascript:ProjectTagIt()', 'Project Tag', 'pt-projtag', 'Perform Project Tagging');

// Define custom ones here

defineLink("India", "WikiProject India");

defineLink("Cricket", "WikiProject Cricket");

defineLink("Hinduism", "WikiProject Hinduism");

// End custom defs

}

function ProjectTagIt(){

x = prompt("Enter Project Banner Template Name ");

if (x != null){doProjTag(x,"");}

else{mw.notify("Cancelled Tagging", { autohide: true});}

}

function doProjTag(proj,Name){

mw.notify("Tagging the page...", { title: 'Editing Talk Page', autohide: false});

var url = "//en.wikipedia.org/w/api.php?action=tokens&format=json&type=edit";

var xhr = new XMLHttpRequest();

xhr.open("GET", url); //Code now working, was set to async

xhr.send();

xhr.onreadystatechange = function() {

if (xhr.readyState == 4 && xhr.status == 200) {

var resp = JSON.parse(xhr.responseText);

//console.log("Token first: ", resp.tokens.edittoken);

$.each(resp, function(i,v){console.log(i,"=",v);});

var etoken = resp.tokens.edittoken;

console.log("The token is: ", etoken);

if (proj != null){

var editData = {

action: "edit",

format: "json",

summary: Name + " Project Tagging using Project Tagger",

token: etoken,

title: 'Talk:' + mw.config.get('wgPageName'),

prependtext: "\{\{" + proj + "|importance=|quality=\}\}"

};

$.each(editData, function(i,v){console.log(i,"=",v);});

$.post('//en.wikipedia.org/w/api.php', editData,

function(data){

jQuery.each(data,

function( index, value ) {

//console.log( "index", index, "value", value );

mw.notify("Talk:" + mw.config.get('wgPageName') + " tagged with " + proj, { title: 'Successfully tagged', autohide: true});

});

});

}

}

}

}