MediaWiki:Common.js
From Game Logs
$(function() {
// Activate only on 5ed pages.
if ((wgCategories || []).indexOf("DnD 5") === -1)
return;
// Convert readable spell name into a Grimoire link.
var grimTextToURL = function(text) {
var spell = text.trim().toLowerCase()
.replace(/[^\w\d\ \-]/g, '')
.replace(/\ +/g, '-');
return spell ? ("//thebombzen.com/grimoire/spells/" + spell) : false
};
$(".dnd5-grimoire").off("mouseover").on("mouseover", function(e) {
// Only one popup allowed.
$(".dnd5-grimoire .dnd5-grimoire-wrapper").hide();
var link = $(e.target).closest(".dnd5-grimoire"),
embed = link.find(".dnd5-grimoire-embed"),
spellURL = grimTextToURL(link.text());
// Toggle visibility of the info div if it's already been populated.
if (embed.length) {
if (embed.not(":visible"))
embed.parent().show();
return;
}
if (!spellURL.length)
return;
var wrapper = $("<div></div>").addClass("dnd5-grimoire-wrapper").appendTo(link),
embed = $("<div></div>").addClass("dnd5-grimoire-embed dnd5-grimoire-loader").appendTo(wrapper);
// Show the block first, better with loader.
// no Promises, damn.
$.ajax({
"url": spellURL,
"type": "get",
"cache": true
})
.then(function(data) {
var doc = $(data);
var article = doc.find("article");
if (!article.length)
return console.log("Failed to find the DOM chunk with spell description");
embed.removeClass("dnd5-grimoire-loader").append(article);
})
.fail(function() {
embed.removeClass("loading").html("<p>Failed to look up this spell :(</p>");
});
});
$(".dnd5-grimoire").off("mouseout").on("mouseout", function(e) {
var target = $(e.target).closest(".dnd5-grimoire-wrapper");
if (!target.length)
return;
target.hide();
});
// Swap captions for ext-anchors to the Grim URL.
$(".dnd5-grimoire").each(function() {
var link = $(this);
// Insert an external link to the Grimoire page on first hover.
if (link.find("a").length)
return;
link.html(
$("<a></a>").text(link.text())
.prop("href", "https:" + grimTextToURL(link.text()))
.prop("target", "_blank")
);
});
});