自动关闭pdd商家后台弹窗

auto close pdd modal

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         自动关闭pdd商家后台弹窗
// @namespace    http://tampermonkey.net/
// @version      2024-07-23
// @description  auto close pdd modal
// @author       haoyue
// @match        https://mms.pinduoduo.com/**
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pinduoduo.com
// @grant        none
// @license
// ==/UserScript==

(function() {
    'use strict';

    // Function to check and close the modal
    function handleClose(modal) {
        const closeButton = modal.querySelectorAll('[class*="close"]')[0]


        if (closeButton) {
            closeButton.click()
            console.info('click close button')
        } else {
            // If no close button is found, remove the modal and the mask directly
            modal.remove()
            const mask = document.querySelector('[data-testid="beast-core-modal-mask"]')
            if (mask) {
                mask.remove()
                console.info('remove mask')
            }
        }
    }

    function checkModal(modal) {
        return modal.textContent.includes('自动跟价') || modal.textContent.includes('设置西藏包邮额度订单')
    }

    function checkAndCloseModal() {
        // Find the modal element
        const modal = document.querySelector('[data-testid="beast-core-modal"]')
        if (modal && checkModal(modal)) {
            // Check if the modal content contains the keyword
            console.info('出现了自动跟价')
            // Try to find the close button by looking for the keyword "关闭"
            handleClose(modal)

        }
    }

    // Function to observe route changes
    function observeRouteChanges() {
        const originalPushState = history.pushState
        const originalReplaceState = history.replaceState

        // Override pushState
        history.pushState = function () {
            originalPushState.apply(this, arguments)
            checkAndCloseModal()
        }

        // Override replaceState
        history.replaceState = function () {
            originalReplaceState.apply(this, arguments)
            checkAndCloseModal()
        }

        // Listen for popstate event (back/forward navigation)
        window.addEventListener('popstate', checkAndCloseModal)
    }

    // Function to observe DOM changes
    function observeDOMChanges() {
        const observer = new MutationObserver(mutations => {
            mutations.forEach(() => {
                checkAndCloseModal()
            })
        })

        observer.observe(document.body, {
            childList: true,
            subtree: true,
        })
    }

    // Start observing route changes and DOM changes
    // observeRouteChanges()
    observeDOMChanges()

    // Initial check in case the modal is already present
    checkAndCloseModal()

})();