(async function () {
// Close the notification if it appears
function closeNotification() {
const notifClose = document.querySelector("span#notification-close");
if (notifClose) {
notifClose.click();
}
}
// Pause for a given duration (in milliseconds)
function pause(duration = 1000) {
return new Promise(resolve => setTimeout(resolve, duration));
}
// Select all buttons that open the "Download & transfer via USB" dialog.
// Note: This uses the DOWNLOAD_AND_TRANSFER prefix.
const menus = Array.from(
document.querySelectorAll('div[id*="DOWNLOAD_AND_TRANSFERACTION"]')
).filter(el => !el.id.endsWith("CONFIRM") && !el.id.endsWith("CANCEL"));
for (let menu of menus) {
// Extract the ASIN from the menu's id.
// E.g. "DOWNLOAD_AND_TRANSFER_ACTIONB07HYK662L" -> "B07HYK662L"
const parts = menu.id.split("");
const asin = parts[parts.length - 1];
console.log(Processing book with ASIN: ${asin}
);
// Click the menu to open the dialog
menu.click();
await pause(500);
// Within the dialog, select the first radio button (device) to download.
// This selector targets the list for this ASIN.
const inputSelector = `ul#download_and_transfer_list_${asin} li[class^='ActionList-module_action_list_item__'] > div > label`;
const input = document.querySelector(inputSelector);
if (!input) {
console.warn(`No download option found for ASIN ${asin}`);
continue;
}
input.click();
await pause(500);
// Find the confirm button within the dialog for this ASIN.
const buttonSelector = `div[id^='DOWNLOAD_AND_TRANSFER_DIALOG_${asin}'] div[class^='DeviceDialogBox-module_button_container__'] > div[id$='_CONFIRM']`;
const button = document.querySelector(buttonSelector);
if (!button) {
console.warn(`No confirm button found for ASIN ${asin}`);
continue;
}
button.click();
await pause(1000);
closeNotification();
await pause(500);
}
})();