Last active
July 17, 2023 22:44
-
-
Save danawoodman/27fe1f17a6d419e9e1169399f16c3aa7 to your computer and use it in GitHub Desktop.
Add your Saved for Later items in your Amazon shopping cart to your first (primary) Wish List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await run(); | |
async function run() { | |
let loop = 0; | |
while (true) { | |
loop++; | |
console.log("loop:", loop); | |
const items = get_items(); | |
if (!items.length) break; | |
await add_all_to_list(items); | |
await wait(3000); | |
} | |
console.log("All done!"); | |
} | |
async function add_all_to_list(items) { | |
let deletes = []; | |
for await (const item of items) { | |
const del = await add_to_list(item); | |
if (del) deletes.push(del); | |
} | |
console.log("deletes:", deletes); | |
for (const id of deletes.filter(Boolean)) { | |
console.log("deleting:", id); | |
const to_delete = get_item(id); | |
if (!to_delete) { | |
console.error("could not find item to delete:", id); | |
continue; | |
} | |
const del = to_delete.querySelector('input[data-action="delete"]'); | |
del.click(); | |
await wait(200); | |
} | |
} | |
async function add_to_list(item) { | |
// Click the "Add to list" link | |
const add_to_list_link = item.querySelector( | |
'[data-action="add-to-list-popover"] input' | |
); | |
if (!add_to_list_link) | |
return console.log("no add to list link for:", item.dataset.itemid); | |
console.log("------------------------------------------------"); | |
console.log("attempting to add to list:", item.dataset.itemid); | |
add_to_list_link.click(); | |
await wait(1000); | |
// Click wish list link to add to wish list | |
const list = document.querySelector( | |
'.a-popover[aria-hidden="false"] .a-dropdown-link' | |
); | |
if (!list) return console.error("no list link for: " + item.dataset.itemid); | |
console.log("adding to list..."); | |
list.click(); | |
await wait(1000); | |
const success = item.querySelector(".add-to-list-success-label"); | |
if (!success) | |
return console.error("did not add item to list: " + item.dataset.itemid); | |
const del = item.querySelector('input[data-action="delete"]'); | |
if (!del) return; | |
return item.dataset.itemid; | |
} | |
function get_items() { | |
return Array.from( | |
document.querySelectorAll( | |
'#sc-saved-cart-items > div:not([data-deleted="true"])' | |
) | |
); | |
} | |
function get_item(id) { | |
return document.querySelector( | |
'#sc-saved-cart-items > div[data-itemid="' + | |
id + | |
'"]:not([data-deleted="true"])' | |
); | |
} | |
function wait(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment