Created
April 27, 2025 10:53
-
-
Save ZhangHanDong/b7c43ea3decd8a8e5f417c2d2faab8ab to your computer and use it in GitHub Desktop.
添加一个按钮,点击后将当前 GitHub 地址转换为 DeepWiki 地址并在新标签页打开
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GitHub 到 DeepWiki 按钮脚本 | |
// 添加一个按钮,点击后将当前 GitHub 地址转换为 DeepWiki 地址并在新标签页打开 | |
(() => { | |
// 创建并添加按钮到页面 | |
const createButton = () => { | |
// 检查是否已存在按钮(避免重复添加) | |
if (document.getElementById('deepwiki-button')) { | |
return; | |
} | |
// 创建按钮 | |
const button = document.createElement('button'); | |
button.id = 'deepwiki-button'; | |
button.innerText = '在 DeepWiki 中打开'; | |
button.style.cssText = ` | |
position: fixed; | |
top: 70px; | |
right: 20px; | |
z-index: 9999; | |
padding: 8px 12px; | |
background-color: #0366d6; | |
color: white; | |
border: none; | |
border-radius: 4px; | |
cursor: pointer; | |
font-weight: bold; | |
font-size: 14px; | |
box-shadow: 0 2px 5px rgba(0,0,0,0.2); | |
transition: all 0.2s ease; | |
`; | |
// 添加悬停效果 | |
button.addEventListener('mouseover', () => { | |
button.style.backgroundColor = '#0255b3'; | |
}); | |
button.addEventListener('mouseout', () => { | |
button.style.backgroundColor = '#0366d6'; | |
}); | |
// 添加点击事件 | |
button.addEventListener('click', () => { | |
// 获取当前 URL | |
const currentUrl = window.location.href; | |
// 将 github.com 替换为 deepwiki.com | |
const deepwikiUrl = currentUrl.replace('github.com', 'deepwiki.com'); | |
// 在新标签页打开 DeepWiki 链接 | |
window.open(deepwikiUrl, '_blank'); | |
}); | |
// 将按钮添加到页面 | |
document.body.appendChild(button); | |
}; | |
// 页面加载完成后创建按钮 | |
if (document.readyState === 'complete' || document.readyState === 'interactive') { | |
createButton(); | |
} else { | |
window.addEventListener('load', createButton); | |
} | |
// 针对 GitHub 的 SPA 特性,监听 URL 变化,确保按钮一直存在 | |
// 使用 MutationObserver 观察 DOM 变化 | |
const observer = new MutationObserver((mutations) => { | |
if (!document.getElementById('deepwiki-button')) { | |
createButton(); | |
} | |
}); | |
// 观察 document.body 的变化 | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment