User:Rillke/genCatList.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// In categories, creates a toolbox link "Create plain list" which, when clicked creates a list at the bottom of the page

(function($, mw) {

	if (14 !== mw.config.get('wgNamespaceNumber')) return;
	var $l = $(mw.util.addPortletLink('p-tb', '#', 'Create plain list'));

	$l.click(function(e) {
		e.preventDefault();
		mw.loader.using('ext.gadget.libAPI', function() {
			var $ta = $('<textarea>').attr('id', 'rk-cpl').height(500).css('width', '100%').appendTo('body').click(function() {
				$(this).select();
			});
			$d = mw.libs.commons.api.$autoQuery({
				action: 'query',
				list: 'categorymembers',
				cmtitle: mw.config.get('wgPageName').replace(/_/g, ' '),
				cmtype: 'file',
				cmlimit: 500
			}, {}, 100);
			$d.progress(function(r) {
				var cms = [];
				$.each(r.query.categorymembers, function(i, cm) {
					cms.push(cm.title);
				});
				$ta.val($ta.val() + '\n' + cms.join('\n'));
			}).done(function() {
				alert("Done.");
				document.location.hash = "rk-cpl";
			})
		});

	});

}(jQuery, mediaWiki));