Open links in current tab

Open links in current tab regardless of _target or site's preferences. Ctrl-click: background tab, Ctrl-Shift-click: foreground tab, Shift-click: new window, Alt-click: force open in current tab

As of 2016-10-22. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          Open links in current tab
// @author        wOxxOm
// @description   Open links in current tab regardless of _target or site's preferences. Ctrl-click: background tab, Ctrl-Shift-click: foreground tab, Shift-click: new window, Alt-click: force open in current tab
// @namespace     http://target._blank.is.retarded
// @version       2.2.0
// @include       *
// @run-at        document-start
// @grant         GM_openInTab
// ==/UserScript==

var suppressing, clickedElement;
window.addEventListener('mousedown', function(e) {
	clickedElement = e.target;
}, true);

window.addEventListener('mouseup', function(e) {
	if (e.button > 1 || e.target != clickedElement)
		return;
	var link = pierceShadow(e);
	if (!link || (link.getAttribute('href') || '').match(/^(javascript|#|$)/))
		return;
	var b = e.button, c = e.ctrlKey, a = e.altKey, s = e.shiftKey, m = e.metaKey;
	if (!a && link.target != '_blank')
		return blockWindowOpen({observer: freezeTarget(link)});
	if (!b && !c && !s && !m && (link.href.replace(/#.*/, '') != location.href.replace(/#.*/, '') || a || link.target == '_blank'))
		location.href = link.href;
	else if (b == 1 || c && !a && !m)
		GM_openInTab(link.href, !s);
	else if (window.chrome && !b && s && !c && !a && !m)
		link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
	else
		return;
	suppressing = true;
	prevent(e);
}, true);

window.addEventListener('click', prevent, true);
window.addEventListener('auxclick', prevent, true);

function prevent(e) {
	if (!suppressing)
		return;
	e.preventDefault();
	e.stopPropagation();
	e.stopImmediatePropagation();
	setTimeout(function() {
		suppressing = false;
	}, 50);
}

function freezeTarget(link) {
	var observer = new MutationObserver(function(mutations) {
		if (link.target == '_blank') {
			link.removeAttribute('target');
			console.log('[Open links in current tab] prevented dynamic target=_blank for', link.href);
		}
	});
	observer.observe(link, {attributes:true, attributeFilter:['target'], characterData:true});
	return observer;
}

function blockWindowOpen(params) {
	var _open = unsafeWindow.open;
	var timeout = setTimeout(function() {
		unsafeWindow.open = _open;
		if (params && params.observer)
			params.observer.disconnect();
	}, 50);
	unsafeWindow.open = exportFunction(function(url, name, features) {
		if (!features) {
			console.log('[Open links in current tab] prevented window.open for', url);
			location.href = link.href;
		} else
			_open(url, name, features);
		unsafeWindow.open = _open;
		clearTimeout(timeout);
	}, unsafeWindow);
}

function pierceShadow(e) {
	var el = e.target;
	while (el.shadowRoot)
		el = el.shadowRoot.elementFromPoint(e.clientX, e.clientY);
	return el.closest('a');
}