Skip to content

Instantly share code, notes, and snippets.

@artchen
Last active March 15, 2024 01:50
Show Gist options
  • Save artchen/27fc4779891dccd9c3dfc7bc2c5c7f84 to your computer and use it in GitHub Desktop.
Save artchen/27fc4779891dccd9c3dfc7bc2c5c7f84 to your computer and use it in GitHub Desktop.
Add AMEX Offers
// ==UserScript==
// @name Add AMEX offers
// @namespace http://tampermonkey.net/
// @version 2024-03-15
// @description - _-
// @author Art Chen
// @match https://global.americanexpress.com/offers/eligible
// @icon https://www.google.com/s2/favicons?sz=64&domain=americanexpress.com
// @grant none
// ==/UserScript==
function waitForSampleButton() {
return new Promise((resolve) => {
const sampleButton = document.querySelector('button[element="card-offer-cta-label"]');
if (sampleButton) {
resolve(sampleButton);
return;
}
setTimeout(() => {
waitForSampleButton().then((btn) => resolve(btn));
}, 1000);
});
}
function makeButton(sampleButton) {
const button = sampleButton.cloneNode(true);
const span = button.querySelector('span');
span.textContent = 'Add all offers';
span.style.borderColor = "green";
span.style.color = "green";
span.style.borderWidth = "2px";
button.style.marginRight = "20px";
button.addEventListener('click', async () => {
const offerButtons = Array.from(document.querySelectorAll("[title='Add to Card']"));
for (let i = 0; i < offerButtons.length; ++i) {
offerButtons[i].click();
await new Promise(r => setTimeout(r, 2000));
span.textContent = `Added ${i + 1} / ${offerButtons.length}`;
}
window.scrollTo({
top: 0,
left: 0,
behavior: "smooth",
});
});
return button;
}
(async function() {
'use strict';
const sampleButton = await waitForSampleButton();
const myButton = makeButton(sampleButton);
const h1 = document.querySelector('h1');
h1.after(myButton);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment