PizzaPortal Enchancer

Skrypt dodaje brakujące opcje filtrowania do PizzaPortal

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         PizzaPortal Enchancer
// @namespace    uniquenamespace
// @version      1.0.0
// @description  Skrypt dodaje brakujące opcje filtrowania do PizzaPortal
// @author       zranoI
// @match        https://pizzaportal.pl/*/restauracje/*
// @grant        none
// ==/UserScript==

var inputValues = {
    maxPrice: Number.MAX_VALUE,
    maxDelivery: Number.MAX_VALUE
};

function apply() {
    var filterPanel = document.querySelector('.filter-and-sort-panel');
    var filterPanelContent = filterPanel.querySelector('.filter-and-sort-panel-content');
    var customFilters = createCustomFilters();
    filterPanel.insertBefore(customFilters,filterPanelContent);

    var openRestaurants = Array.from(document.querySelectorAll('.restaurant-list > ul > li'))
                           .filter(function(restaurant) {
                               var isClosed = !!restaurant.querySelector('.restaurant-closed');
                               if (isClosed) {
                                   restaurant.style.display = 'none';
                               }
                               return !isClosed;
                           });

    document.getElementById('maxPrice').addEventListener('input', function (e) {
        inputValues.maxPrice = this.value || Number.MAX_VALUE;
        updateRestaurants(openRestaurants);
    })
    document.getElementById('maxDelivery').addEventListener('input', function (e) {
        inputValues.maxDelivery = this.value || Number.MAX_VALUE;
        updateRestaurants(openRestaurants);
    });
}

function createCustomFilters() {
    var customFilters = document.createElement('div');
    customFilters.style.backgroundColor = 'white';
    customFilters.style.paddingLeft = '16px';
    customFilters.style.paddingTop = '16px';
    customFilters.style.paddingRight = '16px';
    customFilters.style.paddingBottom = '16px';
    customFilters.style.borderBottom = '1px black solid';
    customFilters.classList.add('sort-method-panel');
    customFilters.classList.add('restaurant-search-panel');
    customFilters.innerHTML =
        '<div class="sort-method-panel-title">Skryptowe opcje</div>' +
        '<input id="maxPrice" placeholder="Minimalna kwota nie większa niż" type="text" class="restaurant-search-panel-input" style="font-size: 1rem;"></input>' +
        '<input id="maxDelivery" placeholder="Dostawa nie droższa niż" type="text" class="restaurant-search-panel-input" style="font-size: 1rem; margin-top: 8px;"></input>';
    return customFilters;
}

function updateRestaurants(restaurants) {
    restaurants.forEach(function (restaurant) {
        var price = inputValues.maxPrice !== Number.MAX_VALUE ? parseFloat(restaurant.querySelector('.restaurant-payment-infos-minimum-order-value > .restaurant-payment-infos-image-container')
                                                                                     .innerText
                                                                                     .replace(',', '.'))
                                                              : 0;
        var delivery = inputValues.maxDelivery !== Number.MAX_VALUE ? parseFloat(restaurant.querySelector('.restaurant-payment-infos-delivery-fee > .restaurant-payment-infos-image-container')
                                                                                           .innerText
                                                                                           .replace(',', '.'))
                                                                    : 0;
        if (price > inputValues.maxPrice || delivery > inputValues.maxDelivery) {
            restaurant.style.display = 'none';
        } else {
            restaurant.style.display = 'block';
        }
    });
}

(function() {
    'use strict';
    setTimeout(apply, 100);
})();