Skip to content

Instantly share code, notes, and snippets.

@cuylerstuwe
Last active June 16, 2019 14:30
Show Gist options
  • Select an option

  • Save cuylerstuwe/b10690108dc9b71af8d3e909b3ef270c to your computer and use it in GitHub Desktop.

Select an option

Save cuylerstuwe/b10690108dc9b71af8d3e909b3ef270c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Amazon Pay Withdrawal QoL
// @namespace salembeats
// @version 1.51
// @description UPDATE: Money formatting fix.
// @author Cuyler Stuwe (salembeats)
// @match https://payments.amazon.com/aes/withdrawals*
// @grant GM_setValue
// @grant GM_getValue
// @noframes
// ==/UserScript==
async function insertAvailableBalance() {
const amountToDisburseElement = document.querySelector("#amountToDisburse");
const availableBalanceUsd = document.querySelector(".label-success").innerText.replace("$", "");
amountToDisburseElement.value = availableBalanceUsd;
amountToDisburseElement.dispatchEvent(new Event("change", {composed: true, bubbles: true}));
amountToDisburseElement.insertAdjacentHTML("afterend", `
<div style="background: #5cb85c; color: white;">
(Defaulted to your available balance of ${availableBalanceUsd})
</div>
`);
}
async function storeBankPreference() {
GM_setValue("preferredBank", document.getElementById("paymentInstrumentId").value);
}
async function recallBankPreference() {
const preferredBank = GM_getValue("preferredBank");
if(!preferredBank) { return; }
const bankSelectElement = document.getElementById("paymentInstrumentId");
bankSelectElement.value = preferredBank;
bankSelectElement.insertAdjacentHTML("afterend", `
<div style="background: #5cb85c; color: white;">
(Defaulted to your preferred bank)
</div>
`);
}
async function injectBankPreferenceButton() {
const bankSelectElement = document.getElementById("paymentInstrumentId");
bankSelectElement.insertAdjacentHTML("afterend", `<a href="#" id="setPreferredBank">Set As Preferred Bank</a>`);
const setPreferredBankButton = document.getElementById("setPreferredBank");
setPreferredBankButton.addEventListener("click", storeBankPreference);
}
async function waitForWithdrawInput() {
await new Promise((resolve, reject) => {
const mutationObserver = new MutationObserver(mutations => {
if(document.getElementById("amountToDisburse") && document.getElementById("paymentInstrumentId")) {
console.log("Ready (on mutations):", mutations);
mutationObserver.disconnect();
resolve();
}
});
mutationObserver.observe(document.body, {subtree: true, childList: true});
});
}
async function disableWithdrawalButtonIfInsufficientFunds() {
const fundsUsd = +(document.querySelector(".label-success").innerText.replace("$", ""));
const MINIMUM_TRANSFER_USD = 1;
if(fundsUsd < MINIMUM_TRANSFER_USD) {
const withdrawButton = document.querySelector("input[type='submit']");
withdrawButton.value = "Insufficient Funds To Withdraw";
withdrawButton.setAttribute("disabled", true);
withdrawButton.style = "outline: 1px solid red; outline-offset: 5px;";
withdrawButton.insertAdjacentHTML("afterend", `
<div style="display: inline-block; margin-left: 10px; background: red; color: white;">
You need $${(1 - fundsUsd)} more before you can initiate a transfer.
</div>`);
}
}
async function main() {
await waitForWithdrawInput();
insertAvailableBalance();
recallBankPreference();
injectBankPreferenceButton();
disableWithdrawalButtonIfInsufficientFunds();
}
(function() {
'use strict';
main();
})();
@cuylerstuwe
Copy link
Author

BEFORE:

Before

AFTER:

After

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