Last active
September 20, 2024 02:40
-
-
Save artchen/da3ad9eb18252b27aae8aec243623af2 to your computer and use it in GitHub Desktop.
Add PayPal Offers
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
// ==UserScript== | |
// @name Add PayPal offers | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-04-02 | |
// @description - _- | |
// @author Art Chen | |
// @match https://www.paypal.com/shopping* | |
// @match https://www.paypal.com/offers* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=paypal.com | |
// @grant none | |
// ==/UserScript== | |
const selectors = { | |
addOfferButton: 'button[data-cy="base_offer_card__auto_save"]:not([disabled])', | |
showMoreButton: '[data-cy="tertiary_card_list__show_more_button"]' | |
}; | |
async function waitForReady(count = 10) { | |
return new Promise((resolve, reject) => { | |
const sampleButton = document.querySelector(selectors.addOfferButton); | |
if (sampleButton) { | |
resolve(sampleButton); | |
return; | |
} else if (count <= 0) { | |
reject("Retry count exhausted"); | |
return; | |
} | |
count--; | |
setTimeout(() => { | |
waitForReady(count).then(resolve).catch(reject); | |
}, 1000); | |
}); | |
} | |
function makeButton() { | |
const button = document.createElement('button'); | |
button.textContent = 'Add all offers'; | |
button.style.position = 'relative'; | |
button.style.zIndex = 10001; | |
const findOffers = () => { | |
const allOffers = document.querySelectorAll(selectors.addOfferButton); | |
button.textContent = `Remaining Offers: ${allOffers.length}. Please wait...`; | |
return allOffers; | |
}; | |
const loadMoreOffers = async () => { | |
const loadOfferBtn = document.querySelector(selectors.showMoreButton); | |
if (!loadOfferBtn) { | |
return false; | |
} | |
loadOfferBtn.click(); | |
try { | |
await waitForReady(); | |
return true; | |
} catch (e) { | |
console.log('No more offer'); | |
return false; | |
} | |
}; | |
button.addEventListener('click', async () => { | |
while (true) { | |
const nextOffer = findOffers()[0]; | |
if (nextOffer) { | |
nextOffer.click(); | |
await new Promise(r => setTimeout(r, 2000)); | |
} else if (await loadMoreOffers()) { | |
continue; | |
} else { | |
break; | |
} | |
} | |
button.textContent = `All offers added. Click to restart.`; | |
window.scrollTo({ | |
top: 0, | |
left: 0, | |
behavior: "smooth", | |
}); | |
}); | |
return button; | |
} | |
(async function() { | |
'use strict'; | |
try { | |
await waitForReady(); | |
const addAllOfferButton = makeButton(); | |
document.getElementById("header-ppLogo").parentElement.prepend(addAllOfferButton); | |
} catch(e) { | |
console.log('Found no offer', e); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
utility pls ?