-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice-worker.js
80 lines (72 loc) · 2.68 KB
/
service-worker.js
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
chrome.storage.onChanged.addListener((changes, areaName) => {
if (changes.chooseTheme.oldValue) {
chrome.scripting.unregisterContentScripts({
ids: [changes.chooseTheme.oldValue],
});
}
if (changes.chooseTheme.newValue) {
const new_flomo_theme = {
id: changes.chooseTheme.newValue,
matches: ['https://v.flomoapp.com/*', 'https://flomoapp.com/*'],
css: ['include/theme/' + changes.chooseTheme.newValue],
runAt: 'document_end',
};
chrome.scripting.registerContentScripts([new_flomo_theme]);
// 使新的css生效
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
// 注入新的内容脚本,如果当前页面是chrome:// 则不注入
if (tabs[0].url.indexOf('chrome://') > -1) {
return;
}
console.log('注入新的css' + tabs[0].id);
chrome.scripting.insertCSS({
target: {
tabId: tabs[0].id,
},
files: ['include/theme/' + changes.chooseTheme.newValue],
});
});
}
});
const DEFAULT_THEME = 'stackoverflow-dark.css';
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ chooseTheme: DEFAULT_THEME }, () => {
console.log('保存成功');
});
});
function getTheme() {
// 获取当前主题
chrome.storage.sync.get('chooseTheme', (result) => {
if (result.chooseTheme) {
console.log('当前主题为', result.chooseTheme);
DEFAULT_THEME = result.chooseTheme;
}
});
return DEFAULT_THEME;
}
const flomo_content_script = {
id: 'flomo_content_script',
matches: ['https://v.flomoapp.com/*', 'https://flomoapp.com/*'],
js: ['scripts/content.js', 'include/highlight.min.js'],
runAt: 'document_end',
};
chrome.scripting.registerContentScripts([flomo_content_script]);
// 监听网络请求
chrome.webRequest.onCompleted.addListener(
function (details) {
// 检查请求URL是否匹配目标
if (details.url.includes('https://h5.udrig.com/app/v1')) {
console.log('捕获到目标请求:', details.url);
// 获取请求所在的tab
chrome.tabs.get(details.tabId, function (tab) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
return;
}
// 在请求所在的tab中执行代码高亮功能
chrome.tabs.sendMessage(details.tabId, { action: 'highlightCode' });
});
}
},
{ urls: ['https://h5.udrig.com/app/v1*'] }
);