User:V111P/js/whatLinksHereLinkFilter.js

// 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($('')

.append(' | Hide pages linking to: ')

.append($('

'

+ ' '

+ ' (?)

')));

$('#' + 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(' | 5000');

}

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();

}

}

});

}

});