-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
39 lines (34 loc) · 1.07 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
try {
importScripts('kattis.js'); // gives updateHints method
} catch (e) {
console.error(e);
}
const HINT_SOURCE = "https://cpbook.net/methodstosolve?oj=kattis&topic=all&quality=all"
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// kattis.js requested the background thread to re-fetch hints
if (message == "updateHints") {
console.log("Background thread received request to update hints.")
updateHints()
.then(rawData => sendResponse(rawData));
return true
}
});
const save = (hints) => {
cache = {
"updated": Date.parse(new Date()),
"data": hints
}
return chrome.storage.local.set(cache).then(() => {
log.info(`Fetched and saved hints to Chrome cache.`)
}).catch(err => {
log.error(`Failed to save hints to Chrome cache. Error: ${err}`)
}).then(() => {
return cache
})
}
const updateHints = () => {
log.warn(`Re-initializing hint cache...`)
return fetch(HINT_SOURCE)
.then(rawRequest => rawRequest.text())
.then((rawHints) => save(rawHints))
}