-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
179 lines (157 loc) · 4.68 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {clearCache, getTabById, getOptions, ignoreRejection} from './chrome.js';
import {processUrl, removeQueryString, isYoutubeUrl} from './url.js';
import {findOnReddit} from './reddit.js';
import {bgOptions} from './query.js';
const BADGE_COLORS = {
error: '#DD1616',
success: '#555555'
};
let bgOpts;
// The respective listeneres will noop if these are false. This hack is needed
// because we can no longer restart the background page in v3.
let tabUpdatedListenerActive = false;
let tabActivatedListenerActive = false;
// Throttling mechanism to handle multiple firings of the tabs.onUpdated event
// as the tab goes through different states while loading.
let recentlyQueried = new Set([]);
// this function seems to run only once in v3, which is fine
(function init() {
// Avoid storage bloat. Ideally, this should happen on browser exit,
// but the Chrome API doesn't provide an event for that.
clearCache();
chrome.tabs.onUpdated.addListener(tabUpdatedListener);
chrome.tabs.onActivated.addListener(tabActivatedListener);
updateListenerFlags();
})();
async function updateListenerFlags() {
const opts = await getOptions(bgOptions);
bgOpts = opts;
tabUpdatedListenerActive = bgOpts.autorun.updated;
tabActivatedListenerActive = bgOpts.autorun.activated;
}
async function tabUpdatedListener(tabId, info, tab) {
updateListenerFlags();
if (!tabUpdatedListenerActive) {
return;
}
if (recentlyQueried.has(tab.url)) {
return;
}
recentlyQueried.add(tab.url);
setTimeout(() => recentlyQueried.delete(tab.url), 1e3);
await removeBadge(tabId);
return autoSearch(tabId, tab.url);
}
async function tabActivatedListener(activeInfo) {
updateListenerFlags();
if (!tabActivatedListenerActive) {
return;
}
const tabId = activeInfo.tabId;
const tab = await getTabById(tabId);
return autoSearch(tabId, tab.url);
}
async function autoSearch(tabId, url) {
if (!isAllowed(removeQueryString(url))) {
return;
}
const isYt = isYoutubeUrl(url) && bgOpts.search.ytHandling;
const exactMatch = bgOpts.search.exactMatch && !isYt;
const urlToSearch = processUrl(url, bgOpts.search.ignoreQs, isYt);
let posts;
if (exactMatch) {
posts = await searchExact(tabId, urlToSearch);
} else {
posts = await searchNonExact(tabId, urlToSearch);
}
return setResultsBadge(tabId, posts);
}
const BG_RETRY_INTERVAL = 5e3;
const MAX_RETRIES = 5;
async function searchExact(tabId, url, retryCount = 0) {
if (retryCount >= MAX_RETRIES) {
return;
}
try {
const posts = await findOnReddit(url, true, true)
if (bgOpts.autorun.retryExact && posts.length === 0) {
return searchNonExact(tabId, url);
}
return posts;
} catch (e) {
if (bgOpts.autorun.retryError) {
setTimeout(() => searchExact(tabId, url, retryCount + 1), BG_RETRY_INTERVAL);
}
await handleError(e, tabId);
}
}
async function searchNonExact(tabId, url, retryCount = 0) {
if (retryCount >= MAX_RETRIES) {
return;
}
try {
return findOnReddit(url, true, false);
} catch (e) {
if (bgOpts.autorun.retryError) {
setTimeout(() => searchNonExact(tabId, url, retryCount + 1), BG_RETRY_INTERVAL);
}
await handleError(e, tabId);
}
}
function isAllowed(url) {
url = url.toLowerCase();
return (url.length > 0) && !(isChromeUrl(url) || isBlackListed(url));
}
function isBlackListed(url) {
return bgOpts.blacklist.some(s => url.search(s) > -1);
}
async function handleError(e, tabId) {
console.log(e);
return setErrorBadge(tabId);
}
function isChromeUrl(url) {
return /^chrome/.test(url);
}
async function setErrorBadge(tabId) {
return setBadge(tabId, 'X', BADGE_COLORS.error);
}
function numToBadgeText(n) {
if (n < 1_000) {
return `${n}`;
} else if (n < 1_000_000) {
return `${Math.trunc(n / 1_000)}K+`;
} else if (n < 1_000_000_000) {
return `${Math.trunc(n / 1_000_000)}M+`;
}
}
async function setResultsBadge(tabId, posts) {
const color = BADGE_COLORS.success;
if (!posts || posts.length === 0) {
return setBadge(tabId, '0', color);
}
if (bgOpts.autorun.badgeContent === 'num_comments') {
const numComments = posts.reduce((acc, p) => acc + p.data.num_comments, 0);
return setBadge(tabId, `${numToBadgeText(numComments)}`, color);
} else {
const numPosts = posts.length;
return setBadge(tabId, `${numToBadgeText(numPosts)}`, color);
}
}
async function removeBadge(tabId) {
return setBadge(tabId, '', BADGE_COLORS.success);
}
async function setBadge(tabId, text, color) {
const badge = { text: text, tabId: tabId };
const bgCol = { color: color, tabId: tabId };
try {
const tab = await getTabById(tabId);
if (!chrome.runtime.lastError) {
chrome.action.setBadgeText(badge);
}
if (!chrome.runtime.lastError) {
chrome.action.setBadgeBackgroundColor(bgCol);
}
} catch (args) {
return ignoreRejection(args);
}
}