Обсуждения » Разработка

Coupon value copy problems - aliexpress

Please help to restore the script. He works on this and this page. Copying coupon values does not work.
(function() {
'use strict';
let coupons = document.querySelectorAll('.coupon-ul .clearfix .coupon-ul');
if (coupons.length) {
for (let i = 0; i < coupons.length; i++) {
let couponBtn = document.createElement('li');
couponBtn.innerHTML = `Get value`;
coupons[i].append(couponBtn);
}
document.querySelector('.use-coupons-info-item').addEventListener('click', eventHandler);
}

function eventHandler(ev) {
let elem = ev.target;
if (elem.tagName != 'BUTTON' || !elem.dataset.couponNumber) return;
let num = +elem.dataset.couponNumber;
let couponValue = document.querySelectorAll('.use-coupons-info-lang-price')[num].innerText;
let couponFrom = document.querySelectorAll('.use-coupons-info-lang-limit')[num].innerText;
let couponDate = document.querySelectorAll('.valid-period-coupon')[num].innerText;
let str;
if (couponValue.startsWith("US $")) {
couponValue = couponValue.match(/\d{1,2}\.\d{2}/g)[0];
couponFrom = couponFrom.match(/\d{1,2}\.\d{2}/g)[0];
couponDate = couponDate.match(/(?<=PT\s-\s)(?:(\d{1,2}\s[a-zA-Zа-яё]{3})|(?:[a-zA-Zа-яё]{3}\s\d{1,2}))/)[0];
str = `${couponValue}/${couponFrom}$ до ${couponDate}`;
}
if (couponValue.endsWith('руб. Off')) {
couponValue = couponValue.match(/\d{1,4},\d{2}/g)[0];
couponFrom = couponFrom.match(/\d{1,4},\d{2}/g)[0];
couponDate = couponDate.match(/(?<=PT\s-\s)(?:(\d{1,2}\s[a-zA-Zа-яё]{3})|(?:[a-zA-Zа-яё]{3}\s\d{1,2}))/)[0];
str = `${couponValue}/${couponFrom} руб. до ${couponDate}`;
}
navigator.clipboard.writeText(str).then(function () {
console.log('Async: Copying to clipboard was successful!');
elem.classList.remove("use-link-button-show");
elem.classList.add("coupon-valid-status");
setTimeout(() => {elem.classList.remove("coupon-valid-status"); elem.classList.add("use-link-button-show");}, 500);
}, function (err) {
console.error('Async: Could not copy text: ', err);
});
}
})();

And please tell how to add to the script copying the name store that owns the coupon by this link.

I've only 6 expired coupons

First of all this whole function
function eventHandler(ev) {
should be added above this line
let coupons = document.querySelectorAll('.coupon-ul .clearfix .coupon-ul');

On my end the advent listener isn't being added to the document.querySelector('.use-coupons-info-item') element, this advent listener would only be added to the first coupon anyways, you should use queryselectorAll if you want every coupoun to have the advent listener.

Install this script and try using the "Get value" button. The validity period of the coupon in your account does not matter. The script should copy the coupon values to the clipboard, but it doesn't.
Ultimately I want to achieve a result like this: $10/20, Amazing Store.
We need to add the function to copy the store name.

I am using Google Translate, sorry for the mistakes in the text.

What's your native language?

You didn't share a link of a script on greasyfork to be installed, but I've tried running your script a few times.

I could see that the "Get value" text was added nearby the coupons, but that's all, nothing else happens.
If you just want to achieve a result like this: $10/20, Amazing Store, it's probably better if you write your own codes from scratch.

You know how to program in JS right? Oh... You don't. You messed up again, this is the development board, not the creation requests board!

§
Создано: 24.12.2020
Отредактировано: 24.12.2020

var Coupouns = document.querySelectorAll("div.use-coupons-details > ul > li:nth-child(3) > a");
var Result = []; //Creates a new blank array
for (var i = Coupouns.length; i--;) { //Starts the for condition
var StoreName = Coupouns[i].innerText;
var CouponValue = document.querySelectorAll("span.use-coupons-info-lang-price")[i].innerText.match(/\d{1,2}\.\d{2}/g)[0];
Result.push('$'+CouponValue+" "+StoreName); //Add To The Array
copy(Result);
} //Finishes the for condition

Ответить

Войдите, чтобы ответить.