S7 / asdasd

// ==UserScript==
// @name         asdasd
// @license      MIT
// @namespace    http://tampermonkey.net/
// @version      0.6.7
// @description  try to take over the world!
// @author       You
// @include      *https://www.emag.hu/search-by-filters-with-redirect?source_id=7&templates%5B%5D=full&is_eab344=false&sort%5Bprice%5D=desc&listing_display_id=2&page%5Blimit%5D=100&page%5Boffset%5D=0&fields%5Bitems%5D%5Bimage_gallery%5D%5Bfashion%5D%5Blimit%5D=2&fields%5Bitems%5D%5Bimage%5D%5Bresized_images%5D=1&fields%5Bitems%5D%5Bresized_images%5D=200x200%2C350x350%2C720x720&fields%5Bitems%5D%5Bflags%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bbuying_options%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bflags%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bbundles%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bgifts%5D=1&fields%5Bitems%5D%5Bcharacteristics%5D=listing&fields%5Bquick_filters%5D=1&search_fraze=&search_key=&filters%5Bvendor%5D%5B6429%5D%5B%5D=1&filters%5Bdepartment%5D%5B%5D=szamitastechnika&filters%5Bsubdepartment%5D%5B%5D=pc-alkatreszek&filters%5Bcategory%5D%5B%5D=37&filters%5Bshop_id%5D=1&filters%5Bpage_type%5D=vendors&ref=lst_leftbar_6409_stock*
// @grant        GM_setValue
// @grant        GM_getValue
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// ==/UserScript==

(function () {
    'use strict';

    if ($('pre').length) {

        var json = $('pre').html();
        json = JSON.parse(json);

        //console.log(json);

        $("pre").prepend(`
                                    <div id="tmpmnky_settings" style="padding:5px; background: #e5e5e5; border: 1px dotted #555">
                                    <b>Keresett kártyák:</b>
                                    <textarea id="card_types" style="width: 100%">` + (GM_getValue("keresett_kartyak") ? GM_getValue("keresett_kartyak") : "") + `</textarea>
                                    <br>
                                    <b>Kizárt kártyák:</b>
                                    <textarea id="exclude"  style="width: 100%">` + (GM_getValue("kizart_cikkszamu_kartyak") ? GM_getValue("kizart_cikkszamu_kartyak") : "") + `</textarea>
                                    <br>
                                    <div class="row" style="display:flex;">
                                    <button style="padding: 6px" id="store_settings">Mentés</button>

                                    <button style="padding: 6px" id="stop_run" disabled>Szkript leállítása</button>
                                    </div>
                                    <hr id="pause-play" style="display:flex">

 <button style="padding: 6px; float: left" id="start_run">Szkript indítása</button>

                                    </div>
    <div id="messages" style="background: #f3eadd; padding: 5px; border: 1px dotted #555"></div>
                           `);

        $([document.documentElement, document.body]).animate({
            scrollTop: $("#pause-play").offset().top
        }, 1000);

        $(document).on('click', '.exclude-this', function (e) {
            e.preventDefault();
            var basket_item_id = $(this).attr('data-id');
            var item_id = $(this).attr('data-item-id');
            if (excludeItem(item_id)) {

                console.log(item_id + " termék kizárva!");
            }

            removeFromBasket(basket_item_id);
            $(this).remove();

        });

        $(document).on('click', '.delete-from-basket', function (e) {
            e.preventDefault();
            var basket_item_id = $(this).attr('data-id');

            removeFromBasket(basket_item_id);
            $(this).remove();

        });

        $('#store_settings').click(function (e) {

            e.preventDefault();

            var card_types = $("#card_types").val();
            var exclude = $("#exclude").val();

            GM_setValue('keresett_kartyak', card_types);
            GM_setValue('kizart_cikkszamu_kartyak', exclude);

            $("#tmpmnky_settings").append('<br><span id="flash_msg" style="color: green;">Beállítások mentve!</span>');
            setTimeout(function () {
                $('#flash_msg').remove();
            }, 2000)

        });

        $('#start_run').click(function (e) {
            e.preventDefault();
            location.href = "https://ipon.hu/shop/group/463/product/data?isB2bHidden=1&limit=120&mmm=run";
        });

        $('#stop_run').click(function (e) {
            e.preventDefault();
            location.href = "https://ipon.hu/shop/group/463/product/data?isB2bHidden=1&limit=120&mmm=settings";
        });

        runScript(json);

        var refresh_interval = 5000;
        var timeleft = refresh_interval;

        $('#stop_run').prop("disabled", false);
        $('#start_run').prop("disabled", true);

        var isPaused = false;

        var downloadTimer = setInterval(function () {

            console.log(timeleft);

            if (isPaused) {
                timeleft = timeleft;

            }
            else {

                if (timeleft < 1000) {
                    clearInterval(downloadTimer);
                    location.href = "https://www.emag.hu/search-by-filters-with-redirect?source_id=7&templates%5B%5D=full&is_eab344=false&sort%5Bprice%5D=desc&listing_display_id=2&page%5Blimit%5D=100&page%5Boffset%5D=0&fields%5Bitems%5D%5Bimage_gallery%5D%5Bfashion%5D%5Blimit%5D=2&fields%5Bitems%5D%5Bimage%5D%5Bresized_images%5D=1&fields%5Bitems%5D%5Bresized_images%5D=200x200%2C350x350%2C720x720&fields%5Bitems%5D%5Bflags%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bbuying_options%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bflags%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bbundles%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bgifts%5D=1&fields%5Bitems%5D%5Bcharacteristics%5D=listing&fields%5Bquick_filters%5D=1&search_fraze=&search_key=&filters%5Bvendor%5D%5B6429%5D%5B%5D=1&filters%5Bdepartment%5D%5B%5D=szamitastechnika&filters%5Bsubdepartment%5D%5B%5D=pc-alkatreszek&filters%5Bcategory%5D%5B%5D=37&filters%5Bshop_id%5D=1&filters%5Bpage_type%5D=vendors&ref=lst_leftbar_6409_stock&rndms=2SiOGFOGJ6";

                }

                $('#start_run').html('Fut ... Következő frissítés: ' + ((timeleft) / 1000) + ' másodperc múlva');
                timeleft -= 1000;

            }
        }, 1000);

        $(document).on('click', '#pause-script', function (e) {
            e.preventDefault();
            isPaused = true;
        });

        $(document).on('click', '#play-script', function (e) {
            e.preventDefault();
            isPaused = false;
        });

        $("#pause-play").after(`<button style="padding: 6px" id="pause-script">Állj</button><button style="padding: 6px" id="play-script">Mehet</button>`);

    }else{

        if($('html').text().indexOf("szokatlanul nagy") >= 0){
            document.title = "Szokatlan forgalom -átirányítás hamarosan";


            setTimeout(function(){


                location.href = "https://www.emag.hu/search-by-filters-with-redirect?source_id=7&templates%5B%5D=full&is_eab344=false&sort%5Bprice%5D=desc&listing_display_id=2&page%5Blimit%5D=100&page%5Boffset%5D=0&fields%5Bitems%5D%5Bimage_gallery%5D%5Bfashion%5D%5Blimit%5D=2&fields%5Bitems%5D%5Bimage%5D%5Bresized_images%5D=1&fields%5Bitems%5D%5Bresized_images%5D=200x200%2C350x350%2C720x720&fields%5Bitems%5D%5Bflags%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bbuying_options%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bflags%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bbundles%5D=1&fields%5Bitems%5D%5Boffer%5D%5Bgifts%5D=1&fields%5Bitems%5D%5Bcharacteristics%5D=listing&fields%5Bquick_filters%5D=1&search_fraze=&search_key=&filters%5Bvendor%5D%5B6429%5D%5B%5D=1&filters%5Bdepartment%5D%5B%5D=szamitastechnika&filters%5Bsubdepartment%5D%5B%5D=pc-alkatreszek&filters%5Bcategory%5D%5B%5D=37&filters%5Bshop_id%5D=1&filters%5Bpage_type%5D=vendors&ref=lst_leftbar_6409_stock&rndms=2SiOGFOGJ6";

            },30000);
        }

    }

})();

