An improved reimplementation of mtg.js for Board & Card Games SE
// ==UserScript== | |
// @name Improved MtG card links | |
// @version 2.0 | |
// @namespace https://github.com/vyznev/ | |
// @description An improved reimplementation of mtg.js for Board & Card Games SE | |
// @author Ilmari Karonen | |
// @license Public Domain (CC-Zero) | |
// @match *://boardgames.stackexchange.com/* | |
// @match *://meta.boardgames.stackexchange.com/* | |
// @grant none | |
// ==/UserScript== | |
StackExchange.ready(function () { | |
// config variables | |
var urlTemplate = 'http://gatherer.wizards.com/Pages/Card/Details.aspx?name=$1'; | |
var windowFeatures = 'scrollbars=1, resizable=1, width=770, height=890'; | |
// click event handler to open MtG card links in a new window | |
$(document).on('click', 'a.mtg-autocard', function (event) { | |
if (event.button !== 0) return ; | |
window.open(this.href, 'autocard' + (+new Date()), windowFeatures); | |
return false; | |
}); | |
// Markdown converter hook to parse card links are not parsed in edit preview | |
// code loosely based on makeTagLinks() in http://dev.stackoverflow.com/content/Js/wmd.en.js | |
function makeMtGLinks (text) { | |
var excludeRanges = null; | |
return text.replace(/\[mtg:([^\[\]]+)\]/g, function (fullMatch, cardName, offset) { | |
// don't replace [mtg:] links inside <a> or <code> tags; | |
// but don't bother looking for them unless we actually see such a link | |
if (excludeRanges === null) { | |
var re = /<(a|code)\b[^>]*>.*?<\/\1>/gi, match; | |
excludeRanges = []; | |
while ((match = re.exec(text)) !== null) { | |
excludeRanges.push(match.index, re.lastIndex); | |
} | |
} | |
var skip = false; | |
for (var i = 0; i < excludeRanges.length; i++) { | |
if (offset < excludeRanges[i]) break; | |
skip = !skip; | |
} | |
if (skip) return fullMatch; | |
var linkName = cardName.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&'); | |
var linkUrl = urlTemplate.replace(/\$1/g, encodeURIComponent(linkName)); | |
return '<a class="mtg-autocard" target="_blank" href="' + linkUrl + '">' + cardName + '</a>'; | |
}); | |
} | |
StackExchange.ifUsing('editor', function () { | |
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor) { | |
editor.getConverter().hooks.chain('postConversion', makeMtGLinks); | |
}); | |
}); | |
// HOTFIX: change the URLs in server-side generated card links, fix double-escaping | |
// this should really be fixed on the server side and this code removed | |
function fixCardLinks() { | |
var cardLinks = $('a.mtg-autocard[href*="autocard.asp"]:not(.hotfixed)'); | |
cardLinks.addClass('hotfixed'); | |
cardLinks.each(function () { | |
this.href = this.href.replace( | |
/^http:\/\/www\.wizards\.com\/magic\/autocard\.asp\?name=([^&#]+)$/, urlTemplate | |
).replace(/%26lt%3[Bb]/g, '%3C') .replace(/%26gt%3[Bb]/g, '%3E') .replace(/%26amp%3[Bb]/g, '%26'); | |
this.textContent = this.textContent.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&'); | |
}); | |
} | |
fixCardLinks(); | |
// re-run the hotfix whenever new posts are loaded via AJAX | |
var urlRegex = /^\/posts\/(ajax-load-realtime|\d+\/edit-submit)\/|^\/review\/(next-task|task-reviewed)\b/; | |
$(document).ajaxComplete(function (event, xhr, settings) { | |
if (urlRegex.test(settings.url)) setTimeout(fixCardLinks, 10); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment