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: don't alter the link

目前為 2016-09-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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: don't alter the link
// @namespace     http://target._blank.is.retarded
// @version       2.0.2
// @include       *
// @run-at        document-start
// @grant         GM_openInTab
// ==/UserScript==

window.addEventListener('mousedown', function(e) {
	if (e.button > 1)
		return;
	var link = e.target.closest('a');
	if (!link || !link.href || link.href.match(/^javascript|^#/))
		return;
	var b = e.button, c = e.ctrlKey, a = e.altKey, s = e.shiftKey, m = e.metaKey;
	if (!b && !c && !a && !s && !m && link.href.replace(/#.*/, '') != location.href.replace(/#.*/, ''))
		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;

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

	function prevent(e) {
		e.preventDefault();
		e.stopPropagation();
		e.stopImmediatePropagation();
		if (e.type != 'mousedown')
			window.removeEventListener(e.type, prevent);
	}
}, true);