Skip to content

Instantly share code, notes, and snippets.

@SeanMcGrath
Last active April 24, 2024 18:12
Show Gist options
  • Save SeanMcGrath/fb4f9e1869a2252738c9398d5ceb5395 to your computer and use it in GitHub Desktop.
Save SeanMcGrath/fb4f9e1869a2252738c9398d5ceb5395 to your computer and use it in GitHub Desktop.
JS code to share all items in a poshmark closet to followers and all available events
const clickDelta = 4000; // ms delay between clicks
const cycleDelta = clickDelta * 100; // ms delay between share cycles
const clickLinks = (el) => {
el.click();
// set a short timeout so there's time to load in the active parties/render the modal
setTimeout(() => {
document
.querySelectorAll("[data-et-name='share_poshmark']")
.forEach((el) => el.click());
}, 200);
};
const notSold = (el) => {
return (
el
.closest(".card")
.querySelectorAll(".sold-tag,.sold-out-tag,.not-for-sale-tag")
.length === 0
);
};
// check if there are any sold tags on page
const hasSold = () => document.querySelectorAll(".sold-tag,.sold-out-tag").length > 0
// one screen height from the bottom
const nearBottom = () => document.body.scrollHeight - window.innerHeight;
const scrollToBottomAndWait = () => {
if (!hasSold()) {
window.scrollTo(0, nearBottom());
}
return new Promise((resolve) => {
setTimeout(() => {
const isScrolledToBottom =
window.innerHeight + window.scrollY >= nearBottom();
if (!isScrolledToBottom && !hasSold()) {
resolve(scrollToBottomAndWait());
} else {
resolve();
}
}, 2000);
});
};
const share = async () => {
await scrollToBottomAndWait();
let timeout = 0;
const doShare = (el) => {
if (notSold(el)) {
// register link clicking
setTimeout(() => clickLinks(el), timeout);
// make sure next registered click comes after
timeout += clickDelta;
}
};
const shareLinks = Array.from(document.querySelectorAll("[data-et-name=share]")).reverse();
shareLinks.forEach(doShare);
};
share();
setInterval(share, cycleDelta);
@biancawriter
Copy link

@SeanMcGrath Thanks for sharing this code! I have a question about the last 2 lines:

 share();
 setInterval(share, cycleDelta);

I'm presuming that cycleDelta get incremented after it's first declared, so that it adds up to the total amount of time it takes to get through one cycle. But I don't understand how. Can you explain? (I'm new to JavaScript, so apologies if this is a silly question.)

@SeanMcGrath
Copy link
Author

cycleDelta is the interval between runs of the share function. the value of cycleDelta itself is constant. setInterval(x, y) runs the specified function x every y milliseconds.

@SeanMcGrath
Copy link
Author

updated to incorporate @ianmcilwraith 's changes (thanks!) and also stop scrolling down the page once sold items are found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment