// ==UserScript==
// @name Aliexpress order input card filler
// @namespace http://tampermonkey.net/
// @version 0.4
// @description try to take over the world!
// @author Andronio / NetSVrn
// @match https://shoppingcart.aliexpress.com/order/secondPayment.htm?cashierRequestToken=*
// @grant none
// ==/UserScript==
let timeout = 50;
(function() {
'use strict';
var div = document.createElement('div');
div.className = 'myBox';
div.innerHTML += `
<input type="text" id="carddata"></br>
<input type="button" id="cardfill" class="mybutton" value="Карта">
`;
// Стили
var styles = `
.myBox {
position: fixed;
top: 0;
right: 0;
background: white;
box-shadow: 1px -1px 4px 1px;
max-width: 40%;
max-height: 400px;
padding: 10px 20px;
overflow-y: auto;
overflow-x: hidden;
z-index:9999;
}
.mybutton {
display: inline;
padding: 5px 10px;
margin-right:auto;
cursor:pointer;
}`
var styleSheet = document.createElement("style")
styleSheet.type = "text/css"
styleSheet.innerText = styles
document.head.append(styleSheet)
document.body.append(div);
let mybutton1 = document.getElementById("cardfill");
mybutton1.addEventListener('click', cardFunc);
let mytext = document.getElementById("carddata");
mytext.addEventListener('keydown', event => {
if (event.keyCode == "13") {
document.getElementById('cardfill').click();
}
});
mytext.focus();
})();
async function cardFunc() {
let cardNumber = document.getElementById('cardNum');
let cardHold = document.getElementById('cardHolder');
let dateExpire = document.getElementById('expires');
let codeCVC = document.getElementById('cvv');
let mytext = document.getElementById("carddata")
let mass;
if (mytext.value == "") return null;
if (/\d{4}\s\d{4}\s\d{4}\s\d{4},\s\d{4}\/\d\d,\s\d{3},\s[\w\s]+,\s/.test(mytext.value)) {
mass = mytext.value.split(', ');
} else {
return alert("Нет данных карты");
}
let nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
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));
};