// ==UserScript==
// @name Aliexpress order card filler
// @namespace http://tampermonkey.net/
// @version 0.3
// @description try to take over the world!
// @author Andronio
// @match https://shoppingcart.aliexpress.com/order/secondPayment.htm?cashierRequestToken=*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
setTimeout(cardFunc, 7000);
async function cardFunc() {
let myCard = '1234 5678 9012 3456, 2021/11, xxx, JOHN SMITH';
let cardNumber = document.getElementById('cardNum');
let cardHold = document.getElementById('cardHolder');
let dateExpire = document.getElementById('expires');
let codeCVC = document.getElementById('cvv');
let mass;
mass = myCard.split(', ');
setInput(cardNumber, mass[0]);
setInput(cardHold, mass[3]);
let date = mass[1].slice(5,7) + '/' + mass[1].slice(2,4);
setInput(dateExpire, date);
setInput(codeCVC, mass[2]);
document.querySelector('input.next-checkbox-input').click(); // Снять галку
await waitForElement('.save-card-info-confirm button:nth-child(1)', 500, 10);
await waitForElement('.next-loading-tip', 500, 10);
await waitForElement('.next-loading-tip', 500, 10, true);
await sleep(200);
document.querySelector('.save-card-info-confirm button:nth-child(1)').click();
await waitForElement('input.next-checkbox-input:checked', 500, 10, true);
await sleep(200);
document.querySelector('button').click();
}
function setInput(input, value) {
if (!input) {
return;
}
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
nativeInputValueSetter.call(input, value);
input.dispatchEvent(new Event('change', {bubbles: true}));
input.dispatchEvent(new Event('keyup', {bubbles: true}));
input.dispatchEvent(new Event('keydown', {bubbles: true}));
input.dispatchEvent(new Event('keypress', {bubbles: true}));
input.dispatchEvent(new Event('input', {bubbles: true}));
input.dispatchEvent(new Event('blur', {bubbles: true}));
};
function waitForElement(selectors, interval = 250, seconds = 0, waitForDisappear = false) {
return new Promise((resolve) => {
if (!Array.isArray(selectors)) {
selectors = [selectors];
}
seconds = seconds * 1000;
const startTime = Date.now();
const check = () => {
let found = selectors.some(s => {
const el = document.querySelector(s);
return !!(el && isVisible(el));
})
if (!waitForDisappear && found || waitForDisappear && !found) {
return resolve(true);
}
if (seconds > 0 && Date.now() - startTime > seconds) {
return resolve(false);
}
setTimeout(check, interval);
};
check();
});
};
function isVisible(e) {
return !!(e.offsetWidth || e.offsetHeight || e.getClientRects().length);
};
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
};
})();