function runScript(json) {

    var kosarba_cikk_szamok = [];

    //console.log(resp);

    var termekek_kosarban = [];

    // loginToIpon();length

    var keresett_kartyak = (GM_getValue("keresett_kartyak") ? GM_getValue("keresett_kartyak").split(',') : []);
    var kizart_kartyak = (GM_getValue("kizart_cikkszamu_kartyak") ? GM_getValue("kizart_cikkszamu_kartyak").split(',') : []);

    $("#messages").append('<hr><br>');

    var all_available = 0;

    //console.info(keresett_kartyak);

    $.each(json.data.items, function (key, item) {

        // console.log(item);
        var available = item.offer.availability.id;
        var termek_nev = item.name;
        var cikk_szam = item.part_number_key;
        var termek_ara = item.offer.price.current;
        var vendor_id = item.offer.vendor.id;
        var vendor_name = item.offer.vendor.name.display;

        if(available == "2"){

            all_available++;

        }

        if (available == "2" && vendor_id == "1" && findSearched(termek_nev, termek_ara, keresett_kartyak) !== -1 && findExclude(termek_nev, kizart_kartyak) !== true) {

            if ($.inArray(termek_nev, termekek_kosarban) === -1) {

                kosarba_cikk_szamok.push(item);
                $("#messages").append('<span style="color:green;">KOSÁRBA [' + cikk_szam + '] - ' + termek_nev + ' - ' + formatPrice(termek_ara) + '</span><br>');

            }
            else {

                $("#messages").append('<span style="color:orange;">MÁR KOSÁRBAN VAN</span> [' + cikk_szam + '] - ' + termek_nev + ' - ' + formatPrice(termek_ara) + '<br>');

                console.info('<span style="color:orange;">Már benne van a kosárban: ' + termek_nev + '</span>');
            }

        }
        else {

            //$("#messages").append('- ['+cikk_szam+ '] - '+ termek_nev +'<br>');
            $("#messages").append('<span style="color:black;">KIHAGYVA!</span> '+  (available == "2" && vendor_id == "1" ? "<span style='color:green'>EMAG Raktáron</span>" : '' )+' [' + cikk_szam + '] - <a href="https://emag.hu/-/pd/' + cikk_szam + '" target="_blank">' + termek_nev + '</a> - <span style="color:red">' + formatPrice(termek_ara) + '</span><br>');

        }

    });

    document.title = json.data.items.length + "/"+ all_available +"db kártya van raktáron";

    if (kosarba_cikk_szamok.length > 0) {

        var push_msg = "";
        var product_ids = "";

        kosarba_cikk_szamok.forEach(element => {

            //

            console.log(kosarba_cikk_szamok);

            //window.open('https://ipon.hu/shop/termek/'+element.slug+'/'+element.id+'?buyme','_blank');

            push_msg = push_msg.concat('<b> ' + formatPrice(element.offer.price.current) + '</b> [ <a href="https://emag.hu/-/pd/' + element.part_number_key + '">' + element.name + '</a> ] \n');

            product_ids = product_ids.concat(element.part_number_key + "|" + element.part_number_key + "|");

        });

        console.log(product_ids);

        //  window.open('https://ipon.hu/shop/kosar?buyme=1&product_ids='+product_ids+'','_blank');

        sendPushMessage(push_msg).then((resp) => {
            //location.href = 'https://www.bestbyte.hu/kosar/tartalom/pag_KosarTartalom.aspx'
        });

    }
    else {




        //$("#tmpmnky_settings").after('<div style="margin-top: 10px; margin-bottom:10px;"><span style="background: #444; color: #fff; padding: 5px; border: 1px dotted #000; width: 100%">Nem találtam új kosárba rakható motyót!</span></div>');

    }

}

