Make download button work

This commit is contained in:
Joey Yakimowich-Payne 2025-10-14 10:25:03 -06:00
commit a36691483f
5 changed files with 477 additions and 0 deletions

51
offscreen.js Normal file
View 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
});