Make download button work
This commit is contained in:
parent
7c3fd294a5
commit
a36691483f
5 changed files with 477 additions and 0 deletions
51
offscreen.js
Normal file
51
offscreen.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { SafariBooksDownloader } from "./safaribooks/downloader.js";
|
||||
import { collectSessionCookies } from "./safaribooks/cookies.js";
|
||||
|
||||
const downloader = new SafariBooksDownloader((message) => {
|
||||
console.info(`[SafariBooksDownloader] ${message}`);
|
||||
});
|
||||
|
||||
async function handleOffscreenDownload(message) {
|
||||
const { requestId, bookId, options = {} } = message;
|
||||
if (!bookId) {
|
||||
chrome.runtime.sendMessage({
|
||||
type: "offscreen-download-complete",
|
||||
requestId,
|
||||
ok: false,
|
||||
error: "Missing book ID."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await collectSessionCookies();
|
||||
await downloader.download(bookId, {
|
||||
theme: options.theme ?? "none",
|
||||
kindle: Boolean(options.kindle)
|
||||
});
|
||||
chrome.runtime.sendMessage({
|
||||
type: "offscreen-download-complete",
|
||||
requestId,
|
||||
ok: true
|
||||
});
|
||||
} catch (error) {
|
||||
chrome.runtime.sendMessage({
|
||||
type: "offscreen-download-complete",
|
||||
requestId,
|
||||
ok: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener((message) => {
|
||||
if (message?.type === "offscreen-download") {
|
||||
handleOffscreenDownload(message);
|
||||
}
|
||||
});
|
||||
|
||||
chrome.runtime
|
||||
.sendMessage({ type: "offscreen-ready" })
|
||||
.catch(() => {
|
||||
// ignore errors if background not ready yet
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue