-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
38 lines (37 loc) · 1.22 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
chrome.runtime.onMessage.addListener((message, sender, reply) => {
if (!message || !message.action || !message.url) {
return false;
}
console.log("fetch " + message.url);
if (message.action === "fetchBlob") {
fetch(message.url)
.then(response => response.blob())
.then(convertBlobToBase64)
.then(base64 => {
reply(base64.split(',')[1]);
})
.catch(error => {
console.error("There has been a problem with your fetch operation:", error);
reply("");
});
} else if (message.action === "fetchCSS") {
fetch(message.url)
.then(response => response.text())
.then(text => reply(text))
.catch(error => {
console.error("Unable to fetch css", error);
reply({error: "Unable to load css " + message.url});
})
}
return true;
});
function convertBlobToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
}