Created
May 29, 2020 16:43
-
-
Save Mozartted/1e2f63a8889d959922fc20166cf735b7 to your computer and use it in GitHub Desktop.
Hotels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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