Jump to content

User:Evano1van/ProjectTagger.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//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});
                    });
              });
     }
  }
 }
}