-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
103 lines (91 loc) · 2.9 KB
/
background.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
let currentTab;
const matchUrlPrefix = 'https://bbs.pku.edu.cn/v2';
const prefixLength = matchUrlPrefix.length;
function getMap(postsOrThreads) {
return postsOrThreads.reduce((prev, { username, quoteUsername }) => {
prev[username] = BLOCK_NONE;
if (quoteUsername) {
prev[quoteUsername] = BLOCK_NONE;
}
return prev;
}, {});
}
function getOperationListFromPosts(posts, callback) {
const storage = chrome.storage.sync.get(getMap(posts), (answers) => {
const operations = posts.map(({ username, quoteUsername }) => {
let op = OP_NONE;
if (answers[username] >= BLOCK_POSTS) {
op = OP_HIDE;
}
if (quoteUsername && answers[quoteUsername] >= BLOCK_ALL) {
op = OP_HIDE;
}
return op;
});
callback(operations);
});
}
function getOperationListFromThreads(threads, callback) {
const storage = chrome.storage.sync.get(getMap(threads), (answers) => {
const operations = threads.map(({ username }) => {
let op = OP_NONE;
if (answers[username] >= BLOCK_THREAD) {
op = OP_HIDE;
}
return op;
});
callback(operations);
});
}
function filterPosts(tabId) {
chrome.tabs.sendMessage(tabId, { type: GET_POSTS }, (posts) => {
getOperationListFromPosts(posts, (operations) => {
chrome.tabs.sendMessage(tabId, { type: FILTER_ITEMS, operations });
});
});
}
function filterThreads(tabId) {
chrome.tabs.sendMessage(tabId, { type: GET_THREADS }, (threads) => {
getOperationListFromThreads(threads, (operations) => {
chrome.tabs.sendMessage(tabId, { type: FILTER_ITEMS, operations });
});
});
}
function renderUserPage(tabId) {
chrome.tabs.sendMessage(tabId, { type: GET_USER }, (username) => {
chrome.storage.sync.get({ [username]: 0 }, (answer) => {
chrome.tabs.sendMessage(tabId, { type: RENDER_USER, blockLevel: answer[username] });
});
});
}
function handleNavigation(e) {
chrome.pageAction.show(e.tabId); // TODO
const prefix = e.url.slice(prefixLength);
if (prefix.startsWith('/post-read.php')) {
filterPosts(e.tabId);
} else if (prefix.startsWith('/thread.php') || prefix.startsWith('/hot-topic.php')) {
filterThreads(e.tabId);
} else if (prefix.startsWith('/user.php')) {
renderUserPage(e.tabId);
}
}
chrome.webNavigation.onDOMContentLoaded.addListener(handleNavigation, {url: [{urlPrefix: matchUrlPrefix}]});
chrome.webNavigation.onHistoryStateUpdated.addListener(handleNavigation, {url: [{urlPrefix: matchUrlPrefix}]});
function changeBlockLevel({ username, blockLevel }) {
if (blockLevel === BLOCK_NONE) {
chrome.storage.sync.remove(username);
} else {
chrome.storage.sync.set({ [username]: blockLevel });
}
}
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
switch (request.type) {
case CHANGE_BLOCK_LEVEL: {
changeBlockLevel(request);
break;
}
default:
}
}
);