Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Gadget-tgcLinks.js

MediaWiki interface page
Revision as of 20:42, 23 November 2025 by JamzOJamz (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
// 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;
}