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
当前为
// ==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.0.9
// @include *
// @run-at document-start
// @grant GM_openInTab
// ==/UserScript==
var suppressing;
window.addEventListener('mouseup', function(e) {
if (e.button > 1)
return;
var link = e.target.closest('a');
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') {
var _open = unsafeWindow.open;
var timeout = setTimeout(function() {
unsafeWindow.open = _open;
}, 50);
unsafeWindow.open = exportFunction(function(url) {
console.log('[Open links in current tab] prevented window.open for', url);
location.href = link.href;
unsafeWindow.open = _open;
clearTimeout(timeout);
}, unsafeWindow);
return;
}
if (!b && !c && !s && !m && (link.href.replace(/#.*/, '') != location.href.replace(/#.*/, '') || a))
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);
}