User:Iceblock/Scripts/Seeking-help.js

From Wikipedia, the free encyclopedia
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.
/*

To use the script, add the following line to Special:MyPage/common.js
importScript('User:Iceblock/Scripts/Seeking-help.js'); // Linkback: [[User:Iceblock/Scripts/Seeking-help.js]]


 * Documentation: This script adds a tab with information on how
 * many Wikipedians currently are looking for help.
 * Clicking the tab will open Category:Wikipedians_looking_for_help 
 *
 *
 * This script is based on the two scripts
 *
 * http://no.wikipedia.org/wiki/MediaWiki:Gadget-HurtigSlett.js
 * http://en.wikipedia.org/wiki/MediaWiki:Gadget-dropdown-menus.js
 *
 * The function createTab2 is taken from 
 * MediaWiki:Gadget-dropdown-menus.js (called createTab there).
 * It is modified not to include 'mid' and to include 'link'.
 * It is also modified to check the page name and set the class
 * name to 'selected' if the page name matches the category name.
 * The rest of this script is based on MediaWiki:Gadget-HurtigSlett.js
 * and modified to check Category:Wikipedians_looking_for_help
 * Under the Vector skin, it places the tab to the left (!) to ensure it is
 * being displayed on special pages.
 *
 * In the function names, 'qd' is replaced with 'seekinghelp'.
 *
 * An unused function (function displayQuickDelete()) is removed from this script.
 * 
 */



// Create cactions LI tab
    function createTab2(cid,ltxt,link) {
        var elements = {
            li: document.createElement('li'),
            a: document.createElement('a'),
            span: document.createElement('span'),
            txt: document.createTextNode(ltxt)
        };
        with (elements) {
            if ( wgPageName == 'Category:Wikipedians_looking_for_help' ) {
                li.className='selected';
            }
            li.id = cid;
            a.href = link;
            span.appendChild(txt); a.appendChild(span); li.appendChild(a);
        }
        return elements.li;
    }



/* Memo to self, den som skal sjekkes mot er denne */
/* http://no.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Kategori:Sider%20som%20er%20foresl%E5tt%20raskt%20slettet&format=xml */
 
/* Vi har to mulige valg å legge denne på: */
/* <div id="p-cactions" class="portlet"> <-- Brukerside, diskusjon, overvåk, etc. */
/* <div class="portlet" id="p-personal"> <-- Brukernavn, min diskusjonsside, innstillinger, etc. */


/* (function displayQuickDelete() removed) */




 
/* init ajax */
function seekinghelp_create_request() {

  try {
    seekinghelp_http = new XMLHttpRequest();
 
  } catch (e) {
    try {
      seekinghelp_http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        seekinghelp_http = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
 
  seekinghelp_http.onreadystatechange = function() {
    if(seekinghelp_http.readyState == 4) seekinghelp_ajax_response();
  }
 
  return true;
}
 

/* make a request */
function seekinghelp_ajax_request() {

  var api_link = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&list=categorymembers&cmtitle=Category:Wikipedians%20looking%20for%20help&format=xml&cmlimit=25';

  if (seekinghelp_create_request () == false) {
     return;
  }
 
  // Then make the request
  seekinghelp_http.open("GET", api_link, true);
  seekinghelp_http.send(null);
}



/* we have received a response */
function seekinghelp_ajax_response() {


  var refresh_time = 60;
  var link = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Category:Wikipedians_looking_for_help&action=purge';
  var items = seekinghelp_http.responseXML.getElementsByTagName('cm');
  //items.length;


  document.getElementById((skin=='vector')?'left-navigation':'p-cactions').getElementsByTagName('div')[0].getElementsByTagName('ul')[0].appendChild(createTab2('ca-seekinghelp', items.length + ' seeking help', link));

 
  /* Repeat */
  /* Actually, I think we should not repeat, check if it is updated on page refresh, and if that is enough */
//  setTimeout("seekinghelp_ajax_request()", refresh_time * 1000);
}
 

addOnloadHook(seekinghelp_ajax_request);