User:Ais523/watchlistei.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Ais523/watchlistei. |
//<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]]