Suradnik:Dvorsky/catwatch.js

Izvor: Wikipedija

Napomena: Nakon objave možda ćete trebati očistiti međuspremnik svog preglednika kako biste vidjeli promjene.

  • Firefox / Safari: držite Shift i pritisnite Reload, ili pritisnite bilo Ctrl-F5 ili Ctrl-R (⌘-R na Macu)
  • Google Chrome: pritisnite Ctrl-Shift-R (⌘-Shift-R na Macu)
  • Internet Explorer / Edge: držite Ctrl i kliknite Refresh, ili pritisnite Ctrl-F5
  • Opera: pritisnite Ctrl-F5.
/* Category watcher ([[User:ais523/catwatch.js]]); adds entries on the watchlist if a page is
   added to a category (and is still in the category when the watchlist is checked). The entry
   will only appear if there is another page on the watchlist edited that day (if the addition
   was the only change on that day, it's neglected). The recategorisation of a page in a watched
   category will also trigger the watching, even if it's a different category that's added or
   removed (unless the watched category is removed); removals from the watched category can't
   be detected.

   A previous version of this script had a limit on the maximum size of a category that could be
   watched; this limit no longer exists.
   
   Thanks to [[User:TheDJ]] for helping me make this compatible with more browsers and preference
   settings, and to Roan Kattouw for fixing [[bugzilla:11275]] and making this cheap on the servers.
*/

//<syntaxhighlight lang="javascript">

/*function DHTMLloadscript( url ) {
  var e = document.createElement("script");
  e.setAttribute('src', url);
  e.setAttribute('type',"text/javascript");
  e.appendChild( document.createTextNode("") ); // Safari bugfix
  document.getElementsByTagName("head")[0].appendChild(e);
};*/

var cwwpajax;
// From [[WP:US]] mainpage (wpajax renamed to cwwpajax)
cwwpajax={
        download:function(bundle) {
                // mandatory: bundle.url
                // mandatory: bundle.async
                // optional:  bundle.onSuccess (xmlhttprequest, bundle)
                // optional:  bundle.onFailure (xmlhttprequest, bundle)
                // optional:  bundle.otherStuff OK too, passed to onSuccess and onFailure
                
                var x = window.XMLHttpRequest ? new XMLHttpRequest() // Firefox, Safari, Opera, IE7
                : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") // IE6
                : false;
                
                if (x) {
                        x.onreadystatechange=function() {
                                x.readyState==4 && cwwpajax.downloadComplete(x,bundle);
                        };
                        x.open("GET",bundle.url,bundle.async);
                        x.send(null); 
                }
                return x;
        },

        downloadComplete:function(x,bundle) {
                x.status==200 && ( bundle.onSuccess && bundle.onSuccess(x,bundle) || true )
                || ( bundle.onFailure && bundle.onFailure(x,bundle) || alert(x.statusText));
        }
};

// Example:
// function dlComplete(xmlreq, data) {
//      alert(data.message + xmlreq.responseText);
// }
//  cwwpajax.download({url:'http://en.wikipedia.org/w/index.php?title=Thresher&action=raw', 
//                   onSuccess: dlComplete, message: "Here's what we got:\n\n" });

// End of [[WP:US]] quote

