Mahgoub.com Out of Stock Filter

Removes all out of stock items on mahgoub.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Mahgoub.com Out of Stock Filter
// @namespace    http://tampermonkey.net/
// @version      2025-04-22
// @description  Removes all out of stock items on mahgoub.com
// @author       SwanKnight
// @match        https://www.mahgoub.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mahgoub.com
// @grant        none
// @license     GPLv3
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Processes a given root node to find each matching product DIV.
     * If a matching product is found (i.e. it has a child DIV with class "red"),
     * its parent is removed from the DOM.
     *
     * @param {HTMLElement|Document} root
     */
    function processNode(root) {
        // Find all DIVs with class "product" within the root element.
        const productDivs = root.querySelectorAll('div.product');
        productDivs.forEach(product => {
            // Check if the product contains a descendant DIV with class "red".
            if (product.querySelector('div.red')) {
                // Retrieve the parent element of the product DIV.
                const parentDiv = product.parentElement;
                if (parentDiv) {
                    parentDiv.remove();
                }
            }
        });
    }

    // Run the initial processing when the DOM is fully loaded.
    window.addEventListener('load', () => {
        processNode(document);

        // Create a MutationObserver to watch for nodes being added to the body.
        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                // Process each added node if it is an element.
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        // Process the newly added node and its subtree.
                        processNode(node);
                    }
                });
            });
        });

        // Begin observing the document body for alterations.
        observer.observe(document.body, {
            childList: true, // Look for added or removed nodes.
            subtree: true // Observe the entire subtree.
        });
    });
})();