-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
51 lines (49 loc) · 1.24 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
/**
* Downloads references locally in the form of a JSON file
*/
function downloadReferences(references, filename) {
// Create file, then object URL, and download using that
// object URL
var file = new File([references], filename, {
type: "application/json",
});
var exportURL = URL.createObjectURL(file);
return browser.downloads
.download({
url: exportURL,
filename: filename,
saveAs: false,
})
.then(
(downloadId) => {
return Promise.resolve([downloadId, exportURL]);
},
(reason) => {
return Promise.reject([reason, exportURL]);
}
);
}
function handleMessage(request, sender, sendResponse) {
const { type, data } = request;
if (type === "downloadMessage") {
const { references, filename } = data;
downloadReferences(references, filename).then(
(value) => {
var [downloadId, exportURL] = value;
return sendResponse({
response: `Success! Download ID was ${downloadId}.`,
objectURL: `Export URL was: ${exportURL}`,
});
},
(error) => {
var [reason, exportURL] = error;
return sendResponse({
response: `Error! Reason was: ${reason}.`,
objectURL: `Export URL was: ${exportURL}`,
});
}
);
}
return true;
}
browser.runtime.onMessage.addListener(handleMessage);