Skip Navigation

open external links in a new tab

I wrote a quick script that opens links to external sites in a new tab. It works everywhere, not just on lemmy.world. You need a browser extension to embed custom javascript to use it.

document.addEventListener('click', evt => {
	const href = (evt.target?.tagName === 'A' ? evt.target : evt.target?.closest('a'))?.getAttribute('href');
	if (href && !href.startsWith(location.origin) && href.match(/^(?:https?:)?\/\//)) {
		window.open(href, '_blank');
		evt.preventDefault();
	}
});
2
2 comments