Cart - Flip

Опции для корзины

当前为 2023-10-24 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           Cart - Flip
// @namespace      scriptomatika
// @author         mouse-karaganda
// @description    Опции для корзины
// @license        MIT
// @include        https://*flip.kz/cart*
// @require        https://greasyfork.org/scripts/379902-include-tools/code/Include%20Tools.js
// @version        1.3
// @grant          none
// ==/UserScript==

(function() {
    const $ = window.jQuery;
    const $$ = window.__krokodil;

    $$.renderStyle(
        '.unavailable_frame { position: fixed; bottom: 0; right: 0; padding: 5px 0 5px 5px; border-radius: 10px 0 0 0; background-color: rgba(10, 121, 213, 0.9); }',
        '.product_frame { display: inline-block; }',
        '.product_frame > div { display: inline-block; }',
        '.product_frame img { width: 32px; border-radius: 5px; }',
        '.product_frame span { margin-left: 10px; color: white; font-size: 12px; }'
    );

    let unavailableList;

    let unavailableFrame = $('<div class="unavailable_frame" />').appendTo(document.body);
    let innerDiv = $('<div />').appendTo(unavailableFrame);

    $('<a class="nbtn gray small m-r-10" />').appendTo(innerDiv).text('К недоступному')
        .click(function(event) {
            let listExists = (unavailableList && unavailableList.length > 0);
            let viewRow = (listExists) ? unavailableList[0].row : $('#module-cart .row').last();
            viewRow.get(0).scrollIntoView();
        });

    $('<a class="nbtn gray small m-r-10" />').appendTo(innerDiv).text('Открыть 3')
        .click(function(event) {
            let period = 500;
            setTimeout(function() {
                window.open('', 'manual_postpone0', 'width=450,height=300,left=0,top=0');
            }, 1);
            setTimeout(function() {
                window.open('', 'manual_postpone1', 'width=450,height=300,left=455,top=0');
            }, period);
            setTimeout(function() {
                window.open('', 'manual_postpone2', 'width=450,height=300,left=910,top=0');
            }, period * 2);
        });

    let productFrame = {
        outer: null,
        clear: function() {
            this.outer.empty();
            this.outer.removeAttr('title');
            this.outer.addClass('m-r-10');
        }
    };

    let comingList = [];

    let findInComingList = function(productId) {
        let comingPage = 0;
        
    };

    let currentIndex, maxIndex;
    let currentPage, maxPage;

    let requestPostponedItem = function() {
        currentIndex++;
        if (currentIndex >= maxIndex)
            return;

        productFrame.clear();

        currentPage = 1;
        maxPage = 1;

        let product = unavailableList[currentIndex].product;

        let div = $('<div />').appendTo(productFrame.outer);
        $('<img />').appendTo(div).attr('src', product.img);

        div = $('<div />').appendTo(productFrame.outer);
        $('<span />').appendTo(div).text(product.name.substring(0, 5) + '…');
        $('<br />').appendTo(div);
        $('<span />').appendTo(div).text(`товар ${currentIndex + 1} / ${maxIndex}`);
        $('<br />').appendTo(div);
        $('<span />').appendTo(div).text(`стр ${currentPage} / ${maxPage}`);
        productFrame.outer.attr('title', product.name);

        $.get('/user?personalis=coming&page=' + currentPage, function(data) {
            let rowList = $('.table.goods .row', data);
            console.log('AJAX rowList == ', rowList);
        });
    };

    $('<a class="nbtn gray small m-r-10" />').appendTo(innerDiv).text('Проверить')
        .click(function(event) {
            let listExists = (unavailableList && unavailableList.length > 0);
            if (!listExists) {
                alert('Недоступных нет');
                return;
            }

            maxIndex = unavailableList.length;
            currentIndex = -1;

            // Получить список ожидаемых постранично до тех пор, пока не найдем нужный товар
            requestPostponedItem();
        });

    productFrame.outer = $('<div class="product_frame" />').appendTo(innerDiv);

    class ProductRow {
        constructor(rowElem) {
            this.row = $(rowElem);
            if (this.isCartPage) {
                this.delivery = $('label + div + div', rowElem);
            }
        }
        get isCartPage() {
            return /\/cart\b/.test(location.href);
        }
        get isUnavailable() {
            if (this.delivery) {
                return this.delivery.text().includes('Недоступен для заказа');
            }
            return false;
        }
        get product() {
            if (!this._product) {
                let catalogElem = $('a[href^="/catalog?"]', this.row);
                this._product = {
                    id: catalogElem.attr('href').match(/\d+/)[0],
                    img: catalogElem.children('img').attr('src'),
                    name: catalogElem.text()
                };
            }
            return this._product;
        }
        addPostponeFrame(winIndex) {
            this.row.addClass('add_postpone_row');

            let div = $('<div />').appendTo(this.delivery).addClass('postpone_frame');

            let button = $('<a class="nbtn gray small" />').appendTo(div).text('Отложить')
                .attr({
                    target: 'manual_postpone' + winIndex,
                    href: '/subscribe?type=preorder&action=add&id=' + this.product.id
                })
                .click(function(event) {
                    setTimeout(function() {
                        //div.remove();
                        $('<span />').text(' ✅ ').insertAfter(button);
                    }, 500);
                });
        }
    }

    unavailableList = $('#module-cart .row')
        .filter(function(index) {
            let prow = new ProductRow(this);
            return prow.isUnavailable;
        })
        .map(function(index) {
            let prow = new ProductRow(this);
            prow.addPostponeFrame(index % 3);
            return prow;
        })
        .get();
    console.log('unavailableList [%o] == ', unavailableList.length);

    console.log('Cart - Flip 💬 1.3');
})();