Force Link Open in New

Opens all web links in new tabs, enhancing browsing efficiency and preventing loss of work progress.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Force Link Open in New
// @name:zh-CN   强制新标签页打开链接
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Opens all web links in new tabs, enhancing browsing efficiency and preventing loss of work progress.
// @description:zh-CN  让所有网页链接都在新标签页中打开,提升浏览效率。
// @author       Yearly
// @license      AGPL-v3.0
// @match        *://*/*
// @exclude      *://*.greasyfork.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Capture all click events and open links in a new tab
    document.addEventListener('click', function(event) {
        let anchor = event.target.closest('a');
        if (anchor && anchor.href) {
            event.preventDefault();
            window.open(anchor.href, '_blank');
        }
    }, true);

    // Override window.open method to open URLs in a new tab
    const originalOpen = window.open;
    window.open = function(url, target = '_blank', features) {
        if (['_self', '_parent', '_top'].includes(target)) {
            target = '_blank';
        }
        return originalOpen.call(window, url, target, features);
    };

})();