async function sendPushMessage(message, sound = "bugle") {

    $.ajax({
        url: "https://api.pushover.net/1/messages.json",
        method: "post",
        data: {
            token: "arg4bh8eyw6rxjrz1nwt88te1v2n7m",
            user: "uxugbn7ifxqke3xq3unscwj4pwxbh2",
            html: 1,
            message: message,
            url: "https://www.emag.hu/vendors/videokartyak/raktaron/vendor/emag/sort-pricedesc/c?ref=lst_leftbar_6409_stock",
            title: "EMAG okosság",
            priority: 1,
            sound: sound
        },
        async: true,

        success: function (data) {

            console.log(data)

        },
        error: function (jqXHR, textStatus, errorThrown) {
            // alert("Error");
        }
    })

    return true;

}

async function addToBasket(element) {

    $.ajax({
        url: "https://ipon.hu/shop/kosar/" + element + "/add",
        method: "POST",
        async: true,

        success: function (data) {

            console.log(data);
            console.log('Cikk:' + element + ' kosárba téve')

        },
        error: function (jqXHR, textStatus, errorThrown) {
            // alert("Error");
        }
    })

}

async function getBasketData() {

    return $.ajax({
        url: "https://ipon.hu/shop/kosar/data",
        async: true,

    })

}

function findSearched(str, price, strArray) {

    for (var j = 0; j < strArray.length; j++) {

        var separator = '|';
        if (strArray[j].indexOf('|') > -1) {

            var d = strArray[j].split('|');

            if (strArray[j] !== "" && str.toUpperCase().match(strArray[j].toUpperCase()) && d[1] > price) return true;

        }
        else {

            if (strArray[j] !== "" && str.toUpperCase().match(strArray[j].toUpperCase())) return true;
        }

    }
    return -1;
}

function findExclude(str, strArray) {
    for (var j = 0; j < strArray.length; j++) {
        if (strArray[j] !== "" && str.toUpperCase().match(strArray[j].toUpperCase())) return true;

    }
    return -1;
}

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
        tmp = item.split("=");
        if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
    });
    return result;
}

function formatPrice(price) {
    return price.toLocaleString('hu-HU', {
        style: 'currency',
        currency: 'HUF',
    });
}

function excludeItem(id) {

    if (GM_setValue("kizart_cikkszamu_kartyak", GM_getValue("kizart_cikkszamu_kartyak").concat(id + ','))) {
        return true;
    }

    return false;
}

function generateRandomString(length) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() *
                                               charactersLength));
    }
    return result;
}

function removeFromBasket(id) {
    $.ajax({
        url: "https://ipon.hu/shop/kosar/" + id + "/remove",
        method: "DELETE",
        async: true,

        success: function (data) {

            //console.log(data);

            console.log('Cikk:' + id + ' eltávolítva kosárból!');

        },
        error: function (jqXHR, textStatus, errorThrown) {
            // alert("Error");
        }
    })

}