Jump to content

User:V111P/js/whatLinksHereLinkFilter.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// 2014-11-30
if (mw.config.get('wgCanonicalSpecialPageName') == 'Whatlinkshere')
$(function () {
	var helpPage = '//en.wikipedia.org/wiki/User:V111P/js/What_Links_Here_link_filter';
	var spanId = 'excludePagesLinkingTo_span';
	var inputId = 'excludePagesLinkingTo_textInput';
	var formId = 'excludePagesLinkingTo_form';
	var pageNames = [];

	$('#' + spanId).remove();

	$('div#mw-content-text form + fieldset')
		.append($('<span id="' + spanId + '"/>')
				.append(' | Hide pages linking to: ')
				.append($('<form id="excludePagesLinkingTo_form" style="display:inline;">'
						  + '<input type="text" id="' + inputId + '" size="40"/> '
						  + '<input type="submit" value="Go"/> (<a href="'
						  + helpPage + '" target="_blank">?</a>)</form>')));

	$('#' + formId).submit(function (e) {
		e.preventDefault();
		pageNames = $('#' + inputId).val().split('|');
		request();
	});

	// add a 5000 link next to the 500 link
	var limit500Lnk = $('#mw-content-text > a')
		.filter(function() { return $.text([this]) == '500'; });
	if (limit500Lnk[0]) {
		limit500Lnk.after(' | <a title="' + limit500Lnk.attr('title') + '" href="'
				+ limit500Lnk.attr('href').replace(/limit=500/, 'limit=5000') + '">5000</a>');
	}

	function request(blcontinue) {
		$.ajax({
			url: '/w/api.php?action=query&list=backlinks&format=json&rawcontinue&bllimit=max&bltitle='
				+ encodeURIComponent(pageNames[0]) + (blcontinue ? '&blcontinue=' + blcontinue : ''),
			dataType: 'json',
			error: function () {
				$('div#mw-content-text form > fieldset').append(' Error. ');
			},
			success: function (result) {
				var console = window.console || {log: function () {}, error: function (s) {alert(s);}};

				if (!result || !result.query || !result.query.backlinks) {
					console.error('What Links Here Script error: No (expected) response received from server');
					return;
				}

				var p = result.query.backlinks;
				var titles = [];
				for (var i = 0; i < p.length; i++) {
					titles.push(p[i].title);
				}
				var elsToRemove = [];

				console.log('using page: %s', pageNames[0]);
				console.log('links before removal: %d', $('ul#mw-whatlinkshere-list li').length);
				$('ul#mw-whatlinkshere-list li').each(function (i, li) {
					var lnk = $(li).find('a').first();
					if ($.inArray($(lnk).text(), titles) > -1) {
						elsToRemove.push(li);
					}
				});
				console.log('removed: %d', elsToRemove.length);
				$(elsToRemove).remove();
				console.log('links after removal: %d', $('ul#mw-whatlinkshere-list li').length);

				if (result['query-continue']) {
					console.log('Continue from: %s', result['query-continue'].backlinks.blcontinue);
					request(result['query-continue'].backlinks.blcontinue);
				}
				else {
					pageNames.shift();
					if (pageNames.length > 0)
						request();
				}

			}
		});
	}
});