Jump to content

User:Gene93k/fwds-fork.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.
// This is a temporary fork of User:Fox Wilson/delsort.js (FWDS)

function defineLink(category){

  mw.util.addPortletLink('pt-delsort', 'javascript:window.doDelsort("' + category + '")', category, '');

}

if (mw.config.get('wgPageName').indexOf('Wikipedia:Articles_for_deletion/') != -1 && mw.config.get('wgPageName').indexOf('Wikipedia:Articles_for_deletion/Log/201') == -1) {
mw.util.addPortletLink ('p-cactions', 'javascript:delsortIt()', 'FWDelsort', 'pt-delsort', 'Perform deletion sorting');
// // Define custom ones here
// defineLink("People");
// 
// defineLink("Academics and educators");
// 
// defineLink("Actors and filmmakers");
// 
// defineLink("Authors");
// 
// defineLink("Bands and musicians");
// 
// defineLink("Businesspeople");
// 
// defineLink("Politicians");
// 
// defineLink("Sportspeople");
// 
// defineLink("Organizations");
// 
// defineLink("Football");
// 
// defineLink("Martial arts");
// 
// defineLink("Albums and songs");
// 
// defineLink("Television");
// 
// defineLink("Music");
// 
// defineLink("Film");
// 
// defineLink("United States of America");
// 
// defineLink("New York");
// 
// defineLink("California");
// 
// defineLink("United Kingdom");
// 
// defineLink("England");
// 
// defineLink("India");
// 
// defineLink("Christianity");
// 
// defineLink("Fictional elements");
// 
// defineLink("Business");
// 
// 
// defineLink("Software");
// 
// 
// 
// defineLink("Internet");
// 
// defineLink("Websites");
// 
// defineLink("Australia");
// // End custom defs
}

function delsortIt(){

  x = prompt("Category: ");

  if (x != null){doDelsort(x);}

  else{alert('Delsort cancelled!');}

}
window.doDelsort = doDelsort;
function doDelsort(cat){

  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 (cat != null){
  var editData = {               
                  action: "edit",
                  format: "json",
                  summary: "Listing at [[WP:DELSORT]] under " + cat + " ([[WP:FWDS|FWDS]] (fork))",
                  token: etoken,
                  title: mw.config.get('wgPageName'),
                  appendtext: "\{\{subst:Delsort|" + cat + "|\~\~\~\~\}\}"
  };
  $.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 );

});});
// Add to the Delsort page 

  //First, get the content

  $.post('//en.wikipedia.org/w/api.php', 
{action:'query', format:'json', prop:'revisions', rvprop:'content', rvlimit:'1', titles:'Wikipedia:WikiProject Deletion sorting/'+cat}, 

function(data){

  $.each(data.query.pages, 

    function(i,v){

      window.editContent = v.revisions[0]['*'].replace('directly below this line -->', 'directly below this line -->\n\{\{' + mw.config.get('wgPageName') + '\}\}');

    } );

  var editData = {               
                  action: "edit",
                  format: "json",
                  token: etoken,

                  summary: "Listing [[" + mw.config.get('wgPageName') + "]] ([[WP:FWDS|FWDS]] (fork))",
                  title: 'Wikipedia:WikiProject Deletion sorting/' + cat,
                  text: window.editContent
  };
  $.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 );});});
  alert('Delsort complete.');
  }
);

}
    }
  }
}