Make extension auto grab book id

This commit is contained in:
Joey Yakimowich-Payne 2025-10-14 10:36:08 -06:00
commit 622a2045e5
3 changed files with 113 additions and 6 deletions

View file

@ -23,6 +23,41 @@ function setDisabled(disabled) {
kindleCheckbox.disabled = disabled;
}
function extractBookIdFromUrl(url) {
try {
const { pathname } = new URL(url);
const segments = pathname.split("/").filter(Boolean).reverse();
return segments.find((segment) => /^[0-9]{9,}$/.test(segment)) || null;
} catch (_) {
return null;
}
}
async function detectActiveBook() {
if (!chrome.tabs?.query) {
return;
}
try {
const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!activeTab?.url || !activeTab.url.includes("/library/view/")) {
return;
}
const detectedBookId = extractBookIdFromUrl(activeTab.url);
if (!detectedBookId) {
return;
}
if (bookIdInput.value !== detectedBookId) {
bookIdInput.value = detectedBookId;
appendLog(`Detected book ID ${detectedBookId} from the active tab.`);
}
} catch (error) {
console.warn("SafariBooks Downloader: unable to detect active book.", error);
}
}
form.addEventListener("submit", async (event) => {
event.preventDefault();
const bookId = bookIdInput.value.trim();
@ -48,3 +83,5 @@ form.addEventListener("submit", async (event) => {
setDisabled(false);
}
});
detectActiveBook();