User:Sreejithk2000/OpenCatFilesInNewWindow.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.
// Adds an "Open in new window" link on Category pages. Helps to do bot check and deletions.

AddOpenFilesInNewWindowLink('#mw-category-media');
AddOpenFilesInNewWindowLink('#mw-pages');
AddOpenFilesInNewWindowLink('#mw-subcategories');
AddOpenFilesInNewWindoLinkForGallery();

function AddOpenFilesInNewWindoLinkForGallery() {
	var $galleryCaption = $('.gallerycaption');
	var catText = $galleryCaption.text();
	catText = catText + ' (<a class="javascript" href="javascript:OpenFilesInNewWindowForGallery()">Open in new window</a>)';
	$galleryCaption.html(catText);
}

function OpenFilesInNewWindowForGallery() {
	var maxCount = prompt('How many files to be opened in a new window?', '7');
	var currentCount = 1;
	$('.gallery').find('.image').each(function(link) {
		if (currentCount++ > maxCount) return false;
		window.open(this.getAttribute('href'), '_blank');
	});
}

function AddOpenFilesInNewWindowLink(mwElm) {
	var $mwElm = $(mwElm);
	var catText = $mwElm.find('p').html();
	catText = catText + '(<a class="javascript" href="javascript:OpenFilesInNewWindow(\'' + mwElm + '\')">Open in new window</a>)';
	$mwElm.find('p').html(catText);
}

function OpenFilesInNewWindow(mwElm) {
	var maxCount = prompt('How many files to be opened in a new window?', '7');
	var currentCount = 1;
	$(mwElm).find('a:not(.image):not(.galleryfilename):not(.javascript)').each(function(link) {
		var $this = $(this);
		if ($this.html() != 'previous 200' 
			&& $this.html() != 'previous page'
			&& $this.html() != 'next 200'
			&& $this.html() != 'next page'
			&& $this.attr('href')) {
			if (currentCount++ > maxCount) return false;
			window.open($this.attr('href'), '_blank');
		}
	});
}