GitHub 切换项目与源码

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

目前為 2025-02-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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    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 }); // 监听整个文档的变化
})();