GitHub DeepWiki Link

在GitHub项目主页添加DeepWiki链接

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GitHub DeepWiki Link
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  在GitHub项目主页添加DeepWiki链接
// @author       zerah
// @match        https://github.com/*/*
// @grant        none
// @run-at       document-idle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 初始化变异观察器
    function initMutationObserver() {
        const targetNode = document.body;

        // 观察器配置
        const config = {
            childList: true,
            subtree: true
        };

        // 创建观察器实例
        const observer = new MutationObserver((mutationsList) => {
            for(const mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    const publicLabel = document.querySelector('.Label--secondary.v-align-middle.mr-1');
                    if (publicLabel && !document.querySelector('.deepwiki-link')) {
                        addDeepWikiLink(publicLabel);
                        // 一旦添加了链接,就不再需要继续监视
                        // observer.disconnect();
                        // 但我们保持观察,以防导航到其他页面
                    }
                }
            }
        });

        // 开始观察
        observer.observe(targetNode, config);
    }

    // 立即运行一次,以防元素已经存在
    setTimeout(() => {
        const publicLabel = document.querySelector('.Label--secondary.v-align-middle.mr-1');
        if (publicLabel) {
            addDeepWikiLink(publicLabel);
        }

        // 无论是否找到元素,都启动观察器
        initMutationObserver();
    }, 500);

    function addDeepWikiLink(publicLabel) {
        // 获取项目路径,例如 qzz0518/coss
        const pathParts = window.location.pathname.split('/');
        if (pathParts.length < 3) return; // 不是项目页面

        const owner = pathParts[1];
        const repo = pathParts[2];

        // 确保我们在项目的主页上
        if (pathParts.length > 3 && pathParts[3] !== '') return;

        // 检查是否已经添加了DeepWiki链接
        if (publicLabel.parentNode.querySelector('.deepwiki-link')) return;

        // 创建DeepWiki链接
        const deepWikiLink = document.createElement('a');
        deepWikiLink.href = `https://deepwiki.com/${owner}/${repo}`;
        deepWikiLink.target = '_blank'; // 在新窗口打开
        deepWikiLink.className = 'Label Label--secondary v-align-middle mr-1 deepwiki-link';
        deepWikiLink.textContent = 'DeepWiki';
        deepWikiLink.style.textDecoration = 'none';
        deepWikiLink.style.cursor = 'pointer';

        // 在Public标签后面插入DeepWiki链接
        publicLabel.parentNode.insertBefore(deepWikiLink, publicLabel.nextSibling);
    }
})();