GitHub 切换项目与源码

在 GitHub 文件页面和 raw 页面之间添加按钮实现跳转。https://github.com/用户/项目 <==> https://raw.githubusercontent.com/用户/项目/ 。GitHub and Raw Page Toggle.

当前为 2025-02-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         GitHub 切换项目与源码
// @namespace    https://greasyfork.org/users/1171320
// @version      1.5
// @description  在 GitHub 文件页面和 raw 页面之间添加按钮实现跳转。https://github.com/用户/项目 <==> https://raw.githubusercontent.com/用户/项目/ 。GitHub and Raw Page Toggle.
// @author       yzcjd
// @author2     Lama AI 辅助
// @match        *://github.com/*/*/blob/*
// @match        *://raw.githubusercontent.com/*/*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加切换按钮的函数
    function addToggleButton() {
        // 删除已有按钮以防止重复
        const existingButton = document.getElementById('toggle-raw-button');
        if (existingButton) {
            existingButton.remove();
        }

        const currentUrl = window.location.href;

        // 在 GitHub 文件页面添加按钮以跳转到 raw 页面
        if (currentUrl.includes('github.com') && currentUrl.includes('/blob/')) {
            const toggleButton = document.createElement('button');
            toggleButton.id = 'toggle-raw-button';
            toggleButton.textContent = 'View Raw';
            toggleButton.style.position = 'fixed';
            toggleButton.style.top = '60px';
            toggleButton.style.right = '10px';
            toggleButton.style.zIndex = '1000';
            toggleButton.style.padding = '5px 10px';
            toggleButton.style.backgroundColor = '#2ea44f';
            toggleButton.style.color = '#fff';
            toggleButton.style.border = 'none';
            toggleButton.style.borderRadius = '5px';
            toggleButton.style.cursor = 'pointer';

            toggleButton.addEventListener('click', function() {
                const rawUrl = currentUrl.replace('github.com', 'raw.githubusercontent.com').replace('/blob/', '/');
                window.location.href = rawUrl; // 在当前页面打开 raw 页面
            });

            document.body.appendChild(toggleButton);
        }

        // 在 raw 页面添加按钮以跳转回 GitHub 文件页面
        if (currentUrl.includes('raw.githubusercontent.com')) {
            const githubUrl = currentUrl.replace('raw.githubusercontent.com', 'github.com').replace(/^(https:\/\/[^\/]+\/[^\/]+\/[^\/]+\/)/, '$1blob/');
            
            const toggleButton = document.createElement('button');
            toggleButton.id = 'toggle-raw-button';
            toggleButton.textContent = 'View on GitHub';
            toggleButton.style.position = 'fixed';
            toggleButton.style.top = '10px';
            toggleButton.style.right = '10px';
            toggleButton.style.zIndex = '1000';
            toggleButton.style.padding = '5px 10px';
            toggleButton.style.backgroundColor = '#0366d6';
            toggleButton.style.color = '#fff';
            toggleButton.style.border = 'none';
            toggleButton.style.borderRadius = '5px';
            toggleButton.style.cursor = 'pointer';

            toggleButton.addEventListener('click', function() {
                window.location.href = githubUrl; // 在当前页面打开 GitHub 页面
            });

            document.body.appendChild(toggleButton);
        }
    }

    // 初次加载时添加按钮
    addToggleButton();

    observer.observe(document.body, { childList: true, subtree: true }); // 监听整个文档的变化
})();