-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
158 lines (138 loc) · 4.31 KB
/
content.js
File metadata and controls
158 lines (138 loc) · 4.31 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 验证并转义HTML特殊字符,防止XSS攻击
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// 验证URL格式是否安全
function isValidUrl(url) {
if (!url) return false;
try {
const parsed = new URL(url);
// 只允许 http 和 https 协议
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
} catch {
return false;
}
}
// 提取 arXiv ID 的函数
function extractArxivId(url) {
// 匹配 arXiv 链接格式: https://arxiv.org/abs/2106.12345 或 https://arxiv.org/abs/2106.12345v1
const arxivMatch = url.match(/\/abs\/([\d.]+)(v\d+)?$/);
if (arxivMatch) {
return arxivMatch[1];
}
// 匹配 hf链接格式: https://huggingface.co/papers/2510.08558
const hfMirrorMatch = url.match(/\/papers\/([\d.]+)$/);
if (hfMirrorMatch) {
return hfMirrorMatch[1];
}
return null;
}
// 默认按钮配置
const DEFAULT_BUTTONS = [
{
id: 'default-1',
name: "ModelScope",
template: "https://modelscope.cn/papers/{id}",
color: "#66ccff",
logo: "https://g.alicdn.com/sail-web/maas/2.9.88/favicon/128.ico"
},
{
id: 'default-2',
name: "PaperScope",
template: "https://paperscope.ai/hf/{id}",
color: "#008a10",
logo: "https://www.paperscope.ai/favicon.svg"
},
{
id: 'default-3',
name: "alphaXiv",
template: "https://www.alphaxiv.org/abs/{id}",
color: "#ff3838",
logo: "https://www.alphaxiv.org/assets/pwa/alphaxiv_app_1024.png"
}
];
// 获取用户设置的按钮配置
async function getButtonConfigs() {
try {
// 首先检查存储中是否有按钮配置
const checkResult = await chrome.storage.sync.get('redirectButtons');
let buttons = checkResult.redirectButtons;
// 如果存储中没有按钮配置,则初始化默认按钮
if (!buttons || buttons.length === 0) {
buttons = DEFAULT_BUTTONS;
await chrome.storage.sync.set({ redirectButtons: buttons });
}
return buttons;
} catch (error) {
console.error('Failed to load button configurations:', error);
return [];
}
}
// 创建并插入跳转按钮
async function addButtons() {
try {
const arxivId = extractArxivId(window.location.href);
if (!arxivId) return;
const buttonConfigs = await getButtonConfigs();
if (!buttonConfigs || buttonConfigs.length === 0) return;
// 创建按钮容器
const container = document.createElement('div');
container.style.cssText = `
margin: 20px 0;
display: flex;
flex-wrap: wrap;
gap: 10px;
`;
// 为每个配置创建按钮
buttonConfigs.forEach(config => {
try {
if (!config.template || !config.name) return;
const targetUrl = config.template.replace('{id}', arxivId);
const button = document.createElement('button');
button.style.cssText = `
padding: 10px 15px;
font-size: 14px;
background-color: ${config.color || '#f7931e'};
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
display: flex;
align-items: center;
`;
// 如果有Logo,则添加Logo(安全方式,防止XSS)
if (config.logo && isValidUrl(config.logo)) {
const img = document.createElement('img');
img.src = config.logo;
img.style.cssText = 'height: 16px; margin-right: 5px;';
img.alt = escapeHtml(config.name);
button.appendChild(img);
const span = document.createElement('span');
span.textContent = config.name;
button.appendChild(span);
} else {
button.textContent = config.name;
}
button.onclick = () => {
window.open(targetUrl, '_blank');
};
container.appendChild(button);
} catch (error) {
console.error('Failed to create button:', error);
}
});
// 找到标题元素并插入按钮容器
const titleElement = document.querySelector('h1');
if (titleElement && titleElement.parentNode) {
titleElement.parentNode.insertBefore(container, titleElement.nextSibling);
} else {
document.body.prepend(container);
}
} catch (error) {
console.error('Failed to add buttons:', error);
}
}
// 页面加载完成后添加按钮
addButtons();