MediaWiki:Gadget-tgcLinks.js: Difference between revisions
MediaWiki interface page
More actions
Created page with "// Change the 'href' attributes of links that are redirects to the Terraria Wiki // to make them go directly to their destination. Add a CSS class to those links // which applies a slightly different color to them. var cssClass = 'tgclink'; $(document).ready(function() { var allRedirectLinksOnPage = collectAllRedirectLinks(); // modify the title and href attributes of a link and add the class for coloring handleDirectInterwikiLinks(allRedirectLinksOnPage.di..." |
(No difference)
|
Latest revision as of 20:42, 23 November 2025
// Change the 'href' attributes of links that are redirects to the Terraria Wiki
// to make them go directly to their destination. Add a CSS class to those links
// which applies a slightly different color to them.
var cssClass = 'tgclink';
$(document).ready(function() {
var allRedirectLinksOnPage = collectAllRedirectLinks();
// modify the title and href attributes of a link and add the class for coloring
handleDirectInterwikiLinks(allRedirectLinksOnPage.direct);
handleIndirectInterwikiLinks(allRedirectLinksOnPage.indirect);
});
function makeNewHovertext(title) {
return 'Terraria Wiki: ' + title + ' (this link is a redirect to the main Terraria Wiki)';
}
function collectAllRedirectLinks() {
var directInterwikiLinks = [];
var indirectInterwikiLinks = [];
$('#mw-content-text a.mw-redirect, #mw-content-text a.extiw').each(function() {
var thislink = $(this);
if (thislink.attr('title').slice(0, 4) === 'tgc:') {
directInterwikiLinks.push(thislink);
} else {
indirectInterwikiLinks.push(thislink);
}
});
return { direct: directInterwikiLinks, indirect: indirectInterwikiLinks };
}
function handleDirectInterwikiLinks(links) {
$.each(links, function() {
var thislink = $(this);
var newHovertext = makeNewHovertext(thislink.attr('title').slice(4)); // strip the 'tgc:' part
thislink
.attr('title', newHovertext)
.attr('target', '_blank') // open in new tab
.addClass(cssClass);
});
}
function handleIndirectInterwikiLinks(links) {
var linkTargets = [];
$.each(links, function() {
var linkTarget = getTargetOfLink($(this));
if ($.inArray(linkTarget, linkTargets) === -1) {
linkTargets.push(linkTarget);
}
});
if (linkTargets.length === 0) {
return;
}
getRedirectUrls(linkTargets).done(function(redirectUrls) {
if (redirectUrls === undefined) {
return;
}
$.each(links, function() {
var thislink = $(this);
var linkTarget = getTargetOfLink(thislink);
var tgcUrl = redirectUrls[linkTarget];
if (tgcUrl === undefined) {
return;
}
var hashIndex = thislink.attr('href').indexOf('#');
if (hashIndex > -1) {
tgcUrl += thislink.attr('href').slice(hashIndex);
}
thislink
.attr('href', tgcUrl)
.attr('title', makeNewHovertext(linkTarget))
.attr('target', '_blank') // open in new tab
.addClass(['extiw', cssClass]);
});
});
}
function getTargetOfLink(linkElement) {
return decodeURIComponent(linkElement.attr('href')
.replace(/_/g, ' ')
.replace(/^\/(wiki\/|mediawiki\/index\.php\/)?/, '') // added index.php case
.replace(/#.+$/, '')
);
}
function getRedirectUrls(linkTargets) {
var deferred = new $.Deferred();
new mw.Api().post({
action: 'query',
format: 'json',
titles: linkTargets.join('|'),
redirects: true,
iwurl: true
}).done(function(data) {
if (data.query.redirects === undefined || data.query.interwiki === undefined) {
deferred.resolve();
return;
}
var redirectUrls = {};
var redirectObjects = data.query.redirects.filter(function(r) { return r.tointerwiki === 'tgc'; });
redirectObjects.forEach(function(redirect) {
var iwObject = data.query.interwiki.find(function (i) {
return i.iw === 'tgc' && i.title === redirect.to;
});
if (iwObject !== undefined) {
redirectUrls[redirect.from] = iwObject.url;
}
});
deferred.resolve(redirectUrls);
}).fail(function() {
var error = arguments;
if (error[0] === 'toomanyvalues') {
var limit = error[1].error.limit;
var deferreds = [];
for (var index = 0; index < Math.ceil(linkTargets.length / limit); index++) {
var linkTargetsSlice = linkTargets.slice(index * limit, (index + 1) * limit);
deferreds.push(getRedirectUrls(linkTargetsSlice));
}
$.when.apply(null, deferreds).done(function() {
var redirectUrls = Array.from(arguments).reduce(function(redirectUrlsCombined, redirectUrlsSlice) {
if (redirectUrlsSlice !== undefined) {
Object.keys(redirectUrlsSlice).forEach(function(key) {
redirectUrlsCombined[key] = redirectUrlsSlice[key];
});
}
return redirectUrlsCombined;
}, {});
deferred.resolve(redirectUrls);
});
} else {
console.error(error);
deferred.resolve();
}
});
return deferred;
}
