显示 Zread 按钮

在 GitHub 仓库页面顶部添加 Zread 按钮,快速跳转对应的 zread.ai 页面

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         显示 Zread 按钮
// @version      0.0.1
// @description  在 GitHub 仓库页面顶部添加 Zread 按钮,快速跳转对应的 zread.ai 页面
// @author       xiaoyu
// @match        https://github.com/*
// @grant        none
// @run-at       document-idle
// @namespace https://greasyfork.org/users/1528948
// ==/UserScript==

(() => {
    'use strict';

    const BUTTON_ID = 'zreadButton';
    const BUTTON_CONTAINER_SELECTOR = '.pagehead-actions.flex-shrink-0.d-none.d-md-inline';

    function init() {
        createOrUpdateButton();
        document.addEventListener('pjax:end', handlePjaxEnd);
    }

    function handlePjaxEnd() {
        // GitHub 使用 PJAX 导航时重新插入按钮
        createOrUpdateButton();
    }

    function createOrUpdateButton() {
        if (!isRepositoryPage()) {
            removeExistingButton();
            return;
        }

        if (document.getElementById(BUTTON_ID)) {
            return;
        }

        const container = document.querySelector(BUTTON_CONTAINER_SELECTOR);
        if (!container) {
            // 容器尚未渲染时稍后重试
            queueRetry();
            return;
        }

        const buttonNode = buildButtonNode();
        container.insertAdjacentElement('afterbegin', buttonNode);
    }

    function queueRetry() {
        window.setTimeout(createOrUpdateButton, 250);
    }

    function removeExistingButton() {
        const existing = document.getElementById(BUTTON_ID);
        if (existing && existing.parentElement) {
            existing.parentElement.removeChild(existing);
        }
    }

    function isRepositoryPage() {
        const segments = location.pathname.split('/').filter(Boolean);
        return segments.length >= 2;
    }

    function buildButtonNode() {
        const wrapper = document.createElement('li');
        wrapper.id = BUTTON_ID;

        const iconSvg = [
            '<svg class="octicon" width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">',
            '    <rect x="1" y="1" width="14" height="14" rx="3" fill="#f6f8fa" stroke="#d0d7de"/>',
            '    <path d="M4.75 3.5H11.25V5.25L7.06 10H11.25V12.5H4.75V10.75L8.94 6H4.75V3.5Z" fill="#0969da"/>',
            '</svg>'
        ].join('');

        const buttonHtml = [
            `<a target="_blank" rel="noopener noreferrer" class="btn btn-sm" href="${buildZreadUrl()}" aria-label="打开对应的 zread 页面">`,
            iconSvg,
            ' Zread',
            '</a>'
        ].join('');

        wrapper.innerHTML = buttonHtml;

        return wrapper;
    }

    function buildZreadUrl() {
        const current = new URL(location.href);
        current.hostname = 'zread.ai';
        current.protocol = 'https:';
        return current.toString();
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init, { once: true });
    } else {
        init();
    }
})();