Skip to content

Instantly share code, notes, and snippets.

@Mozartted
Created May 29, 2020 16:42
Show Gist options
  • Save Mozartted/460f03fac188a8d40428f5707b7aeeae to your computer and use it in GitHub Desktop.
Save Mozartted/460f03fac188a8d40428f5707b7aeeae to your computer and use it in GitHub Desktop.
Hotels
function isHidden(el) {
return (el.offsetParent === null)
}
function async applyCoupon (coupon) {
// async or return promise
var response = {}; // refer the format below
document.querySelector(".deals-and-discounts-toggle > button").click();
setTimeout(() => {
document.querySelector("#coupon-code-field").value = coupon;
let event = new Event('input', { bubbles: true });
document.querySelector('input[id="coupon-code-field"]').dispatchEvent(event);
document.querySelector("#coupon-code-apply-btn").click();
}, 1000)
setTimeout(() => {
if(!isHidden(document.querySelector("p[id=coupon-code-error-message]"))){
throw new Error("We don’t recognize this coupon code. Please check that you’ve entered it correctly")
}
// the coupon worked so let's play
response.store = "hotels";
response.message = document.querySelector(".coupon-reassurance-message").innerText;
response.status = true;
response.coupon_code = coupon;
let discount = document.querySelector(".c-hds-success-1.discount").firstChild.innerText.split(/\r?\n/)[0];
response.discount_amount = discount || null;
response.discount_percentage = null;
return response;
}, 4000)
}
function async applayAllCoupons(coupons) {
var response = [];
// loop coupons
for(var i = 0;i<coupons.length;i++) {
try {
let coupon = coupons[i];
let result = await applyCoupon(coupon);
response.push(result)
} catch(e) {
response.push({
store: "hotels",
message: e.message,
status: false, // based on the message
coupon_code: coupon
})
}
}
console.log(response)
// return response;
}
let coupons = ["2344355", "5SAVEMAY20"]
let response = await applayAllCoupons(coupons);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment