-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
39 lines (33 loc) · 1.13 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
// run sender: extension executes its toolbar icon is clicked — sends a message to content.js
chrome.action.onClicked.addListener(tab => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
chrome.tabs.sendMessage(tabs[0].id, { run: 'true' }, {});
});
});
async function addB64(array) {
// https://gist.github.com/HaNdTriX/bdffd11761701fbeba27f23e9a69515f
const toDataURL = url => fetch(url)
.then(response => response.blob())
.then(blob => new Promise(resolve => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
}))
.catch(error => { console.log(error); });
// populate base64 fields in the JSON
let i = 1;
for (let obj of array) {
console.log(`(${i++}/${array.length}) downloading ${obj.url} as ${obj.name}`);
await toDataURL(obj.url).then(encoding => {
obj.base64 = encoding.split(',')[1];
});
}
return array;
}
// respond to message from zip() in content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
addB64(request.array).then(updatedArray => sendResponse(updatedArray));
return true;
}
);