在 GitHub 文件页面和 raw 页面之间添加按钮实现跳转,不在新标签页中打开页面,并支持动态加载的内容
当前为
// ==UserScript==
// @name GitHub and Raw Page Toggle
// @namespace http://tampermonkey.net/
// @version 1.4
// @description 在 GitHub 文件页面和 raw 页面之间添加按钮实现跳转,不在新标签页中打开页面,并支持动态加载的内容
// @author Your Name
// @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 = '10px';
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();
// 使用 MutationObserver 监听 DOM 变化
const observer = new MutationObserver(() => {
addToggleButton(); // 检测到 DOM 变化时重新添加按钮
});
observer.observe(document.body, { childList: true, subtree: true }); // 监听整个文档的变化
})();