-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
48 lines (43 loc) · 1.37 KB
/
content.js
File metadata and controls
48 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 检测当前页面是否是GitHub仓库页
function isRepoPage() {
return window.location.href.match(/github\.com\/[^/]+\/[^/]+/);
}
// 生成DeepWiki链接
function createDeepWikiLink() {
const pathParts = window.location.pathname.split('/');
const owner = pathParts[1];
const repo = pathParts[2];
return `https://deepwiki.com/${owner}/${repo}`;
}
// 添加按钮到页面
// 修改后的按钮注入逻辑
function injectButton() {
if (document.getElementById('deepwiki-btn')) return; // 防止重复注入
const button = document.createElement('button');
button.id = 'deepwiki-btn';
button.textContent = '查看 DeepWiki';
button.style.marginLeft = '10px';
button.style.padding = '6px 12px';
button.addEventListener('click', () => {
try {
const newUrl = createDeepWikiLink();
if (newUrl && isRepoPage()) {
window.location.href = newUrl;
}
} catch (error) {
console.error('跳转失败:', error);
}
});
// 调整选择器定位到正确的导航位置
const nav = document.querySelector('.js-repo-nav ul');
if (nav) {
const li = document.createElement('li');
li.appendChild(button);
nav.appendChild(li);
}
}
// 添加DOM加载监听
if (isRepoPage()) {
document.addEventListener('DOMContentLoaded', injectButton);
window.addEventListener('load', injectButton); // 双保险确保加载完成
}