Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
98 lines (86 loc) · 2.99 KB
/
Copy pathbackground.js
File metadata and controls
98 lines (86 loc) · 2.99 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
let noHistoryDomains = [];
chrome.storage.onChanged.addListener((changes, namespace) => {
if (changes.noHistoryDomains) {
noHistoryDomains = changes.noHistoryDomains.newValue || [];
console.log('Updated domain list:', noHistoryDomains);
}
});
chrome.storage.sync.get(['noHistoryDomains'], (result) => {
noHistoryDomains = result.noHistoryDomains || [];
console.log('Initial domain list:', noHistoryDomains);
});
// 防抖:避免短时间内重复删除同一 URL
let processedUrls = new Set();
// 删除历史记录的通用逻辑
const deleteHistory = (urlString) => {
const url = new URL(urlString);
const domain = url.hostname;
if (!noHistoryDomains.some(d => domain === d || domain.endsWith('.' + d))) {
return;
}
// 防抖:短时间内跳过重复 URL
if (processedUrls.has(urlString)) {
console.log(`Already processed URL, skipping: ${urlString}`);
return;
}
processedUrls.add(urlString);
setTimeout(() => processedUrls.delete(urlString), 2000); // 缩短防抖时间到 2 秒
console.log(`Matched domain: ${domain}, deleting history for: ${urlString}`);
// 第一次尝试:直接删除
setTimeout(() => {
chrome.history.deleteUrl({ url: urlString }, () => {
if (chrome.runtime.lastError) {
console.error(`Failed to delete history for ${urlString}:`, chrome.runtime.lastError);
} else {
console.log(`Successfully deleted history: ${urlString}`);
}
// 后备:使用 chrome.history.search 查找并删除所有相关记录
chrome.history.search(
{ text: url.origin, startTime: 0, maxResults: 100 },
(historyItems) => {
historyItems.forEach((item) => {
if (item.url.startsWith(url.origin)) {
chrome.history.deleteUrl({ url: item.url }, () => {
console.log(`Fallback: deleted history: ${item.url}`);
});
}
});
}
);
});
}, 1000); // 增加延迟到 1 秒
};
// 监听网页请求事件
chrome.webRequest.onBeforeRequest.addListener(
(details) => deleteHistory(details.url),
{ urls: ["<all_urls>"] }
);
chrome.webRequest.onCompleted.addListener(
(details) => deleteHistory(details.url),
{ urls: ["<all_urls>"] }
);
chrome.webRequest.onHeadersReceived.addListener(
(details) => deleteHistory(details.url),
{ urls: ["<all_urls>"] }
);
// 监听页内跳转事件(SPA 跳转)
chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
if (details.url) {
console.log(`History state updated: ${details.url}`);
deleteHistory(details.url);
}
});
// 监听页面提交事件(包括刷新)
chrome.webNavigation.onCommitted.addListener((details) => {
if (details.url) {
console.log(`Page committed: ${details.url}`);
deleteHistory(details.url);
}
});
// 监听页面加载完成事件
chrome.webNavigation.onDOMContentLoaded.addListener((details) => {
if (details.url) {
console.log(`DOM content loaded: ${details.url}`);
deleteHistory(details.url);
}
});