User:Perhelion/linkcount.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/** Shows the count of links to red links per mouseover and mass analyse per top action tab link 'New-Link-Count'.
* @author: Perhelion (idea/request: Freddy2001, Kenny McFly)
* @revision: 00:03, 23 January 2018 (UTC)
**/
/* global mediaWiki, jQuery */
(function (mw, $) {
"use strict";
if (!mw.config.get('wgNamespaceNumber') && mw.config.get('wgAction') === 'view')
$.when(
mw.loader.using('mediawiki.util'),
mw.loader.load('//commons.wikimedia.org/w/load.php?modules=ext.gadget.jquery.badge'),
$.ready).done(function () {
var r_links = {},
process = false,
a_title = $('#t-whatlinkshere a').text() + ": ",
limit = 99,
id = 0,
titles = [],
$al = $('#mw-content-text').find('.new');
function setLinkCount(title, e) {
if (e && r_links[title] > 1)
$(e).badge(r_links[title], 'inline', true).find('div').css({
position: "relative",
"margin": "0 -7px",
top: "-7px"
});
return (e.title = a_title + r_links[title]);
}
function getLinkCount(title, e) {
process = 1;
if ($.fn.badge)
$.getJSON(mw.util.wikiScript("api"), {
format: "json",
action: "query",
"prop": "linkshere",
"titles": title,
"lhprop": "",
"lhnamespace": "0",
"lhlimit": "max"
}, function (r) {
process = 0;
if (!r || !r.query)
return;
r = r.query.pages;
if (e) { // mouseover
r = r[Object.keys(r)[0]];
if (r) {
r_links[title] = r.linkshere.length;
return setLinkCount(title, e);
}
}
// var id = 0;
for (var i in r) {
i = r[i];
if (i) {
var ti = i.title.replace(/ /g, "_");
r_links[ti] = i.linkshere.length;
setLinkCount(ti, $al[id]);
}
id++;
}
// Not needed anymore
if ($al.length < limit)
$al.off('mouseover', doLinkCount);
if (titles.length < Math.max(limit, id))
$('#ca-lnkcnt').remove();
});
}
function splitLinks() {
var titleLimited;
if (titles.length > Math.max(limit, id)) { // Needs limit with loop!?
titleLimited = titles.slice(id, id + limit); // aware splice
mw.notify($("<span>Limit of " + (limit + 1) + " red links reached!<br> Click again for progressing the other " +(titles.length - limit - id)+ "</span>"), {
title: 'New-Link-Count',
type: 'warn'
});
getLinkCount(titleLimited.join('|'));
} else if (!id) {
getLinkCount(titles.join('|'));
}
}
function doAllLinkCount() {
if (!titles.length)
mw.loader.using([], function () {
$.each($al, function (i, a) {
titles.push(mw.util.getParamValue('title', a.getAttribute('href')));
});
return splitLinks();
});
else splitLinks();
}
function doLinkCount(e) {
if (process) // run api not in race condition
return;
e = e.target;
var title = mw.util.getParamValue('title', e.getAttribute('href'));
if (r_links.hasOwnProperty(title)) // run api only once
return setLinkCount(title, e);
getLinkCount(title, e);
}
if ($al.length) {
$al.one('mouseover', doLinkCount);
$(mw.util.addPortletLink('p-cactions', '#', 'New-Link-Count', 'ca-lnkcnt')).find('a').click(doAllLinkCount).addClass('new');
}
});
}(mediaWiki, jQuery));