User:V111P/js/wlhFilters.js
Appearance
< User:V111P | js
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:V111P/js/wlhFilters. |
// 2015-03-26
// Note: You must include Template: or Category: before template and category names
// Note: It's not recommended to be used for templates used on large number of pages, or categories with large number of pages
if (mw.config.get('wgCanonicalSpecialPageName') == 'Whatlinkshere')
$(function () {
var helpPage = '//en.wikipedia.org/wiki/User:V111P/js/What Links Here link filter';
var spanId = 'vWLHFilter_span';
var apiOptionNames = [['embeddedin', 'ei'], ['backlinks', 'bl'], ['categorymembers', 'cm']];
var optionLabels = ['transcluding', 'linking to', 'from category'];
var $span, $input, $select;
$('#' + spanId).remove();
$span = $('<span/>').attr('id', spanId)
.append(' | Hide pages ')
.append($('<form id="sart" style="display:inline;">'
+ '<select></select>: '
+ '<input type="text" size="40"/> '
+ '<input type="submit" value="Go"/> (<a href="'
+ helpPage + '" target="_blank">?</a>)</form>'))
.appendTo($('div#mw-content-text form + fieldset'));
$input = $span.find('input');
$select = $span.find('select');
$.each(optionLabels, function (i, val) {
$select.append($('<option>' + val + '</option>'));
});
$span.find('form').submit(function (e) {
e.preventDefault();
pageNames = $input.val().split('|');
apiOptionNum = $select[0].selectedIndex;
request(pageNames, apiOptionNames[apiOptionNum]);
});
// 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(pageNames, apiParams, continueFrom) {
var longName = apiParams[0];
var shortName = apiParams[1];
$.ajax({
url: '/w/api.php?action=query&list=' + longName + '&format=json&rawcontinue=&'
+ shortName + 'limit=max&' + shortName + 'title='
+ encodeURIComponent(pageNames[0]) + (continueFrom ? '&' + shortName + 'continue=' + continueFrom : ''),
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[longName]) {
console.error('What Links Here Script error: No (expected) results received from server.');
return;
}
var p = result.query[longName];
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']) {
var nextContinueFrom = result['query-continue'][longName][shortName + 'continue'];
console.log('Continue from: %s', nextContinueFrom);
request(pageNames, apiParams, nextContinueFrom);
}
else {
pageNames.shift();
if (pageNames.length > 0)
request(pageNames, apiParams);
}
} // success()
}); // $.ajax()
} // request()
});