User:JackSchmidt/JS CatWatch.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:JackSchmidt/JS CatWatch. |
// Fancy thing to watch all articles in a category
// (c)2007 Jack Schmidt and Kathryn Lybarger available under GPL, BSD, or CC-SA
if(false) {
// This (disabled) code adds a new tab "CatWatch"...
// Which when clicked adds buttons next to all category links...
// Which when clicked lookup how many articles are in the category,
// and when clicked again add those articles to your watchlist
// and then disappears.
// You can try it out. Just add this to your main js file:
// Begin catwatch install snippet
importScript('User:JackSchmidt/JS Ajax.js');
importScript('User:JackSchmidt/JS Watchlist.js');
importScript('User:JackSchmidt/JS CatWatch.js');
$( function () {
mw.util.addPortletLink('p-cactions',"javascript:JS_CatWatch.AddThemAll()",
"CatWatch", "ca-catwatch", "Add 'Watch By Category' buttons");
});
// End snippet
// And then reload an article page. Careful not to pollute your watchlist.
}
// Code to add a lookup button to a category link,
// which finds all articles in the category, and
// makes a button to add them to your watchlist.
JS_CatWatch = {};
JS_CatWatch.AddLookupButton = function( node, catname ) {
var but;
but = document.createElement( "input" );
but.type="button";
but.value="Lookup '" + catname + "'";
but.onclick = JS_CatWatch.LookupFunction( node, but, catname );
node.parentNode.appendChild( but, node );
}
JS_CatWatch.LookupFunction = function( node, but, catname ) {
return function() { return JS_CatWatch.Lookup( node, but, catname ); }
}
JS_CatWatch.Lookup = function( node, but, catname ) {
but.value = "Looking up '" + catname + "'";
but.onclick = null;
JS_API(
{ action: "query", list: "categorymembers", cmtitle: catname,
cmnamespace: "0|14", cmlimit: "500", format: "json" },
catname,
JS_CatWatch.CompleteFunction( node, but, catname )
);
}
JS_CatWatch.CompleteFunction = function( node, but, catname ) {
return function( id, txt ) { return JS_CatWatch.Complete( node, but, catname, id, txt ); }
}
JS_CatWatch.Complete = function( node, but, catname, id, txt ) {
var p = eval("(" + txt + ")");
but.pages = [];
for ( var member in p["query"]["categorymembers"] ) {
member = p["query"]["categorymembers"][member];
but.pages.push( member["title"] );
}
but.value = "Watch " + but.pages.length + " arts";
but.onclick = JS_CatWatch.WatchFunction( node, but, catname );
}
JS_CatWatch.WatchFunction = function( node, but, catname ) {
return function() { return JS_CatWatch.Watch( node, but, catname ); }
}
JS_CatWatch.Watch = function( node, but, catname ) {
for( var i = 0; i < but.pages.length ; i++ ) {
JS_Watchlist.Add( but.pages[i] );
}
but.onclick = null;
but.pages = null;
but.parentNode.removeChild( but );
}
// This is a little overaggressive, but not too bad.
JS_CatWatch.AddThemAll = function(){
var prefix = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace(/\$1/,"");
var elements = document.getElementsByTagName('a');
for (var j=1;j<elements.length;j++) {
if ( (elements[j].href.match(prefix + "Category:") )
&& !(elements[j].href.match("#")) ) {
JS_CatWatch.AddLookupButton(elements[j], elements[j].href.slice(prefix.length));
}
}
}