Greasy Fork

来自缓存

Greasy Fork is available in English.

自动关闭pdd商家后台弹窗

auto close pdd modal

当前为 2024-07-23 提交的版本,查看 最新版本

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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()

})();