Cart - Flip

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

目前為 2023-10-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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');
})();