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

目前為 2016-11-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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.6
// @include       *
// @run-at        document-start
// @grant         GM_openInTab
// ==/UserScript==

if (window == top) {
	window.addEventListener('message', function(e) {
		// some stupid sites choke on object data
		if (!/^\{/.test(e.data))
			return;
		var data = tryParse(e.data);
		if (data.name == GM_info.script.name)
			navigate(data.url);
	});
}

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 (b == 1 || c && !a && !m)
		GM_openInTab(link.href, !s || b == 1);
	else if (window.chrome && b === 0 && s && !c && !a && !m)
		link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
	else if (!c && !s && !m && !a) {
		if (link.target == '_blank')
			link.target = '';
		blockWindowOpenAndMutations(link);
		return;
	}
	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 blockWindowOpenAndMutations(link) {
	var observer = new MutationObserver(function() {
		if (link.target == '_blank') {
			link.removeAttribute('target');
			console.log('[Open links in current tab] prevented dynamic target=_blank for', link.href);
			navigate(link.href);
		}
	});
	observer.observe(link, {attributes:true, attributeFilter:['target'], characterData:true});

	var _open = unsafeWindow.open;
	var timeout = setTimeout(function() {
		unsafeWindow.open = _open;
		observer.disconnect();
	}, 50);
	unsafeWindow.open = exportFunction(function(url, name, features) {
		if (!features) {
			console.log('[Open links in current tab] prevented window.open for', url);
			navigate(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');
}

function navigate(url) {
	if (window == top) {
		var link = document.createElement('a');
		link.href = url;
		link.dispatchEvent(new MouseEvent('click'));
	} else
		top.postMessage(JSON.stringify({name: GM_info.script.name, url: url}), '*');
}

function tryParse(str) {
	try { return JSON.parse(str); }
	catch(e) {}
}