FAHASA

For FAHASA coupons

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         FAHASA
// @version      1.3.2
// @description  For FAHASA coupons
// @author       duongoku
// @match        https://www.fahasa.com/ma-giam-gia*
// @namespace https://greasyfork.org/users/832690
// ==/UserScript==

function apply_coupon(coupon) {
    let url = "https://www.fahasa.com/onestepcheckout/index/couponCode";

    let xhr = new XMLHttpRequest();
    xhr.open("POST", url, false);

    let data = `{"sessionId":"${SESSION_ID}","couponCode":"${coupon}","apply":"1"}`

    xhr.send(data);

    let message = `Message: ${JSON.parse(xhr.responseText).message}`;
    let noti = document.createElement('p');
    let text = document.createTextNode(message);
    noti.appendChild(text);
    document.getElementsByClassName('fhs-header_desktop')[0].appendChild(noti);
    console.log(message);
}

function create_checkout_button() {
    let btn = document.createElement('button');
    btn.innerHTML = 'CHECKOUT';
    btn.onclick = checkout;
    document.getElementsByClassName('fhs-header_desktop')[0].appendChild(btn);
}

function checkout() {
    create_checkout_button();
    window.open('https://www.fahasa.com/onestepcheckout/index#fhs_checkout_coupon', '_blank');
}

async function fetch_coupons(ids) {
    let url = '/node_api/fhsrule/event_couponsshow';
    let coupons = [];
    let count = 0;

    ids.forEach((id) => {
        $jq.ajax({
            url: url,
            data: { 'id': id, 'limit': 99 },
            method: 'POST',
            success: function (data) {
                if (data.result) {
                    for (let i = 0; i < data.periods.length; i++) {
                        if (data.periods[i].coupon_code) {
                            if (!data.periods[i].coupon_code.includes('?')) {
                                coupons.push(data.periods[i].coupon_code);
                            }
                        }
                    }
                }
                count += 1;
            }
        });
    });

    while (count < ids.length) {
        await new Promise(r => setTimeout(r, 50));
    }

    return coupons;
}

async function wait4coupons() {
    // Yes, these are hard-coded coupons ids
    let ids = [676, 677, 659, 673, 674, 657];
    let coupons = await fetch_coupons(ids);

    while (coupons.length < 1) {
        await new Promise(r => setTimeout(r, 500));
        coupons = await fetch_coupons(ids);
    }

    coupons.forEach((coupon) => {
        apply_coupon(coupon);
    });

    checkout();
}

window.onload = wait4coupons;