-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
37 lines (33 loc) · 1.09 KB
/
background.js
File metadata and controls
37 lines (33 loc) · 1.09 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
// 监听扩展安装事件
chrome.runtime.onInstalled.addListener((details) => {
console.log('Style Injector (v2.2) 已安装');
// 初始化存储结构(如果不存在)
chrome.storage.local.get(['styleRules'], (result) => {
if (!result.styleRules) {
chrome.storage.local.set({ styleRules: {} });
}
});
});
// 监听标签页更新,确保样式注入
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
try {
const url = new URL(tab.url);
const hostname = url.hostname;
chrome.storage.local.get(['styleRules'], (result) => {
const allRules = result.styleRules || {};
const rules = allRules[hostname];
if (rules && rules.length > 0) {
chrome.tabs.sendMessage(tabId, {
action: 'updateStyles',
rules: rules
}).catch(() => {
// 忽略错误:可能是 content script 还没加载好
});
}
});
} catch (e) {
console.error('Error checking tab url:', e);
}
}
});