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

目前為 2023-09-07 提交的版本,檢視 最新版本

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

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

var pageBase = location.href.split('#', 1)[0];
var suppressing, clickedElement;

window.addEventListener('click', prevent, true);
window.addEventListener('auxclick', prevent, true);
window.addEventListener('mousedown', remember, true);
window.addEventListener('mouseup', check, true);

function remember(e) {
  clickedElement = e.composedPath ? e.composedPath()[0] : e.target;
}

function check(e) {
  var el = e.composedPath ? e.composedPath()[0] : e.target;
  if (e.button > 1 || el !== clickedElement)
    return;
  var a = el.closest('a');
  if (!a)
    return;
  blockWindowOpenAndMutations(a);
  var url = a.href;
  if (url.lastIndexOf('javascript:', 0) === 0)
    return;
  if (!url || url.split('#', 1)[0] === pageBase) {
    clearLinkTarget(a);
    return;
  }
  var b = e.button, c = e.ctrlKey, alt = e.altKey, s = e.shiftKey, m = e.metaKey;
  if (b === 1 || c && !alt && !m)
    GM_openInTab(url, !s || b === 1);
  else if ((window.chrome || !CSS.supports('-moz-appearance', 'none')) && b === 0 && s && !c && !alt && !m)
    a.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
  else if (!c && !s && !m && !alt) {
    clearLinkTarget(a);
    return;
  } else
    return;
  suppressing = true;
  prevent(e);
}

function clearLinkTarget(a) {
  if (a.target === '_blank')
    a.target = '';
}

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

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

  var _open = unsafeWindow.open;
  var timeout = setTimeout(function () {
    unsafeWindow.open = _open;
    observer.disconnect();
  }, 50);

  unsafeWindow.open = function (url, name, opts) {
    let o0 = `${opts || ''}`, o1, o2, rel = [
      (o1 = o0.replace(/(^|,)\s*noopener\s*($|,)/gi, '')) !== o0 && 'noopener',
      (o2 = o1.replace(/(^|,)\s*noreferrer\s*($|,)/gi, '')) !== o1 && 'noreferrer',
    ];
    if (!o2.trim()) {
      console.log('[Open links in current tab] prevented window.open for', url);
      if (rel.some(Boolean)) a.relList.add.apply(a.relList, rel.filter(Boolean));
      navigate(a.href);
    } else
      _open(url, name, opts);
    unsafeWindow.open = _open;
    clearTimeout(timeout);
  };
}

function navigate(url) {
  if (window === top) {
    var a = document.createElement('a');
    a.href = url;
    a.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) {}
}