GitHub 在新标签页中打开链接

默认在新标签页中打开所有 GitHub 链接

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GitHub 在新标签页中打开链接
// @namespace    http://example.com/your-namespace
// @version      1.0
// @description  默认在新标签页中打开所有 GitHub 链接
// @author       YuChujiu
// @match        https://github.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 定义一个函数,将所有链接设置为在新标签页中打开
    function openLinksInNewTab() {
        document.querySelectorAll('a').forEach(function(link) {
            // 如果链接没有设置 target 属性,则设置 target="_blank"
            if (!link.hasAttribute('target')) {
                link.setAttribute('target', '_blank');
            }
        });
    }

    // 页面加载时立即执行该函数
    openLinksInNewTab();

    // 使用 MutationObserver 监听页面的变化(处理 GitHub 动态加载的内容)
    const observer = new MutationObserver(openLinksInNewTab);

    // 监控整个页面的变化,子节点和嵌套子节点的变化都会触发
    observer.observe(document.body, { childList: true, subtree: true });

})();