-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
119 lines (100 loc) · 3.74 KB
/
content.js
File metadata and controls
119 lines (100 loc) · 3.74 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 创建链接显示元素
function createLinkDisplay(element) {
// 检查是否已经添加了链接显示
if (element.nextSibling?.className === 'github-folder-link') {
return element.nextSibling;
}
const linkDisplay = document.createElement('span');
linkDisplay.className = 'github-folder-link';
// 获取完整链接
const baseUrl = window.location.origin;
const path = element.getAttribute('href');
const fullUrl = baseUrl + path;
linkDisplay.textContent = fullUrl;
linkDisplay.style.display = 'none';
// 将链接显示元素插入到元素后面
element.parentNode.insertBefore(linkDisplay, element.nextSibling);
return linkDisplay;
}
// 初始化链接显示功能
function initLinks() {
// 查找所有文件和文件夹元素
const elements = document.querySelectorAll([
'div[role="row"] a[href*="/"]', // 文件和文件夹行中的链接
'.js-navigation-open', // 旧版GitHub的文件和文件夹链接
'a[href*="/blob/"]', // 文件链接
'a[href*="/tree/"]', // 文件夹链接
'a[role="rowheader"]' // 新版GitHub的文件和文件夹链接
].join(','));
elements.forEach(element => {
// 检查是否是文件或文件夹链接(排除其他类型的链接)
if (element.href) {
const linkDisplay = createLinkDisplay(element);
// 鼠标悬停时显示链接
element.addEventListener('mouseenter', () => {
linkDisplay.style.display = 'inline';
});
element.addEventListener('mouseleave', () => {
linkDisplay.style.display = 'none';
});
// 为父元素也添加悬停事件
const parentRow = element.closest('div[role="row"]');
if (parentRow) {
parentRow.addEventListener('mouseenter', () => {
linkDisplay.style.display = 'inline';
});
parentRow.addEventListener('mouseleave', () => {
linkDisplay.style.display = 'none';
});
}
}
});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "showLink") {
// 发送消息给后台脚本以存储 URL
chrome.runtime.sendMessage({
action: "saveUrl",
url: request.url
}, () => {
console.log("消息已发送到后台脚本");
});
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "parsePage") {
const { url, directoryPre, filePre } = request;
// 这里仅模拟解析页面的行为,实际应用中需要根据页面结构提取所需信息
const fileList = [];
const newUrls = [];
document.querySelectorAll('a').forEach(link => {
const href = link.getAttribute('href');
if (href.startsWith(directoryPre) || href.startsWith(filePre)) {
newUrls.push(new URL(href, url).href);
}
});
document.querySelectorAll('.js-navigation-open').forEach(item => {
const path = item.getAttribute('data-path');
if (path && !fileList.includes(path)) {
fileList.push(path);
}
});
sendResponse({
fileList,
newUrls
});
}
});
// 监听页面变化
const observer = new MutationObserver((mutations) => {
initLinks();
});
// 开始观察页面变化
observer.observe(document.body, {
childList: true,
subtree: true
});
// 初始加载时执行
initLinks();
// 添加定期检查,以确保动态加载的内容也能正确显示链接
setInterval(initLinks, 2000);