-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
69 lines (60 loc) · 2.15 KB
/
background.js
File metadata and controls
69 lines (60 loc) · 2.15 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
// background.js v2.0
const tabState = {};
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
const tabId = sender.tab?.id;
if (msg.type === 'GET_TAB_STATE') {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
sendResponse(tabState[tabs[0]?.id] || { status: 'unknown' });
});
return true;
}
if (!tabId) return;
if (msg.type === 'DOI_FOUND') {
tabState[tabId] = { status: 'checking', doi: msg.doi };
chrome.action.setBadgeText({ text: '…', tabId });
chrome.action.setBadgeBackgroundColor({ color: '#1e3a5f', tabId });
}
if (msg.type === 'CHECK_RESULT') {
const r = msg.result;
tabState[tabId] = {
status: r.retracted ? 'retracted' : (msg.citationTotal > 0 ? 'scanning' : 'ok'),
result: r,
citationTotal: msg.citationTotal || 0,
citationSummary: null,
};
if (r.retracted) {
chrome.action.setBadgeText({ text: '✕', tabId });
chrome.action.setBadgeBackgroundColor({ color: '#991b1b', tabId });
} else {
chrome.action.setBadgeText({ text: '…', tabId });
chrome.action.setBadgeBackgroundColor({ color: '#92400e', tabId });
}
}
if (msg.type === 'CITATION_PROGRESS') {
if (tabState[tabId]) {
tabState[tabId].citationSummary = msg.citationSummary;
tabState[tabId].status = 'scanning';
}
}
if (msg.type === 'CITATION_RESULT') {
const cs = msg.citationSummary;
const r = msg.result;
tabState[tabId] = {
status: r.retracted ? 'retracted' : 'ok',
result: r,
citationTotal: cs.total,
citationSummary: cs,
};
if (r.retracted) {
chrome.action.setBadgeText({ text: '✕', tabId });
chrome.action.setBadgeBackgroundColor({ color: '#991b1b', tabId });
} else if (cs.retractedCount > 0) {
chrome.action.setBadgeText({ text: `${cs.retractedCount}`, tabId });
chrome.action.setBadgeBackgroundColor({ color: '#b45309', tabId });
} else {
chrome.action.setBadgeText({ text: '✓', tabId });
chrome.action.setBadgeBackgroundColor({ color: '#166534', tabId });
}
}
});
chrome.tabs.onRemoved.addListener(tabId => { delete tabState[tabId]; });