User:Evano1van/ProjectTagger.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. |
This user script seems to have a documentation page at User:Evano1van/ProjectTagger. |
//WikiProject Tagger written based on [[WP:FWDS|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 [[User:Evano1van/ProjectTagger|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});
});
});
}
}
}
}