-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
59 lines (48 loc) · 2.01 KB
/
Copy pathcontent.js
File metadata and controls
59 lines (48 loc) · 2.01 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
// 内容脚本 - 用于修改页面标题
console.log('📄 QuickTab内容脚本已加载');
// 监听来自popup的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('📨 收到消息:', request);
if (request.action === 'updatePageTitle') {
try {
const newTitle = request.title;
if (!newTitle || typeof newTitle !== 'string') {
throw new Error('无效的标题');
}
// 检查是否在特殊页面
if (window.location.protocol === 'chrome:' ||
window.location.protocol === 'chrome-extension:' ||
window.location.protocol === 'moz-extension:') {
throw new Error('无法修改Chrome内部页面标题');
}
// 修改页面标题
const oldTitle = document.title;
document.title = newTitle;
// 同时修改head中的title标签
let titleElement = document.querySelector('title');
if (titleElement) {
titleElement.textContent = newTitle;
} else {
// 如果没有title标签,创建一个
titleElement = document.createElement('title');
titleElement.textContent = newTitle;
document.head.appendChild(titleElement);
}
console.log(`✅ 页面标题已修改: "${oldTitle}" -> "${newTitle}"`);
sendResponse({ success: true, newTitle: newTitle, oldTitle: oldTitle });
} catch (error) {
console.error('❌ 修改页面标题失败:', error);
sendResponse({ success: false, error: error.message });
}
} else if (request.action === 'ping') {
// 用于检查内容脚本是否已加载
sendResponse({ success: true, message: 'content script ready' });
}
return true; // 保持消息通道开放
});
// 页面加载完成后,检查是否有待修改的标题
window.addEventListener('load', () => {
console.log('📄 页面加载完成,内容脚本就绪');
});
// 立即检查脚本是否已加载
console.log('📄 QuickTab内容脚本初始化完成');