Mastodon: open external links in new tab
Makes external links inside posts open in a new tab so your timeline stays on screen.
Code to copy
// Force external links in posts to open in a new browser tab
(function () {
function patch() {
document.querySelectorAll('.status__content a[href^="http"], .detailed-status__content a[href^="http"]').forEach(function (a) {
if (a.dataset.jzNewtab) { return; }
var url;
try { url = new URL(a.href); } catch (e) { return; }
if (url.host === location.host) { return; }
a.dataset.jzNewtab = '1';
a.target = '_blank';
a.rel = 'noopener noreferrer';
});
}
patch();
var obs = new MutationObserver(patch);
obs.observe(document.body, { childList: true, subtree: true });
})();
How to use this example
- Copy the code with the button above.
- Install JustZix (2 minutes) and open the extension on the target page.
- Add a new rule matching that page.
- Paste the code into the rule's JavaScript panel and save — it runs on every page visit.
Rate this example
No ratings yet — be the first.