(function () {
var DESKTOP = '(min-width: 1025px) and (pointer: fine)';
var mq = window.matchMedia(DESKTOP);
function isDesktop() { return mq.matches; }
function tidy(a) {
if (!a || !a.href || !/^https?:/i.test(a.href)) return;
if (a.dataset.keepTarget === 'true') return;
if (a.closest('.no-tab-rewrite')) return;
if (isDesktop()) {
a.target = '_blank';
if (!/noopener/.test(a.rel)) {
a.rel = (a.rel ? a.rel + ' ' : '') + 'noopener noreferrer';
}
} else if (a.target && a.target !== '_self') {
a.removeAttribute('target');
}
}
function sweep(root) {
(root || document).querySelectorAll('a[href]').forEach(tidy);
}
// Catch the click itself, so late-added links and plugin re-adds are covered.
document.addEventListener('click', function (e) {
var a = e.target.closest ? e.target.closest('a[href]') : null;
if (!a) return;
tidy(a);
}, true);
// Re-sweep anything added to the page after load.
if (window.MutationObserver) {
new MutationObserver(function (muts) {
muts.forEach(function (m) {
m.addedNodes.forEach(function (n) {
if (n.nodeType !== 1) return;
if (n.tagName === 'A') tidy(n);
else if (n.querySelectorAll) sweep(n);
});
});
}).observe(document.documentElement, { childList: true, subtree: true });
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () { sweep(); });
} else {
sweep();
}
mq.addEventListener('change', function () { sweep(); });
})();