Jump to content

User:Henrik/js/dyk-notifier.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.
// <nowiki>
// <pre>

/*
To install, add 

// [[User:Henrik/dyk-notifier]]
importScript('User:Henrik/js/dyk-notifier.js'); 

to your monobook.js (located at User:YOURUSERNAME/monobook.js)
*/

function mylog(text) {
var p2 = document.createElement("p");
p2.appendChild(
  document.createTextNode(text)
);
p2.setAttribute("class", "labrat");
var bottle = document.getElementById("log");
bottle.appendChild(p2);
}

function mydate()
{
  var months = ["January","February","March","April",
                "May","June","July","August",
                "September","October","November","December"];

  var currentTime = new Date();
  var month = currentTime.getMonth();
  var day = currentTime.getDate();
  var year = currentTime.getFullYear();
  return ""+day+" "+months[month]+"|"+year;
}

function clear(){
  var parent = document.getElementById("log");
  while ( parent.hasChildNodes() ) { parent.removeChild(parent.firstChild); }
}

function makeXhr() {
  xhr = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         xhr = new XMLHttpRequest();
         if (xhr.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //xhr.overrideMimeType('text/xml');
            xhr.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               xhr = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!xhr) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
  return xhr;
}

function makePOSTRequest(url, parameters) {           
   //xhr.onreadystatechange = alertContents;
   var xhr = makeXhr();  
   xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xhr.setRequestHeader("Content-length", parameters.length);
   xhr.setRequestHeader("Connection", "close");
   xhr.onreadystatechange  = function()
     { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) {
                 mylog("successful!"); 
              }
         }
      }
   xhr.open('POST', url, false);
   xhr.send(parameters);
}
  

function postTalk(user,msg,summary)
{ 
    var xhr = makeXhr();
    xhr.onreadystatechange = function(){ 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) {
                 var doc = xhr.responseXML;  
                 var form = xhr.responseXML.getElementById( 'editform' );

                 var pstr = 'wpEditToken='+encodeURIComponent(form.wpEditToken.value) + "&"+
                            'wpAutoSummary='+encodeURIComponent(form.wpAutoSummary.value) + "&"+
			    'wpStarttime='+encodeURIComponent(form.wpStarttime.value) + "&"+
			    'wpEdittime='+encodeURIComponent( form.wpEdittime.value) + "&"+
			    //'wpSave='+encodeURIComponent("Save page") + "&" +
                            'wpSection=new&'+
                            'wpSummary='+encodeURIComponent("foo") + "&" +
                            'wpTextbox1='+encodeURIComponent("foo");

                 alert(form.action); alert(pstr); 
                 makePOSTRequest(form.action,pstr);
              } 
         }
    }; 
   xhr.overrideMimeType('text/xml');
   xhr.open('GET', "http://en.wikipedia.org/w/index.php?title="+user+"&action=edit",  false); 
   xhr.send(null); 
}
 
function xpathfind(findPattern) {
 return document.evaluate( findPattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
}

function dykNotifyAll(autosave) {
 allLinks = xpathfind("//div[@id=\"credits\"]/ul/li");
 for(i=0;i<allLinks.snapshotLength;i++) {
   var idx = i + 1;
   articleLink = xpathfind("//div[@id=\"credits\"]/ul/li["+idx+"]//a[1]");
   var title = articleLink.snapshotItem(0).textContent;

   mylog(title);

   var atalkurl = articleLink.snapshotItem(0).href.replace("/wiki/","/wiki/Talk:")+"?action=edit&xamfind="+escape("({{.*}})?(.*\n=.*=)?")+"&amreplace="+escape("$1\n{{dyktalk|"+mydate()+"}}\n$2")+"&amsummary=Dyktalk"+autosave;

   postTalk("User_talk:Henrik/sandbox/test1","hello ~~"+"~~","hi");

   //mylog(" -- talk page: " + atalkurl);
   resultLinks = xpathfind("//div[@id=\"credits\"]/ul/li["+idx+"]//a[starts-with(@title,'User talk:')]");
   if ( (res = resultLinks.snapshotItem(0) ) != null ){ 
      authormsg = escape("{"+"{subst:UpdatedDYK|"+mydate()+"|"+title+"}} --~~"+"~~");
      authorurl =  res.href+"?action=edit&section=new&amsummary="+escape(title)+"&amaddbefore="+authormsg+autosave;
      //window.open(authorurl,"authortalk"+i); 
      mylog(" -- author: " + res.textContent); 
   }
   if ( (res = resultLinks.snapshotItem(1) ) != null ){ 
      authormsg = escape("{"+"{subst:UpdatedDYKNom|"+mydate()+"|"+title+"}} --~~"+"~~");
      authorurl =  res.href+"?action=edit&section=new&amsummary="+escape(title)+"&amaddbefore="+authormsg+autosave;
      //window.open(authorurl,"notifiertalk"+i); 
      mylog(" -- nominator: " + res.textContent); 
   }
 }
}

function insertDykButtons() {
  if (mw.config.get('wgPageName') != "User:Henrik/sandbox" && 
      mw.config.get('wgPageName') != "Template:Did_you_know/Next_update" && 
      mw.config.get('wgPageName') != "Template:Did_you_know/Next_update/Clear")
    return;

  d=document.createElement('div')
  var b=document.createElement('input');
  b.type='button';
  b.value='Notify contributors';
  b.onclick=function(){
    dykNotifyAll("");    
  }
  d.appendChild(b);

  var b=document.createElement('input');
  b.type='button';
  b.value='Notify contributors (autosave)';
  b.onclick=function(){
    dykNotifyAll("&amautosave=1");    
  }
  d.appendChild(b);
  document.getElementById("credits").appendChild(d);
}

$(insertDykButtons);
// </nowiki>