function cwOntoWatchlist(xmlreq, data)
{
  var a=xmlreq.responseText;
  var c=data.catname;
  var i;
  var diag="";
  if(a.indexOf("<categorymembers/>")!=-1) return; //cat is empty, nothing to do here
  var latesttime=0;
  var latestpage="";
  var temp, temp2;
  latestpage=a.split('title="')[1].split('" timestamp=')[0];
  latesttime=a.split('timestamp="')[1].split('"/>')[0];
  a=document.getElementsByTagName("h4"); //get dates in the watchlist
  var monthname=new Array();
  monthname['01']="siječanj";
  monthname['02']="veljača";
  monthname['03']="ožujak";
  monthname['04']="travanj";
  monthname['05']="svibanj";
  monthname['06']="lipanj";
  monthname['07']="srpanj";
  monthname['08']="kolovoz";
  monthname['09']="rujan";
  monthname['10']="listopad";
  monthname['11']="studeni";
  monthname['12']="prosinac";
  latesttime=new String(latesttime);
  
  var mn1, mn2, mn3, mn4;
  
  // Allow for different date styles:
  // Siječanj 29, 2001
  mn1=""+monthname[latesttime.substr(5,2)]+" "+new Number(latesttime.substr(8,2))+
      ", "+latesttime.substr(0,4);
  // 29 siječanj 2001
  mn2=""+new Number(latesttime.substr(8,2))+". "+monthname[latesttime.substr(5,2)]+
      " "+latesttime.substr(0,4);
  // 2001 siječanj 29
  mn3=""+latesttime.substr(0,4)+" "+monthname[latesttime.substr(5,2)]+
      " "+new Number(latesttime.substr(8,2));
  // 2001-01-29
  mn4=""+latesttime.substr(0,4)+"-"+latesttime.substr(5,2)+"-"+latesttime.substr(8,2);

  i=a.length;
  while(i--)
  {
    if(a[i].innerHTML==mn1||a[i].innerHTML==mn2||a[i].innerHTML==mn3||a[i].innerHTML==mn4)
    {
      diag+=a[i].innerHTML+":";
      temp=a[i].nextSibling.firstChild;
      if(temp==null) temp=a[i].nextSibling.nextSibling.firstChild;
      while(temp!=null)
      {
        diag+=temp.tagname+"!";
        if(temp.tagName!=null)
        if(temp.tagName.toLowerCase()=="li")
        {
          temp2=temp.innerHTML.match(/; ([0-9][0-9]):([0-9][0-9])(:[0-9][0-9])? \. \./);
          temp2=new Number(temp2[1])*100+new Number(temp2[2]);
          diag+=temp2+",";
          if(temp2<new Number(latesttime.substr(11,2)+latesttime.substr(14,2))) {temp2=temp; break;}
        }
        temp2=temp; temp=temp.nextSibling;
        if(temp==null) break;
      }
      temp=document.createElement("li");
      if(temp2==null) {return;}
      if(a[i].nextSibling.firstChild!=null) // IE-like whitespace handling
        a[i].nextSibling.insertBefore(temp,temp2);
      else // Firefox-like whitespace handling
        a[i].nextSibling.nextSibling.insertBefore(temp,temp2);

      temp.innerHTML="(diff) (hist) . . <a "+
                     "href='http://hr.wikipedia.org/wiki/Kategorija:"+
                     encodeURI(c)+
                     "'>Kategorija:"+c+"</a>; "+latesttime.substr(11,2)+":"+latesttime.substr(14,2)+
                     " . . (+ <a "+
                     "href='http://hr.wikipedia.org/wiki/"+
                     encodeURI(latestpage)+"'>"+latestpage+"</a> "+
                     "(<a href='http://hr.wikipedia.org/w/index.php?title="+encodeURI(latestpage)+
                     "&diff=last'>last</a>))";
      return;
    }
  }
}

var WatchedCategories;

//An example of a query to check a category would be

//http://hr.wikipedia.org/w/api.php?action=query&list=categorymembers&cmcategory=Wikipedia%20protected%20edit%20requests&cmlimit=1&cmprop=title|timestamp&cmsort=timestamp&cmdir=desc&format=xml
$(function(){
  //DHTMLloadscript( 'http://hr.wikipedia.org/w/index.php?title=User:'+wgUserName
  //  +'/WatchedCategories.js&action=raw&ctype=text/javascript');
  if(wgPageName=="Special:Watchlist")
  {
    if(document.getElementsByTagName("tt").length>0)
    {
      alert("You seem to be using the Javascript-enhanced watchlist, which is incompatible "+
            "with the category watchlist script at present; please uninstall it.");
      return;
    }
    // load the script using AJAX, because DHTML fails in Firefox and document.write fails in Safari
    eval(cwwpajax.download({url:'http://hr.wikipedia.org/w/index.php?title=Suradnik:'+wgUserName
                               +'/WatchedCategories.js&action=raw&ctype=text/javascript',
                               async:false}).responseText);
    if(WatchedCategories==undefined)
    {
      if(confirm("Još nemaš listu praćenih kategorija. Želiš li kreirati jednu?"))
        location.href='http://hr.wikipedia.org/w/index.php?title=Suradnik:'+wgUserName+
        '/WatchedCategories.js&action=edit&preload=Suradnik:Dvorsky/WatchedCategoriesTemplate.js';
    }
    else
    {
      var i;
      for(i=0; i<WatchedCategories.length; i++)
      {
        var a=WatchedCategories[i];
        cwwpajax.download({url:'http://hr.wikipedia.org/w/api.php?action=query&list=categorymembers&'
                           +'cmcategory='+encodeURI(a)+'&cmlimit=1&cmprop=title'
                           +'|timestamp&cmsort=timestamp&cmdir=desc&format=xml',
                           onSuccess:cwOntoWatchlist,
                           catname:a, async:true});
      }
    }
  }
});

//</syntaxhighlight>
//<!--[[Kategorija:Wikipedijine skripte]]-->