User:Lupin/watchlistDumper.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

// <pre><nowiki>

// Dump your watchlist into an edit page

// Installation: add 
//
// {{subst:js|User:Lupin/watchlistDumper.js}} 
// 
// to your user javascript file.

// Usage: type 
// 
// javascript:watchlistDumper.dump()
// 
// into the address bar of your browser when editing the relevant page.
// 
// Alternatively, bookmark this as a url for easy access, 
// or add a link to your toolbox.

watchlistDumper = {

  download : function(bundle) {
	var x = window.XMLHttpRequest ? new XMLHttpRequest()
	: window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
	: false;
	
	if (x) {
	  x.onreadystatechange=function() {x.readyState==4 && watchlistDumper.downloadComplete(x,bundle);};
	  x.open("GET",bundle.url,true); 
	  x.send(null); 
	}
  },

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

  processWatchlist : function(req, bundle) {
	try { var editbox=document.editform.wpTextbox1; } catch (err) { 
	  alert('Please edit the page where you want to dump your watchlist'); return; 
	}
	
	var watchlist=[];
	var lines=req.responseText.split('\n');
	for (var i=0; i<lines.length; ++i) {
	  if (lines[i].indexOf('<li><input type="checkbox" name="id[]" value=') > -1) {
		var article=lines[i].replace(/.*title="(.*?)">.*/, '$1');
		if (/^(Category|Image)/.test(article)) { article = ':' + article; }
		watchlist.push(article);
	  }
	}
	if (watchlist.length > 0) {
	  editbox.value += '*[[' + watchlist.join(']]\n*[[') + ']]\n';
	} 
	alert( 'Dumped ' + watchlist.length + ' watched pages' );
  },

  dump : function() {
	watchlistDumper.download({
       url: 'http://en.wikipedia.org/wiki/Special:Watchlist/edit', 
			  onSuccess: watchlistDumper.processWatchlist,
			  onFailure: function(){alert('Watchlist download failed for some reason; please try again.');}
	});
  }

}
//</nowiki></pre>