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();
	}
});
9 points · 2 comments · view on lemmy.world

2 Comments

Kovu@lemmy.world · 5 pts · 3y (1 reply)

why not just middle mouse click?

countstex@feddit.dk · 2 pts · 3y

It can't be that eas.... Well damn!