Jump to content

User:Ais523/watchlistei.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.
//<pre><nowiki>
// Watchlist export/import ([[User:ais523/watchlistei.js]]), by [[User:ais523]].

// To use this script, visit [[Special:Watchlist]] and follow the new links for 'import' or 'export'
// there. The exported watchlist can be copy/pasted into the edit box for import, and is also legal
// (but somewhat obfuscated) wikitext for use with [[Special:Recentchangeslinked]]. Doesn't work in
// Internet Explorer, does work in Firefox 1, I haven't checked other browsers.

// To install this script, the easiest way is to type {{subst:js|User:ais523/watchlistei.js}} at the
// bottom of [[Special:Mypage/monobook.js]], and bypass your cache (Ctrl-F5 in Firefox for Windows).

// Each line of an exported watchlist is in the form
// *[[urlencodedpagename]]

// Note that this feature is now part of the software, making this script redundant.

if(navigator.userAgent.indexOf("MSIE")==-1) // fail gracefully in IE
{
// AJAX
var wleiwpajax;
// From [[WP:US]] mainpage (wpajax renamed to wleiwpajax)
wleiwpajax={
        download:function(bundle) {
                // mandatory: bundle.url
                // 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()
                : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
                : false;
                
                if (x) {
                        x.onreadystatechange=function() {
                                x.readyState==4 && wleiwpajax.downloadComplete(x,bundle);
                        };
                        x.open("HEAD",bundle.url,true);
                        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));
        }
};

var wleiRunOnce=1;
// Links to export and import
$(function(){
  if(wgPageName=="Special:Watchlist"&&location.href.indexOf("list/")==-1&&wleiRunOnce)
  {
    wleiRunOnce=0;
    var t=document.getElementById('bodyContent').getElementsByTagName('hr')[0];
    var n=document.createElement("p");
    t.parentNode.replaceChild(n,t);
    n.innerHTML="You can also <a href='http://en.wikipedia.org/wiki/Special:Watchlist/edit"+
    "?ais523wlei=export'>export</a> this watchlist or <a href='http://en.wikipedia.org/wiki/"+
    "Special:Watchlist/edit?ais523wlei=import'>import</a> pages from an exported watchlist.<hr/>";
  }
});

var wleiExRunOnce=1;
// Exporting: producing the list
$(function(){
  if(location.href.indexOf("Special:Watchlist/edit?ais523wlei=export")!=-1&&wleiExRunOnce)
  {
    wleiExRunOnce=0;
    var a=document.getElementById('bodyContent').getElementsByTagName('input');
    var b=new Array();
    b[0]="<h2>Exported watchlist:</h2>\n<pre>";
    var i=0;
    while(i<a.length-1)
    {
      b[i+1]="*[[:"+encodeURIComponent(a[i].value)+"]]";
      i++;
    }
    b[i+1]="</pre>";
    document.getElementById('bodyContent').innerHTML=b.join('\n');
  }
});

//Importing: textarea to input the watchlist
$(function(){
  if(location.href.indexOf("Special:Watchlist/edit?ais523wlei=import")!=-1)
  {
    document.getElementById('bodyContent').innerHTML=
      "Watchlist to import:<br/><textarea id='watchlistImport' rows=20 cols=80></textarea><br/>\n"+
      "<a href='javascript:wleiImport()'>Import watchlist</a><br/>\n"+
      "Pages not yet processed: <span id='wlein'>0</span>, retrying: <span id='wleif'>0</span> "+
      "(total retries: <span id='wleir'>0</span>, 4 retries max per page allowed), "+
      "succeeded: <span id='wleis'>0</span>, failed: <span id='wleic'>0</span>.<br />\n"+
      "Return to <a href='http://en.wikipedia.org/wiki/Special:Watchlist'>watchlist</a>.\n<div id='faillist'></div>";
  }
});

function wleiImport()
{
  var a=document.getElementById('watchlistImport').value.split('\r').join('\n').split('\n');
  var i=a.length;
  while(i--)
  {
    var m=a[i].match(/^\** *\[\[(.*)\]\] *$/);
    if(m!=undefined&&m[1]!=undefined&&m[1]!="")
    {
      watchPageUsingAJAX(m[1]);
      document.getElementById('wlein').innerHTML=
        1+new Number(document.getElementById('wlein').innerHTML);
    }
  }
}

function wleiSuccess(a,b)
{
  document.getElementById('wleis').innerHTML=
    1+new Number(document.getElementById('wleis').innerHTML);
  if(b.isRetrying)
  {
    document.getElementById('wleif').innerHTML=
      new Number(document.getElementById('wleif').innerHTML)-1;
  }
  else
  {
    document.getElementById('wlein').innerHTML=
      new Number(document.getElementById('wlein').innerHTML)-1;
  }
}
function wleiFailure(a,b)
{
  if(!b.isRetrying)
  {
    document.getElementById('wleif').innerHTML=
      1+new Number(document.getElementById('wleif').innerHTML);
    document.getElementById('wlein').innerHTML=
      new Number(document.getElementById('wlein').innerHTML)-1;
  }
  b.isRetrying++;
  if(b.isRetrying<5)
  {
    document.getElementById('wleir').innerHTML=
      1+new Number(document.getElementById('wleir').innerHTML);
    window.setTimeout(function(){wleiwpajax.download(b);},a.getResponseHeader('Retry-After')*1000);
  }
  else
  {
    document.getElementById('wleif').innerHTML=
      new Number(document.getElementById('wleif').innerHTML)-1;
    document.getElementById('wleic').innerHTML=
      new Number(document.getElementById('wleic').innerHTML)+1;
    if(document.getElementById('faillist').innerHTML=='') document.getElementById('faillist').innerHTML='Failed:';
    document.getElementById('faillist').innerHTML+=
      '<br/>*[[<a href="'+b.url.split("&maxlag=5").join("")+'">'+b.urlencodedpagename+'<a>]]';
  }
  return 1;
}

function watchPageUsingAJAX(urlencodedpagename)
{
  wleiwpajax.download({url:'http://en.wikipedia.org/w/index.php?title='+urlencodedpagename+
    '&action=watch&maxlag=5', onSuccess:wleiSuccess, onFailure:wleiFailure, isRetrying:0,
    urlencodedpagename:urlencodedpagename});
}
}
//</nowiki></pre>
//[[Category:Wikipedia scripts]]