link工具

点击复制,提取链接

目前為 2024-09-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name         link工具
// @namespace    http://tampermonkey.net/
// @version      0.8.3.1
// @description  点击复制,提取链接
// @author       lwj
// @match        *://m.linkmcn.com/*
// @match        *://detail.tmall.com/item*
// @match        *://item.taobao.com/item*
// @match        *://chaoshi.detail.tmall.com/item*
// @match        *://traveldetail.fliggy.com/item*
// @match        *://detail.tmall.hk/hk/item*
// @license      MIT
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_setClipboard
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function () {
    // 版本号
    var versionTitleTxt = 'Version 0.8.3.1';
    // 检查当前页面 URL 是否匹配指定的网址
    var isHomeURL = function () {
        var currentURL = window.location.href;
        return currentURL.indexOf("https://m.linkmcn.com/#/live/plan?select=") !== -1;
    };

    // 从 localStorage 中获取上一次的 URL,如果没有则设置为空字符串
    let lastCurrentURL = localStorage.getItem('lastCurrentURL') || '';

    var isTableCardURL = function () {
        return window.location.href.indexOf("https://m.linkmcn.com/#/live/plan/tableCard/") !== -1;
    };

    var isBatchPrintURL = function () {
        return window.location.href.indexOf("https://m.linkmcn.com/#/live/plan/batchPrint") !== -1;
    };

    var isTmallItemURL = function () {
        var url = window.location.href;

        // 检查URL是否包含任何指定的域名或路径
        return (
            url.indexOf("https://detail.tmall.com/item") !== -1 ||
            url.indexOf("https://detail.tmall.hk/hk/item") !== -1 ||
            url.indexOf("https://item.taobao.com/item") !== -1 ||
            url.indexOf("https://chaoshi.detail.tmall.com/item") !== -1 ||
            url.indexOf("https://traveldetail.fliggy.com/item") !== -1
        );
    }

    var notificationTimer; // 通知计时器
    var isHiding = false; // 标志,表示是否正在隐藏通知
    let countSort_notificationTimeout = null;// 排序提示定时器

    let temp_itemId = '';

    class ToggleButtonComponent {
        constructor(localStorageKey, buttonText, dropdownContainer, useSpecialSwitch = 0, defaultState = false) {
            this.localStorageKey = localStorageKey;
            this.switchState = localStorage.getItem(this.localStorageKey) === null ? defaultState : localStorage.getItem(this.localStorageKey) === 'true';
            this.buttonText = buttonText;
            this.dropdownContainer = dropdownContainer;
            this.useSpecialSwitch = useSpecialSwitch === 1;
            this.createComponent();
        }

        createComponent() {
            // 创建按钮容器
            this.buttonContainer = document.createElement('div');
            this.buttonContainer.classList.add('flex', 'items-center', 'dropdown-item');
            this.buttonContainer.style.cssText = 'padding: 12px 16px; user-select: none; cursor: pointer; display: flex; justify-content: space-between; align-items: center;';

            if (this.buttonText === '纯净模式') {
                this.buttonContainer.style.paddingTop = '0px';
            }
            // 创建按钮文本
            this.buttonTextElement = document.createElement('span');
            this.buttonTextElement.textContent = this.buttonText;
            this.buttonTextElement.classList.add('lh-22');

            this.buttonContainer.appendChild(this.buttonTextElement);

            if (this.useSpecialSwitch) {
                // 创建特殊开关样式按钮
                this.createSpecialSwitch();
            } else {
                // 创建普通按钮
                this.createStandardButton();
            }

            // 将按钮容器添加到下拉容器中
            this.dropdownContainer.appendChild(this.buttonContainer);

            // 创建对应的空的子页面并设置样式
            this.secondaryContent = document.createElement('div');
            this.secondaryContent.id = this.localStorageKey + '-secondary-content';
            this.secondaryContent.style.cssText = 'margin: 0px 10px; display: none; padding: 10px 12px; background: #E9E9E9; border: 1px solid #D2D2D2; border-radius: 10px;';

            // 将子页面添加到按钮容器的下方
            this.dropdownContainer.appendChild(this.secondaryContent);
        }

        createStandardButton() {
            this.button = document.createElement('button');
            this.button.id = 'main_standardFunShowButton';
            this.button.style.cssText = 'background: none; border: none; font-size: 16px; cursor: pointer; transition: transform 0.3s; margin: 0px 6px';
            this.button.appendChild(createSVGIcon());
            this.buttonContainer.appendChild(this.button);

            // 绑定点击事件
            this.button.addEventListener('click', () => this.handleStandardButtonClick());
            // 给标题绑定模拟点击开关事件
            this.buttonTextElement.addEventListener('click', () => this.titleToSwitchClick());
        }

        handleStandardButtonClick() {
            // 切换二级页面显示状态
            const secondaryContent = document.getElementById(this.localStorageKey + '-secondary-content');
            if (secondaryContent) {
                secondaryContent.style.display = secondaryContent.style.display === 'block' ? 'none' : 'block';
            } else {
                console.log(`${this.buttonText} 二级页面异常`);
            }

            // 旋转按钮图标
            const icon = this.button.querySelector('svg');
            const rotation = icon.style.transform === 'rotate(90deg)' ? 'rotate(0deg)' : 'rotate(90deg)';
            icon.style.transform = rotation;
        }

        createSpecialSwitch() {
            this.button = document.createElement('button');
            this.button.innerHTML = '<div class="ant-switch-handle"></div><span class="ant-switch-inner"><span class="ant-switch-inner-checked"></span><span class="ant-switch-inner-unchecked"></span></span>';
            this.button.setAttribute('type', 'button');
            this.button.setAttribute('role', 'switch');
            this.button.setAttribute('aria-checked', this.switchState); // 设置开关状态
            this.button.classList.add('ant-switch', 'css-9fw9up');
            if (this.switchState) {
                this.button.classList.add('ant-switch-checked');
            }
            this.buttonContainer.appendChild(this.button);
            this.buttonContainer.style.cursor = 'default';

            // 添加点击事件监听
            this.button.addEventListener('click', () => this.handleSwitchClick());
        }

        handleSwitchClick() {
            // 切换开关状态
            const newState = this.button.getAttribute('aria-checked') === 'true' ? 'false' : 'true';
            this.button.setAttribute('aria-checked', newState);

            if (newState === 'true') {
                this.button.classList.add('ant-switch-checked');
                showNotification(`${this.buttonText}:开启`);
            } else {
                this.button.classList.remove('ant-switch-checked');
                showNotification(`${this.buttonText}:关闭`);
            }

            // 更新开关状态
            updateSwitchState(this.localStorageKey, newState === 'true');
        }

        titleToSwitchClick() {
            // 模拟点击开关按钮
            this.button.click();
        }

        remove_titleToSwitchClick() {
            // 移除模拟点击开关按钮
            this.buttonTextElement.removeEventListener('click', () => this.titleToSwitchClick());
        }

        // 获取开关状态
        getSwitchState() {
            return this.button.getAttribute('aria-checked') === 'true';
        }

        // 获取子页面元素
        getSecondaryContent() {
            return this.secondaryContent;
        }
    }

    class SonToggleButtonComponent extends ToggleButtonComponent {
        constructor(localStorageKey, buttonText, dropdownContainer, descriptionText, useSpecialSwitch = false, defaultState = false) {
            super(localStorageKey, buttonText, dropdownContainer, useSpecialSwitch, defaultState);
            this.buttonText = buttonText;
            this.descriptionText = descriptionText;

            this.createAdditionalContent();
        }

        // 创建附加内容容器
        createAdditionalContent() {
            // 修改按钮文本样式并去除 class="lh-22"
            this.buttonTextElement.style.cssText = 'font-size: 14px; margin: 0;';
            this.buttonTextElement.classList.remove('lh-22');

            // 调整父类的内容容器样式
            this.buttonContainer.style.cssText = 'display: flex; user-select: none; justify-content: space-between; align-items: center; padding: 0;';

            // 新增的说明容器
            this.descriptionContainer = document.createElement('div');
            this.descriptionContainer.classList.add('description-content');
            this.descriptionContainer.style.cssText = 'font-size: 12px; color: #9B9B9B; user-select: none; margin: 5px 0px; padding-bottom: 5px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #D2D2D2; white-space: pre-wrap;';
            this.descriptionContainer.textContent = this.descriptionText;

            // 子设置容器
            this.childSettingsContainer = document.createElement('div');
            this.childSettingsContainer.classList.add('child-settings');
            this.childSettingsContainer.style.cssText = 'display: block; justify-content: space-between; align-items: center;';

            // 初始化子开关容器的样式
            this.updateSwitchStyle();

            // 将说明容器和子设置容器添加到下拉容器中
            this.dropdownContainer.appendChild(this.buttonContainer);
            this.dropdownContainer.appendChild(this.descriptionContainer);
            this.dropdownContainer.appendChild(this.childSettingsContainer);
        }

        // 重写createSpecialSwitch以添加额外的类
        createSpecialSwitch() {
            // 确保父类方法被正确调用
            super.createSpecialSwitch();

            // 确保 this.button 已初始化
            if (!this.button) {
                console.error('this.button is not initialized');
                return;
            }

            // 将函数定义为对象的方法
            this.autoInput_handleSwitchClick = () => {
                if (!this.getSwitchState()) {
                    itemSort_inputConter.showSwitchDiv(true);  // 显示输入内容选择框
                    itemSort_countInputFun.showNumberControlDiv(false);  // 隐藏数字控制区
                    itemSort_countInputFun.toggleDivButtonState(false);  // 关闭数字控制按钮
                }
            };

            if (this.buttonText === '自动填充') {
                // console.log(this.buttonText);
                this.button.addEventListener('click', () => this.autoInput_handleSwitchClick());
            }

            // 添加额外的类
            this.button.classList.add('ant-switch-small');
        }

        // 独立的方法用于更新开关样式
        updateSwitchStyle() {
            if (!this.getSwitchState()) {
                initializeContainerStyle(this.childSettingsContainer, false); // 使用函数初始化
            }
        }

        // 控制单独组件的开启关闭
        showSwitchDiv(flag) {
            if (flag) {
                initializeContainerStyle(this.buttonContainer, true);
                initializeContainerStyle(this.descriptionContainer, true);
            } else {
                initializeContainerStyle(this.buttonContainer, false);
                initializeContainerStyle(this.descriptionContainer, false);
            }
        }

        createSelectBox(options = ['', '0', '1', '2', '3', '999'], savedKey = this.localStorageKey + '_savedValue', container = this.buttonContainer) {
            // 先移除button元素
            if (this.button) {
                this.buttonContainer.removeChild(this.button);
            }

            // 创建下拉框
            const selectBox = document.createElement('select');
            selectBox.setAttribute('id', this.localStorageKey + '-select-box');
            selectBox.style.cssText = `
                font-size: 14px;
                margin: 0;
                width: 42px;
                height: 18px;
                border: 1px solid rgb(155, 155, 155);
                background-color: rgb(249,249,249);
                color: rgb(155, 155, 155);
                border-radius: 20px;
                padding: 0 2.5%;
                -webkit-appearance: none;
                -moz-appearance: none;
                appearance: none;
                position: relative;
                cursor: pointer;
            `;

            options.forEach(value => {
                const option = document.createElement('option');
                option.value = value;
                option.style.cssText = `
                    text-align: center;
                    padding: 0 5px;
                `;
                option.textContent = value === '' ? '空' : value;
                selectBox.appendChild(option);
            });

            const savedValue = localStorage.getItem(savedKey) || options[1];
            selectBox.value = savedValue;

            selectBox.addEventListener('change', () => {
                localStorage.setItem(savedKey, selectBox.value);
                updateSwitchState(this.localStorageKey, selectBox.value);

                // 悬浮时和激活时边框颜色变化
                selectBox.style.borderColor = '#ff6200';
            });

            // 悬浮时和激活时边框颜色变化
            selectBox.addEventListener('mouseover', function () {
                selectBox.style.borderColor = '#ff6200';
            });

            selectBox.addEventListener('mouseout', function () {
                if (!selectBox.matches(':focus')) {
                    selectBox.style.borderColor = 'rgb(155, 155, 155)';
                }
            });

            selectBox.addEventListener('focus', function () {
                selectBox.style.borderColor = '#ff6200';
            });

            selectBox.addEventListener('blur', function () {
                selectBox.style.borderColor = 'rgb(155, 155, 155)';
            });

            container.appendChild(selectBox);
        }

        createDivButton(beforeText, afterText, onClickCallback = null, container = this.buttonContainer) {
            // 先移除button元素
            if (this.button) {
                this.buttonContainer.removeChild(this.button);
            }

            this.beforeText = beforeText;
            this.afterText = afterText;
            this.onClickCallback = onClickCallback;

            // 功能开启按钮
            this.divButton = document.createElement('div');
            this.divButton.setAttribute('id', this.localStorageKey + '_divButton');
            this.divButton.textContent = beforeText;
            this.divButton.style.cssText = `
                font-size: 14px;
                margin: 0;
                width: 42px;
                height: 18px;
                border: 1px solid rgb(155, 155, 155);
                background-color: rgb(249,249,249);
                color: rgb(155, 155, 155);
                border-radius: 20px;
                padding: 0 2%;
                -webkit-appearance: none;
                -moz-appearance: none;
                appearance: none;
                position: relative;
                cursor: pointer;
                user-select: none;
                text-align: center;
            `;

            // 悬浮时和激活时边框颜色变化
            this.divButton.addEventListener('mouseover', () => {
                if (this.divButton.textContent === beforeText) {
                    this.divButton.style.borderColor = '#ff6200';
                }
                else {
                    this.divButton.style.borderColor = 'rgb(155, 155, 155)';
                }
            });

            this.divButton.addEventListener('mouseout', () => {
                if (this.divButton.textContent === beforeText) {
                    this.divButton.style.borderColor = 'rgb(155, 155, 155)';
                }
                else {
                    this.divButton.style.borderColor = 'rgb(249, 249, 249)';
                }
            });

            // 按钮点击事件
            this.divButton.addEventListener('click', () => {
                this.toggleDivButtonState();
            });

            container.appendChild(this.divButton);
        }

        // 控制单独组件的开启关闭
        toggleDivButtonState(isActivated = null) {
            // 如果提供了isActivated参数,则根据参数设置状态,否则根据当前状态切换
            const shouldActivate = isActivated !== null ? isActivated : (this.divButton.textContent === this.beforeText);

            if (shouldActivate) {
                this.divButton.textContent = this.afterText;
                this.divButton.style.color = '#fff';
                this.divButton.style.backgroundColor = '#ff0000';
                this.divButton.style.borderColor = '#fff';
                showNotification(`${this.buttonText}:开启`);
                if (this.onClickCallback) {
                    this.onClickCallback(true);
                }
            } else {
                this.divButton.textContent = this.beforeText;
                this.divButton.style.color = 'rgb(155, 155, 155)';
                this.divButton.style.backgroundColor = 'rgb(249,249,249)';
                this.divButton.style.borderColor = 'rgb(155, 155, 155)';
                showNotification(`${this.buttonText}:关闭`);
                if (this.onClickCallback) {
                    this.onClickCallback(false);
                }
            }
        }

        // 判断开关开启状态
        getDivButtonState() {
            if (this.divButton.textContent === this.beforeText) {
                return false;// 开关开启状态
            } else {
                return true;// 开关关闭状态
            }
        }

        createNumberControDiv(defaultNumber = 0, single = false, countType = 'none', savedKey = this.localStorageKey + '_savedValue', container = this.buttonContainer) {
            if (single) {
                //先移除button元素
                if (this.button) {
                    this.buttonContainer.removeChild(this.button);
                }
            }

            // 数字文本及SVG控制区
            this.numberControDiv = document.createElement('div');
            this.numberControDiv.style.cssText = `
                font-size: 14px;
                display: flex; 
                align-items: center;
                min-width: 42px;
                height: 18px;
                width: auto;
                border: 1px solid rgb(155, 155, 155);
                background-color: rgb(249,249,249);
                color: rgb(155, 155, 155);
                border-radius: 20px;
                padding: 0 2.5%;
                -webkit-appearance: none;
                -moz-appearance: none;
                appearance: none;
                position: relative;
                cursor: default;
            `;

            // 数字文本
            this.countText = document.createElement('span');
            this.countText.textContent = defaultNumber; // 使用默认数字
            this.countText.style.cssText = 'font-size: 14px; margin: 0 auto;';
            this.numberControDiv.appendChild(this.countText);

            // 创建函数来设置悬停颜色
            function addHoverEffect(svgElement) {
                svgElement.addEventListener('mouseover', function () {
                    var path = svgElement.querySelector('path');
                    if (path) {
                        path.setAttribute('data-original-fill', path.getAttribute('fill'));
                        path.setAttribute('fill', '#ff6200');
                    }
                });

                svgElement.addEventListener('mouseout', function () {
                    var path = svgElement.querySelector('path');
                    if (path) {
                        path.setAttribute('fill', path.getAttribute('data-original-fill'));
                    }
                });
            }

            // SVG控制区
            var svgControlDiv = document.createElement('div');
            svgControlDiv.style.cssText = 'display: flex; flex-direction: column; justify-content: space-between; cursor: pointer;';

            // 上方SVG
            var svgUp = document.createElementNS("http://www.w3.org/2000/svg", "svg");
            svgUp.setAttribute("width", "6");
            svgUp.setAttribute("height", "6");
            svgUp.setAttribute("viewBox", "0 0 1024 1024");
            svgUp.innerHTML = `
                    <path d="M854.016 739.328l-313.344-309.248-313.344 309.248q-14.336 14.336-32.768 21.504t-37.376 7.168-36.864-7.168-32.256-21.504q-29.696-28.672-29.696-68.608t29.696-68.608l376.832-373.76q14.336-14.336 34.304-22.528t40.448-9.216 39.424 5.12 31.232 20.48l382.976 379.904q28.672 28.672 28.672 68.608t-28.672 68.608q-14.336 14.336-32.768 21.504t-37.376 7.168-36.864-7.168-32.256-21.504z" fill="#8a8a8a"></path>
                `;
            addHoverEffect(svgUp);
            svgControlDiv.appendChild(svgUp);

            // 下方SVG
            var svgDown = document.createElementNS("http://www.w3.org/2000/svg", "svg");
            svgDown.setAttribute("width", "6");
            svgDown.setAttribute("height", "6");
            svgDown.setAttribute("viewBox", "0 0 1024 1024");
            svgDown.innerHTML = `
                    <path d="M857.088 224.256q28.672-28.672 69.12-28.672t69.12 28.672q29.696 28.672 29.696 68.608t-29.696 68.608l-382.976 380.928q-12.288 14.336-30.72 19.968t-38.912 4.608-40.448-8.704-34.304-22.016l-376.832-374.784q-29.696-28.672-29.696-68.608t29.696-68.608q14.336-14.336 32.256-21.504t36.864-7.168 37.376 7.168 32.768 21.504l313.344 309.248z" fill="#8a8a8a"></path>
                `;
            addHoverEffect(svgDown);
            svgControlDiv.appendChild(svgDown);

            svgUp.addEventListener('click', () => this.updateCount(1, 'countInput'));
            svgDown.addEventListener('click', () => this.updateCount(-1, 'countInput'));

            this.numberControDiv.appendChild(svgControlDiv);
            container.appendChild(this.numberControDiv);
        }

        // 用于更新数字文本的方法
        updateCount(increment, notificationType) {
            let currentValue = parseInt(this.countText.textContent);
            currentValue += increment;
            this.setCount(currentValue);

            if (notificationType === 'countInput') {
                if (this.getCount() > 0) {
                    showNotification(`下一个输入序号:${currentValue}`, 0);
                } else {
                    this.setCount(1);
                    showNotification('当前已是最小序号', 1500);
                    this.countSort_reShowNotification();
                }
            }
        }

        // 获取当前的计数
        getCount() {
            return this.countText.textContent;
        }

        // 设置当前计数
        setCount(count) {
            this.countText.textContent = count;
        }

        // 控制numberControDiv显示/隐藏的方法
        showNumberControlDiv(visible) {
            if (this.numberControDiv) {
                this.numberControDiv.style.display = visible ? 'flex' : 'none';
            }
        }

        // 常显通知方法
        countSort_reShowNotification() {
            var show_time = 1500;
            if (countSort_notificationTimeout) {
                clearTimeout(countSort_notificationTimeout);
            }
            countSort_notificationTimeout = setTimeout(() => {
                if (this.getDivButtonState()) {
                    showNotification("下一个输入序号:" + this.getCount(), 0);
                }
                countSort_notificationTimeout = null;
            }, show_time); // 延迟后触发
        }

        // 重写handleSwitchClick以控制子页面的可点击性
        handleSwitchClick() {
            super.handleSwitchClick();

            const newState = this.getSwitchState();
            initializeContainerStyle(this.childSettingsContainer, newState);
        }

        // 获取子设置容器
        getChildSettingsContainer() {
            return this.childSettingsContainer;
        }

        getSelectBoxValue() {
            const selectBox = this.buttonContainer.querySelector('select');
            return selectBox ? selectBox.value : null;
        }
    }

    // 创建开关容器元素
    var switchesContainer = document.createElement('div');
    switchesContainer.classList.add('flex', 'items-center', 'justify-between', 'pb-12');
    switchesContainer.style.cssText = 'position: fixed; top: 0px; left: 50%; transform: translateX(-50%); z-index: 9999;';
    if (isHomeURL()) {
        document.body.appendChild(switchesContainer);
    }

    // 封装函数返回SVG图标
    function createSVGIcon() {
        var svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
        svgIcon.setAttribute('class', 'icon custom-svg'); // 添加自定义类名
        svgIcon.setAttribute('viewBox', '0 0 1024 1024');
        svgIcon.setAttribute('width', '20');
        svgIcon.setAttribute('height', '20');
        svgIcon.setAttribute('fill', '#bbbbbb');
        svgIcon.innerHTML = '<path d="M288.791335 65.582671l446.41733 446.417329-446.41733 446.417329z"></path>';
        svgIcon.style.cssText = 'vertical-align: middle;'; // 垂直居中样式

        return svgIcon;
    }

    // 添加事件监听用于下拉箭头
    document.addEventListener('mouseenter', function (event) {
        if (event.target instanceof Element && event.target.matches('svg.icon.custom-svg')) { // 仅匹配具有自定义类名的SVG
            event.target.setAttribute('fill', '#ff6200');
        }
    }, true);

    document.addEventListener('mouseleave', function (event) {
        if (event.target instanceof Element && event.target.matches('svg.icon.custom-svg')) { // 仅匹配具有自定义类名的SVG
            event.target.setAttribute('fill', '#bbbbbb');
        }
    }, true);

    var dropdown_style = document.createElement('style');
    dropdown_style.textContent = `
        @keyframes dropdownContentAnimation {
            0% {
                transform: translateX(-50%) translateY(14px) scale(0);
            }
            100% {
                transform: translateX(-50%) translateY(0) scale(1);
            }
        }

        @keyframes dropdownContentExitAnimation {
            0% {
                transform: translateX(-50%) translateY(0) scale(1);
            }
            100% {
                transform: translateX(-50%) translateY(14px) scale(0);
            }
        }

        #dropdownContent.show {
            animation: dropdownContentAnimation 0.3s ease-out;
        }

        #dropdownContent.hide {
            animation: dropdownContentExitAnimation 0.3s ease-out;
        }

        @keyframes dropdownButtonAnimation {
            0% { transform: scale(1); }
            35% { transform: scale(1.15, 1.3); }
            85% { transform: scale(0.94); }
            100% { transform: scale(1); }
        }

        #dropdownButtonAnimate.animate {
            animation: dropdownButtonAnimation 0.45s ease-out;
        }
    `;

    document.head.appendChild(dropdown_style);

    var dropdownContainer = document.createElement('div');
    dropdownContainer.style.cssText = 'position: relative; display: inline-block;';

    var dropdownButtonName = '更多功能';

    var dropdownButton = document.createElement('button');
    dropdownButton.id = 'dropdownButtonAnimate'; // 添加 id 以便于应用动画
    dropdownButton.textContent = dropdownButtonName;
    dropdownButton.style.cssText = 'margin-top: 0px; padding: 0px 10px; cursor: pointer; border-radius: 999px;z-index: 3;';
    dropdownButton.classList.add('ant-btn', 'css-9fw9up', 'ant-btn-default', 'primaryButton___N3z1x');

    var dropdownDeck = document.createElement('div');
    dropdownDeck.style.cssText = `
        top: 0;
        left: 0;
        width: 200px;
        height: 60px;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
    `;

    var dropdownContent = document.createElement('div');
    dropdownContent.id = 'dropdownContent';
    dropdownContent.style.cssText = `
        display: none;
        position: absolute;
        background-color: #f9f9f9;
        top: 0px;
        left: 50%;
        transform: translateX(-50%);
        min-width: 200px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
        border-radius: 16px;
        max-height: 360px;
        overflow-y: auto; /* 使用 auto 可以根据内容是否溢出显示滚动条 */
        scrollbar-width: none; /* 隐藏滚动条,适用于 Firefox */
        -ms-overflow-style: none; /* 隐藏滚动条,适用于 IE 和 Edge */
        transform-origin: top center;
    `;

    // 获取内容的显示状态
    function getDropdownContentDisplayState() {
        return dropdownContent.style.display !== 'none'; // 返回 true 表示可见,false 表示不可见
    }

    var dropdownMask = document.createElement('div');
    dropdownMask.style.cssText = `
        position: sticky;
        top: 0;
        left: 0;
        width: 200px;
        height: 60px;
        backdrop-filter: none;
        border-radius: 10px 10px 0 0;
        box-sizing: border-box;
        border-bottom: 0px solid #DDD;
        z-index: 99; /* 确保遮罩在内容上方 */
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgba(249, 249, 249, 0);
    `;

    dropdownDeck.appendChild(dropdownButton);
    dropdownContainer.appendChild(dropdownDeck);
    dropdownContent.appendChild(dropdownMask);
    
    dropdownContainer.appendChild(dropdownContent);
    switchesContainer.appendChild(dropdownContainer);

    function on_dropdownMask() {
        dropdownMask.style.borderBottom = '1px solid #DDD';
        dropdownMask.style.backdropFilter = 'blur(8px) brightness(90%)';
        dropdownMask.style.backgroundColor = 'rgba(249, 249, 249, 0.5)';
    }

    function off_dropdownMask() {
        dropdownMask.style.borderBottom = '0px solid #DDD';
        dropdownMask.style.backdropFilter = 'none';
        dropdownMask.style.backgroundColor = 'rgba(249, 249, 249, 0)';
    }

    dropdownButton.addEventListener('click', function () {
        // 强制重新开始动画
        this.classList.remove('animate');

        // 使用 requestAnimationFrame 确保动画类已被移除
        requestAnimationFrame(() => {
            this.classList.add('animate');
        });

        this.addEventListener('animationend', function () {
            this.classList.remove('animate');
        }, { once: true });

        if (dropdownContent.style.display === 'none' || dropdownContent.classList.contains('hide')) {
            dropdownContent.style.display = 'block';
            pureMode_infoDisplay(pureSwitch.getSwitchState());
            dropdownContent.classList.remove('hide');
            dropdownContent.classList.add('show');
            if (dropdownContent.scrollTop > 0) {
                on_dropdownMask();
            }
        } else {
            dropdownContent.classList.remove('show');
            dropdownContent.classList.add('hide');
            dropdownContent.addEventListener('animationend', function onAnimationEnd() {
                if (dropdownContent.classList.contains('hide')) {
                    dropdownContent.style.display = 'none';
                    pureMode_infoDisplay(pureSwitch.getSwitchState());
                    off_dropdownMask();
                }
                dropdownContent.removeEventListener('animationend', onAnimationEnd);
            });
        }
    });

    dropdownContent.addEventListener('scroll', function () {
        if (dropdownContent.scrollTop > 0 && dropdownContent.style.display !== 'none') {
            on_dropdownMask();
        }
        else {
            off_dropdownMask();
        }
    });

    window.addEventListener('click', function (event) {
        if (!dropdownContainer.contains(event.target)) {
            if (dropdownContent.style.display === 'block') {
                dropdownContent.classList.remove('show');
                dropdownContent.classList.add('hide');

                // 强制重新开始动画
                dropdownButton.classList.remove('animate');

                // 使用 requestAnimationFrame 确保动画类已被移除
                requestAnimationFrame(() => {
                    dropdownButton.classList.add('animate');
                });

                dropdownButton.addEventListener('animationend', function () {
                    this.classList.remove('animate');
                }, { once: true });

                dropdownContent.addEventListener('animationend', function onAnimationEnd() {
                    if (dropdownContent.classList.contains('hide')) {
                        dropdownContent.style.display = 'none';
                        off_dropdownMask();
                        pureMode_infoDisplay(pureSwitch.getSwitchState());
                    }
                    dropdownContent.removeEventListener('animationend', onAnimationEnd);
                });
            }
        }
    });

    // 版本描述区域
    var versionTitle = document.createElement('p');
    versionTitle.textContent = versionTitleTxt;
    versionTitle.style.cssText = `
        font-size: 12px;
        line-height: 1.8; /* 调整行高 */
        margin: 0 20px;
        justify-content: center; /* 使用 flex 居中对齐 */
        align-items: center;
        border-top: 1px solid #D2D2D2;
        text-align: center; /* 居中文本 */
        color: #9B9B9B;
        display: flex; /* 添加 display: flex 以使用 flexbox 布局 */
        cursor: pointer; /* 鼠标指针 */
        user-select: none;
    `;

    var isExpanded_versionTitle = false; // 用于跟踪当前状态

    // 悬浮时显示“展开所有菜单”或“折叠所有菜单”
    versionTitle.addEventListener('mouseover', function () {
        versionTitle.textContent = isExpanded_versionTitle ? '折叠所有菜单' : '展开所有菜单';
    });

    // 鼠标移开时恢复原始文本
    versionTitle.addEventListener('mouseout', function () {
        versionTitle.textContent = versionTitleTxt;
    });

    // 点击事件处理
    versionTitle.addEventListener('click', function () {
        // 执行点击操作,展开或折叠所有菜单
        document.querySelectorAll('#main_standardFunShowButton').forEach(button => button.click());

        // 切换状态并更新悬浮文本
        isExpanded_versionTitle = !isExpanded_versionTitle;
        versionTitle.textContent = versionTitleTxt; // 恢复原始文本
    });

    // 更新本地存储的开关状态
    var updateSwitchState = function (switchName, newState) {
        localStorage.setItem(switchName, newState.toString());
    };


    // 通用子菜单容器样式的函数
    function initializeContainerStyle(container, state) {
        if (!state) {
            container.style.opacity = '0.4';// 设置透明度使内容变浅
            container.style.pointerEvents = 'none';// 禁止点击操作
        } else {
            container.style.opacity = '';// 恢复透明度
            container.style.pointerEvents = '';// 恢复点击操作
        }
    }

    var notification_style = document.createElement('style');
    notification_style.type = 'text/css';
    notification_style.innerHTML = `
            @keyframes showNotification {
                0% {
                    transform: translateX(-50%) scale(0);
                }
                40% {
                    transform: translateX(-50%) scale(.96);
                }
                55% {
                    transform: translateX(-50%) scale(1.04);
                }
                100% {
                    transform: translateX(-50%) scale(1);
                }
            }

            @keyframes hideNotification {
                5% {
                    transform: translateX(-50%) scale(1);
                }
                100% {                
                    opacity: 0;
                    transform: translateX(-50%) scale(0.2);
                }
            }

            @keyframes notificationButtonAnimation {
                0% { transform: translateX(-50%) scale(1); }
                100% { transform: translateX(-50%) scale(1.15); opacity: 0;}
            }

            .notification {
                cursor: default;
                position: fixed;
                bottom: 60px; 
                left: 50%;
                background-color: rgba(0, 0, 0, 0.5);
                color: #fff;
                padding: 10px;
                border-radius: 12px;
                display: none;
                z-index: 9999;
                backdrop-filter: blur(10px) brightness(90%); /* 添加模糊效果 */
                -webkit-backdrop-filter: blur(10px); /* 兼容Safari浏览器 */
                transform-origin: center;
                width: auto; /* 默认宽度 */
                max-width: 68%;
                white-space: nowrap; /* 单行显示 */
                overflow: hidden; /* 超出内容隐藏 */
                text-overflow: ellipsis; /* 溢出省略号 */
                text-align: center; /* 文本居中显示 */
                transform: translateX(-50%); /* 初始水平居中 */
            }
            `;
    document.head.appendChild(notification_style);

    // 创建通知弹窗
    var NotificationContainer = document.createElement('div');
    NotificationContainer.classList.add('notification');
    NotificationContainer.id = 'showNotificationContainer';
    document.body.appendChild(NotificationContainer);

    // 将开关和按钮加入页面
    // 添加纯净模式开关控件
    const pureSwitch = new ToggleButtonComponent('pureSwitch', '纯净模式', dropdownContent, 1);
    // 添加复制开关控件
    const old_copySwitchContainer = new ToggleButtonComponent('copySwitch', '点击店名复制', dropdownContent);
    // 添加提取商品链接控件
    const newonlyItemIdButtonContainer = new ToggleButtonComponent('newmainOnlyItemIdSwitchState', '链接自动提取', dropdownContent);
    // 添加手卡截图控件
    const tableCardPngSwitchContainer = new ToggleButtonComponent('tableCardPngSwitchState', '快捷截图', dropdownContent,);
    // 添加商品排序开关控件
    const itemSortButtonContainer = new ToggleButtonComponent('mainItemSortButtonSwitchState', '商品排序', dropdownContent);
    // 添加着色与计算器开关控件
    const drawColorAndCalculatorContainer = new ToggleButtonComponent('mainDrawColorAndCalculatorContainer', '着色与计算器(Beta)', dropdownContent);
    // 添加商品排序开关控件
    const AuxiliaryFunctionsContainer = new ToggleButtonComponent('AuxiliaryFunctionsContainer', '辅助功能', dropdownContent);
    dropdownContent.appendChild(versionTitle);

    // 复制开关子功能区域
    const main_copySwitch = old_copySwitchContainer.getSecondaryContent();
    const copySwitchContainer = new SonToggleButtonComponent('copySwitchContainer', '点击店名复制', main_copySwitch, "点击店铺名称时,自动复制店铺链接到剪切板", 1);
    const copySwitch_onlyPart = new SonToggleButtonComponent('copySwitch_onlyPart', '部分复制触发方式', copySwitchContainer.getChildSettingsContainer(), "设置复制部分内容的触发方式", 1);
    const copySwitch_onlyPart_clickCount = ['单击', '双击'];
    copySwitch_onlyPart.createSelectBox(copySwitch_onlyPart_clickCount);
    const copySwitch_smartCpoy = new SonToggleButtonComponent('copySwitch_smartCpoy', '复制长度', copySwitchContainer.getChildSettingsContainer(), "设置复制内容的长度", 1);
    const copySwitch_smartCpoy_length = ['默认', '智能'];
    copySwitch_smartCpoy.createSelectBox(copySwitch_smartCpoy_length);

    // 提取链接子功能区域
    const main_onlyItemIdSwitch = newonlyItemIdButtonContainer.getSecondaryContent();
    const sonMain_onlyItemIdSwitch = new SonToggleButtonComponent('mainOnlyItemIdSwitchState', '链接自动提取', main_onlyItemIdSwitch, "当点击商品链接窗口时,会主动提取当前剪切板中的商品链接,并更新你的剪切板", 1);
    const mainOnlyItemId_autoSave = new SonToggleButtonComponent('mainOnlyItemId_autoSave_state', '自动保存', sonMain_onlyItemIdSwitch.getChildSettingsContainer(), '替换链接后自动点击“保存”按钮', 1);
    const mainOnlyItemId_checkTtemLink = new SonToggleButtonComponent('mainOnlyItemId_checkTtemLink_state', '链接检查', sonMain_onlyItemIdSwitch.getChildSettingsContainer(), '链接中不存在12位ID时,使用“警告”替换当前剪切板\n\n开启此功能会引起剪切板冲突', 1);

    // 快捷截图子功能区域
    const main_tableCardPngSwitch = tableCardPngSwitchContainer.getSecondaryContent();
    const sonMain_tableCardPngSwitch = new SonToggleButtonComponent('main_tableCardPngSwitch', '手卡截图', main_tableCardPngSwitch, "截取当前页面的手卡并添加到剪切板", 1);
    const tableCardPng_resolution = new SonToggleButtonComponent('mainTableCardPng_resolution', '清晰度', sonMain_tableCardPngSwitch.getChildSettingsContainer(), "设置截图的清晰度\n\n注意:更低的清晰度并不能提高截图的响应速度", 1);
    const tableCardPng_resolution_select = ['普通', '高清', '超清'];
    tableCardPng_resolution.createSelectBox(tableCardPng_resolution_select);
    const tableCardPng_checkItemIdRow = new SonToggleButtonComponent('tableCardPng_checkItemIdRow', '上播ID核对', sonMain_tableCardPngSwitch.getChildSettingsContainer(), "截图时在手卡中添一行“上播ID”,以便核对是否正确", 1);
    const tableCardPng_checkItemIdRow_onlyPng = new SonToggleButtonComponent('tableCardPng_checkItemIdRow_onlyPng', '仅截图时显示', tableCardPng_checkItemIdRow.getChildSettingsContainer(), "开启后仅在截图时会含有“上播id核对行”", 1);


    // 自动填充子功能区域
    const main_itemSort = itemSortButtonContainer.getSecondaryContent();
    const sonMain_itemSortSwitch = new SonToggleButtonComponent('main_itemSort_SwitchState_1', '自动填充', main_itemSort, "点击排序时自动自动清空或输入", 1);
    const itemSort_inputConter = new SonToggleButtonComponent('itemSort_inputConter', '输入内容', sonMain_itemSortSwitch.getChildSettingsContainer(), "设置自动输入的默认内容", 1);
    const itemSort_inputConter_input = ['空', '0', '1', '2', '3', '999', '2536'];
    itemSort_inputConter.createSelectBox(itemSort_inputConter_input);
    const itemSort_countInputFun = new SonToggleButtonComponent('itemSort_countInputFun', '计数输入', sonMain_itemSortSwitch.getChildSettingsContainer(), "将“输入内容”更新为当前计数,以方便排序", 1);
    itemSort_countInputFun.createNumberControDiv(10, false, 'countInput');
    itemSort_countInputFun.createDivButton('开始', '退出',
        (isActivated) => {
            if (isActivated) {
                itemSort_countInputFun.showNumberControlDiv(true);  // 显示数字控制区
                itemSort_countInputFun.setCount(1);
                itemSort_countInputFun.countSort_reShowNotification();// 计数通知显示恢复
                itemSort_inputConter.showSwitchDiv(false);  // 隐藏输入内容选择框
                temp_itemId = '';
            } else {
                itemSort_countInputFun.showNumberControlDiv(false);  // 隐藏数字控制区
                itemSort_inputConter.showSwitchDiv(true);  // 显示输入内容选择框
            }
        }
    );
    itemSort_countInputFun.showNumberControlDiv(false);
    const itemSort_anchorSort = new SonToggleButtonComponent('itemSort_anchorSort', '主播排序', sonMain_itemSortSwitch.getChildSettingsContainer(), "根据输入内容,按照当前商品顺序,进行主播排序", 1);
    itemSort_anchorSort.createDivButton('开始', '退出',
        (isActivated) => {
            if (isActivated) {
                if(itemSort_countInputFun.getDivButtonState()){
                    const button = document.getElementById('itemSort_countInputFun_divButton');
                    button.click();

                    itemSort_countInputFun.showNumberControlDiv(false);  // 隐藏数字控制区
                    itemSort_inputConter.showSwitchDiv(true);  // 显示输入内容选择框
                }
                processItems();
            }
            else {
                // 检查是否已有弹窗存在,如果有则移除
                const existingModal = document.querySelector('.dropdown-modal');
                if (existingModal) {
                    existingModal.style.display = 'none';
                    dropdownButton.style.display = ''; // 恢复按钮
                }
            }
        }
    );

    // 着色与计算器子功能区域
    const main_drawColorAndCalculator = drawColorAndCalculatorContainer.getSecondaryContent();
    const sonMain_drawColor = new SonToggleButtonComponent('main_drawColor', '着色器', main_drawColorAndCalculator, "点击着色按钮后对当前手卡进行着色\n\n注意:着色器功能目前处于测试阶段,可能存在一些问题", 1);
    const sonMain_drawColor_skuDesc = new SonToggleButtonComponent('main_drawColor_skuDesc', '规格栏', sonMain_drawColor.getChildSettingsContainer(), "启用对当前手卡的SKU规格栏着色", 1);
    const sonMain_drawColor_livePrice = new SonToggleButtonComponent('main_drawColor_livePrice', '直播间到手价栏', sonMain_drawColor.getChildSettingsContainer(), "启用对当前手卡的直播间到手价栏着色", 1);
    const sonMain_drawColor_liveGameplay = new SonToggleButtonComponent('main_drawColor_liveGameplay', '优惠方式栏', sonMain_drawColor.getChildSettingsContainer(), "启用对当前手卡的优惠方式栏着色", 1);
    const sonMain_drawColor_preSetInventory = new SonToggleButtonComponent('main_drawColor_preSetInventory', '预设库存栏', sonMain_drawColor.getChildSettingsContainer(), "启用对当前手卡的预设库存栏着色", 1);
    const sonMain_drawColor_inventory = new SonToggleButtonComponent('main_drawColor_inventory', '库存栏', sonMain_drawColor.getChildSettingsContainer(), "启用对当前手卡的库存栏着色", 1);
    // 智能替换
    const sonMain_smartReplace = new SonToggleButtonComponent('main_smartReplace', '智能替换', sonMain_drawColor.getChildSettingsContainer(), "1.智能识别手卡中的文字并替换为对应格式\n2.替换违禁词汇\n\n例如:将“99/2箱*6瓶”转写为“平均到手”的形式;\n将“49/2小箱/6瓶”转写为“平均到手”的形式\n例如:将“100%棉”替换为“99.99%棉”", 1);
    // 计算器开关
    const sonMain_calculator = new SonToggleButtonComponent('main_calculator', '计算器', sonMain_drawColor.getChildSettingsContainer(), "点击着色按钮后对当前手卡的算式进行计算\n\n注意:计算器功能目前处于测试阶段,可能存在一些问题;\n\n例如:无法计算形如“(3+9)/2”的算式;如果你想正确计算形如这样的算式,请将算式调整为“(3+9)/(2)”", 1);

    // 辅助功能子功能区域
    const AuxiliaryFunctions_container = AuxiliaryFunctionsContainer.getSecondaryContent();
    const sAF_backToLastCurrentURL = new SonToggleButtonComponent('sAF_backToLastCurrentURL', '最近浏览', AuxiliaryFunctions_container, "会主动提示上次浏览的场次页面,点击弹出的通知会跳转到上次浏览的场次页面", 1, true);
    const sAF_closeBrowserUpdateDiv = new SonToggleButtonComponent('sAF_closeBrowserUpdateDiv', '关闭浏览器更新提示', AuxiliaryFunctions_container, "关闭浏览器更新提示,防止影响使用", 1, true);
    const sAF_pastedSeachSwitchConta = new SonToggleButtonComponent('sAF_pastedSeachSwitchConta', '快捷粘贴搜索', AuxiliaryFunctions_container, "进行粘贴操作后,自动点击搜索按钮", 1, false);
    const sAF_AdvanceTableCardTitle = new SonToggleButtonComponent('sAF_AdvanceTableCardTitle', '手卡标题优化', AuxiliaryFunctions_container, "优化手卡标题,使其更易于识别", 1, true);
    const sAF_AdvanceBatchPrint = new SonToggleButtonComponent('sAF_AdvanceBatchPrint', '手卡打印功能优化', AuxiliaryFunctions_container, "优化打印页面默认文件名,允许仅打印手卡的部分区域并且添加“上播ID核对”区域", 1, true);
    const sAF_AdvanceBatchPrint_alwaysDisplay = new SonToggleButtonComponent('sAF_AdvanceBatchPrint_alwaysDisplay', '总是显示ID核对行', sAF_AdvanceBatchPrint.getChildSettingsContainer(), "总是显示ID核对行,不论是否仅打印手卡的部分区域", 1, true);
    const sAF_FristPhotoCopyForTmallAndTaobao = new SonToggleButtonComponent('sAF_FristPhotoCopyForTmallAndTaobao', '天猫&淘宝主图复制', AuxiliaryFunctions_container, "在天猫或淘宝的主图上点击“复制图片”按钮,以快速将图片拷贝到剪切板\n\n注意:此功能会需要多个网页的剪切板访问权限", 1, true);

    // 单击和双击事件的延迟和计数器
    var delay = 200; // 延迟时间,单位毫秒
    var timer = null;
    
    // 监听鼠标左键点击事件
    document.addEventListener('click', function (event) {
        // 检查是否开启了点击复制功能且在匹配的网址上
        if (copySwitchContainer.getSwitchState() && isHomeURL()) {
            clearTimeout(timer);
    
            timer = setTimeout(function () {
                handleMouseClick(event, event.detail === 2);
            }, delay);
        }
    });
    
    // 鼠标点击响应事件
    function handleMouseClick(event, isDoubleClick = false) {
        if (event.target && 
            event.target.classList.contains('link-overflow-tip') && 
            event.target.closest('#MarkHighlight-shopDetail-outShopName')) {
            var text = event.target.textContent;

            // 根据是否处于双击模式及点击类型,决定复制的文本内容
            if (toggleDoubleClickMode()) {
                if(isDoubleClick) {
                    if(toggleSmartCopyLength()) {
                        copiedText = processText(text);
                    } else {
                        copiedText = text.substring(0, 3);
                    }
                } else{
                    copiedText = text.substring();
                }

            } else {
                if(isDoubleClick) {
                    copiedText = text.substring();
                } else{
                    if(toggleSmartCopyLength()) {
                        copiedText = processText(text);
                    } else {
                        copiedText = text.substring(0, 3);
                    }
                }
            }

            GM_setClipboard(copiedText);
            showNotification("复制成功:" + copiedText);
            itemSort_countInputFun.countSort_reShowNotification(); // 计数通知显示恢复
        }
    }

    // 返回单击和双击模式的切换函数
    function toggleDoubleClickMode() {
        const isDoubleClickMode = copySwitch_onlyPart.getSelectBoxValue();

        switch (isDoubleClickMode) {
            case '单击':
                return false;
            case '双击':
                return true;
            default:
                return false;
        }
    }

    // 返回复制长度的切换函数
    function toggleSmartCopyLength() {
        const smartCopyLength = copySwitch_smartCpoy.getSelectBoxValue();

        switch (smartCopyLength) {
            case '默认':    
                return false;
            case '智能':
                return true;
            default:
                return false;
        }
    }

    // 生成过滤词汇的正则表达式
    var chineseFilterWords = ["旗舰店", "旗舰", "商品", "海外", "官方", "食品", "化妆品", "生活馆",
        "专营店", "国旅", "数码", "医药", "专卖店", "饮品", "女装", "男装", "企业店", "童装",
        "中国", "集团"];
    var chineseFilterRegex = new RegExp(chineseFilterWords.join('|'), 'g');  // 合并成一个正则表达式

    // 检查文本开头并根据要求处理
    function processText(text) {
        var englishPattern = /^[A-Za-z\s.,!?;:'"-]+/;  // 匹配以英文开头的部分,包含常见符号

        // 检查是否以英文开头
        var match = text.match(englishPattern);
        if (match) {
            // 如果以英文开头,返回匹配的英文部分
            return match[0];
        } else {
            // 如果以中文开头,使用正则表达式过滤指定词汇
            return text.replace(chineseFilterRegex, '');
        }
    }

    // 添加鼠标悬浮和移出事件监听器
    NotificationContainer.addEventListener('mouseenter', function () {
        clearTimeout(notificationTimer); // 悬浮时清除计时器
        // console.log('Mouse entered notification'); // 调试日志
    });
    NotificationContainer.addEventListener('mouseleave', function () {
        // console.log('Mouse left notification'); // 调试日志
        var time = 3000;
        if (itemSort_countInputFun.getDivButtonState()) time = 0;
        resetTimer(time); // 移出时重置计时器
    });

    function showNotification(message, duringTime = 3000, showImage = false) {
        // 清除之前的计时器
        if (notificationTimer) {
            clearTimeout(notificationTimer);
        }

        // 重置隐藏标志
        isHiding = false;

        // 重置通知样式
        NotificationContainer.innerHTML = '';
        NotificationContainer.style.width = 'auto';
        NotificationContainer.style.transform = 'translateX(-50%)';
        NotificationContainer.style.animation = 'none';
        NotificationContainer.style.padding = '10px';

        // 短暂移除并重新添加通知元素,强制触发动画
        document.body.removeChild(NotificationContainer);
        setTimeout(() => {
            document.body.appendChild(NotificationContainer);

            // 设置通知文本内容
            NotificationContainer.innerHTML = message;

            // 如果指定了显示图片,则读取剪贴板中的图片并显示
            if (showImage) {
                NotificationContainer.style.padding = '5px';
                navigator.clipboard.read().then(async function (data) {
                    for (const item of data) {
                        for (const type of item.types) {
                            if (type.startsWith('image/')) {
                                const blob = await item.getType(type);
                                const imageURL = URL.createObjectURL(blob);

                                const imageElement = document.createElement('img');
                                imageElement.src = imageURL;
                                imageElement.style.width = '300px';
                                imageElement.style.borderRadius = '8px';

                                const imageContainer = document.createElement('div');
                                imageContainer.style.paddingTop = '10px';
                                imageElement.style.maxWidth = 'auto';
                                imageContainer.style.borderRadius = '8px';
                                imageContainer.style.margin = 'auto';
                                imageContainer.style.display = 'block';
                                imageContainer.appendChild(imageElement);

                                NotificationContainer.appendChild(imageContainer);

                                // 图片加载完成后调整位置并设置消失定时器
                                imageElement.onload = function () {
                                    NotificationContainer.style.left = '50%';

                                    NotificationContainer.style.display = 'block';
                                    NotificationContainer.style.animation = 'showNotification 0.5s forwards';
                                    // 设置消失动画计时器
                                    resetTimer(duringTime);
                                };

                                break;
                            }
                        }
                    }
                }).catch(function (error) {
                    console.error('Error reading clipboard:', error);
                });
            } else {
                // 显示通知
                NotificationContainer.style.display = 'block';
                NotificationContainer.style.animation = 'showNotification 0.5s forwards';

                // 设置消失动画计时器
                resetTimer(duringTime);
            }
        }, 50); // 确保通知元素短暂移除再添加
    }

    function hideNotification() {
        if (isHiding) return;
        isHiding = true;

        NotificationContainer.style.animation = 'hideNotification 0.5s forwards';

        // 在动画结束后隐藏元素
        notificationTimer = setTimeout(function () {
            NotificationContainer.style.display = 'none';
            isHiding = false;
        }, 500);
    }

    function resetTimer(duringTime = 3000) {
        if (notificationTimer) {
            clearTimeout(notificationTimer);
            // console.log("清除计时器");
        }
        if (duringTime > 0) {
            notificationTimer = setTimeout(function () {
                hideNotification();
                // console.log("设置计时器");
            }, duringTime); // 3秒后自动隐藏通知
        }
    }

    async function clickButton(pastedData = true, delayTime = 0, containerSelector = document, buttonSelector, buttonText = '') {
        // 判断粘贴内容是否为空或为 true
        if (pastedData === true || (typeof pastedData === 'string' && pastedData.trim().length > 0)) {
            // 查找指定容器内的按钮
            var container = containerSelector === document ? document : document.querySelector(containerSelector);
            if (container) {
                // 如果提供了 buttonText,则进一步检查按钮文本内容
                var buttons = container.querySelectorAll(buttonSelector);
                var button = null;
                if (buttonText) {
                    buttons.forEach(btn => {
                        var span = btn.querySelector('span');
                        if (span && span.textContent.trim() === buttonText) {
                            button = btn;
                        }
                    });
                } else {
                    button = buttons[0]; // 如果没有提供 buttonText,默认选择第一个匹配的按钮
                }

                // 如果找到符合条件的按钮,延迟点击
                if (button) {
                    setTimeout(function () {
                        button.click();
                        // console.log(`已点击文本内容为“${buttonText || '按钮'}”的按钮`);
                    }, delayTime);
                } else {
                    console.log(`未找到符合条件的按钮`);
                }
            }
        }
    }

    // 监听粘贴事件
    document.addEventListener('paste', function (event) {
        // 判断是否开启了自动点击功能且在匹配的网址上
        if (sAF_pastedSeachSwitchConta.getSwitchState() && isHomeURL()) {
            // 获取粘贴板中的内容
            var pastedData = (event.clipboardData);
            var className = '.ant-btn.css-9fw9up.ant-btn-primary';
            clickButton(pastedData, 100, undefined, className);
        }
    });

    // 检查文本中是否是天猫链接函数
    function checkForTmallInClipboard(text) {
        const regex = /https:\/\/[^ ]*tmall[^ ]*id=\d{12}/;
        return regex.test(text);
    }

    function checkForChaoshiInClipboard(text) {
        const regex = /https:\/\/[^ ]*chaoshi[^ ]*id=\d{12}/;
        return regex.test(text);
    }

    function checkForFeizhuInClipboard(text) {
        const regex = /https:\/\/[^ ]*fliggy[^ ]*id=\d{12}/;
        return regex.test(text);
    }

    // 提取链接id函数
    function extractIdFromClipboard(text) {
        // 检查是否只含有11-12位数字
        // const onlyDigitsMatch = text.match(/^\d{11,12}$/);
        const onlyDigitsMatch = text.match(/^\d{12}$/);
        if (onlyDigitsMatch) {
            return text; // 如果剪切板仅包含11位或12位数字,直接返回
        }
    
        // 匹配 id= 后面的11或12位数字
        // const idMatch = text.match(/id=(\d{11,12})/);
        const idMatch = text.match(/id=(\d{12})/);
        return idMatch ? idMatch[1] : null;
    }
    

    // 粘贴功能
    async function simulatePaste(targetElement, clearBeforePaste = true) {
        try {
            // 从剪贴板读取文本
            let clipboardText = await navigator.clipboard.readText();

            // 检查目标元素是否为可编辑元素
            if (targetElement.isContentEditable || targetElement.tagName === 'INPUT' || targetElement.tagName === 'TEXTAREA') {
                // 如果clearBeforePaste为true,清空目标元素的内容
                if (clearBeforePaste) {
                    if (targetElement.isContentEditable) {
                        targetElement.innerHTML = '';
                    } else {
                        targetElement.value = '';
                    }
                }

                // 插入剪贴板内容到目标元素
                if (document.execCommand('insertText', false, clipboardText)) {
                    // console.log('粘贴成功:' + clipboardText);
                } else {
                    targetElement.value += clipboardText;
                }
            } else {
                // alert('目标元素不可编辑');
            }
        } catch (err) {
            console.error('读取剪贴板内容失败:', err);
            showNotification("读取剪贴板内容失败");
        }
    }

    function updateClipboardContent() {
        var tmail = "https://detail.tmall.com/item.htm?id=";
        var chaoshi = "https://chaoshi.detail.tmall.com/item.htm?id=";
        var taobao = "https://item.taobao.com/item.htm?id=";
        var feizhu = "https://traveldetail.fliggy.com/item.htm?id=";
        navigator.clipboard.readText().then((clipText) => {
            const isTmall = checkForTmallInClipboard(clipText);
            const isChaoshi = checkForChaoshiInClipboard(clipText);
            const isFeizhu = checkForFeizhuInClipboard(clipText);
            const itemId = extractIdFromClipboard(clipText);
            // console.log("itemId:" + itemId);
            // console.log("itemId-length:" + itemId.length);

            if (itemId) {
                var newUrl;
                if ((isTmall || isFeizhu) && !isChaoshi) {
                    // 转换为天猫链接
                    newUrl = tmail + itemId;
                }
                else if (isChaoshi) {
                    // 转换为猫超链接
                    newUrl = chaoshi + itemId;
                } else {
                    if (true) {
                        // 转换为淘宝链接
                        newUrl = taobao + itemId;
                    } else {
                        // 转换为天猫链接
                        newUrl = tmail + itemId;
                    }
                }
                GM_setClipboard(newUrl);
                showNotification("剪贴板内容已更新为:" + newUrl);
                //console.log('剪贴板内容已更新为:' + newUrl);
            } else {
                if (mainOnlyItemId_checkTtemLink.getSwitchState()) {
                    // 防止错误粘贴功能
                    GM_setClipboard("12位数字ID不全");
                }
                showNotification("剪贴板中没有找到12位数字ID");
                // console.log('剪贴板中没有找到12位数字ID');
            }
        }).catch((err) => {
            console.error('读取剪贴板失败:', err);
        });
    }

    // 监听鼠标左键点击事件
    document.addEventListener('click', function (event) {
        // 提取商品链接粘贴功能区域
        if (sonMain_onlyItemIdSwitch.getSwitchState() && isHomeURL()) {
            if (event.target.closest('.ant-form-item.liveLinkFormItem___RPAQZ.css-9fw9up.ant-form-item-has-success')) {
                if (event.target.classList.contains('ant-input-affix-wrapper') ||
                    event.target.classList.contains('ant-input-affix-wrapper-status-error') ||
                    event.target.classList.contains('css-9fw9up')) {
                    // console.log('目标元素被点击');
                    updateClipboardContent();
                    simulatePaste(document.activeElement);

                    // 点击保存按钮
                    if (mainOnlyItemId_autoSave.getSwitchState()) {
                        var fbutton = '.ant-drawer-footer';
                        var buttonName = '.ant-btn.css-9fw9up.ant-btn-primary';
                        showNotification("粘贴成功&保存成功");
                        clickButton(undefined, 500, fbutton, buttonName);
                    }
                }
            }
        }
        // 自动序号标1功能
        if (sonMain_itemSortSwitch.getSwitchState()) {
            if (event.target.closest('.ant-input-number.ant-input-number-sm.css-1ayq15k')) {
                if (event.target.classList.contains('ant-input-number-input')) {
                    // console.log('目标元素被点击');
                    if (!itemSort_countInputFun.getDivButtonState()) {
                        GM_setClipboard(itemSort_inputConter.getSelectBoxValue());
                    } else {
                        GM_setClipboard(itemSort_countInputFun.getCount() - 1);
                    }
                    simulatePaste(document.activeElement);
                }
            }
        }
        // 自动激活排序输入框
        if (sonMain_itemSortSwitch.getSwitchState()) {
            if (event.target.classList.contains('sortWeights___Kn8mn') ||
                event.target.closest('.sortWeights___Kn8mn')) {
                // console.log('找到sortWeights___Kn8mn');
                activateInputFieldAndSave(event);
            }
        }
    });

    /* 快捷截图功能区 */
    // 返回渲染倍数
    function selectedTableCardPngResolution() {
        const resolution = tableCardPng_resolution.getSelectBoxValue();
        // console.log("设定的分辨率:" + resolution);
        switch (resolution) {
            case '普通':
                return 1.5;
            case '高清':
                return 2.5;
            case '超清':
                return 4;
            default:
                return 2.5;
        }
    }

    function loadImageIcon() {
        return '<span class="ant-btn-icon ant-btn-loading-icon" style=""><span role="img" aria-label="loading" class="anticon anticon-loading anticon-spin"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="loading" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"></path></svg></span></span>';
    }

    if (sonMain_tableCardPngSwitch.getSwitchState() && isTableCardURL()) {
        // Load html2canvas library
        function loadHtml2Canvas(callback) {
            var script = document.createElement('script');
            script.src = 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js';
            script.onload = callback;
            document.head.appendChild(script);
        }

        function createCaptureScreenshotButton() {
            const targetClass = '[class*="ant-space"][class*="css-9fw9up"][class*="ant-space-horizontal"][class*="ant-space-align-center"]';

            const captureScreenshot_observer = new MutationObserver((mutationsList, observer) => {
                for (let mutation of mutationsList) {
                    if (mutation.type === 'childList') {
                        const targetElement = document.querySelector(targetClass);
                        if (targetElement) {
                            if (document.querySelector('.captureScreenshot')) {
                                captureScreenshot_observer.disconnect();
                                return;
                            }

                            var captureScreenshot = document.createElement('div');
                            captureScreenshot.classList.add('ant-space-item');

                            var captureScreenshotButton = document.createElement('button');
                            captureScreenshotButton.textContent = '快捷截图';
                            captureScreenshotButton.classList.add('ant-btn', 'css-9fw9up', 'ant-btn-default', 'captureScreenshot');
                            captureScreenshot.appendChild(captureScreenshotButton);

                            targetElement.insertBefore(captureScreenshot, targetElement.firstChild);

                            captureScreenshotButton.addEventListener('click', captureScreenshotFunction);

                            captureScreenshot_observer.disconnect();
                            break;
                        }
                    }
                }
            });

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

        function loadImageAsDataURL(url) {
            return new Promise((resolve, reject) => {
                GM_xmlhttpRequest({
                    method: 'GET',
                    url: url,
                    responseType: 'blob',
                    onload: function (response) {
                        const blob = response.response;
                        const reader = new FileReader();
                        reader.onloadend = function () {
                            resolve(reader.result);
                        };
                        reader.onerror = function () {
                            reject(new Error('Failed to load image'));
                        };
                        reader.readAsDataURL(blob);
                    },
                    onerror: function () {
                        reject(new Error('Network error'));
                    }
                });
            });
        }

        async function captureScreenshotFunction() {
            const tableElement = document.querySelector('table');
            var displayScale = selectedTableCardPngResolution();
            // console.log("设定的倍率:" + displayScale);

            showNotification("截图中  " + loadImageIcon(), 0);
            if (tableElement) {
                const rows = tableElement.querySelectorAll('tr');

                if (rows.length >= 3) {
                    rows[2].cells[0].textContent = '';

                    // 隐藏除第二行和第三行外的所有行
                    rows.forEach((row, index) => {
                        if (index !== 1 && index !== 2) {
                            if (index === 3 && isShowTableCheckedRow()) {
                                row.style.display = '';
                            } else {
                                row.style.display = 'none';
                            }
                        }
                    });

                    const imgElement = rows[2].cells[2].querySelector('img');
                    if (imgElement) {
                        try {
                            const dataUrl = await loadImageAsDataURL(imgElement.src);
                            imgElement.src = dataUrl;

                            setTimeout(() => {
                                // 使用 html2canvas 捕获截图
                                html2canvas(tableElement, { scale: displayScale }).then(canvas => {
                                    // 恢复所有行的显示状态
                                    rows.forEach(row => {
                                        row.style.display = '';
                                    });

                                    // 隐藏检查格式行
                                    if (true) {
                                        rows.forEach((row, index) => {
                                            if (index === 3) {
                                                row.style.display = isCheckItemIdRow_onlyPng();
                                            }
                                        });
                                    }

                                    canvas.toBlob(async function (blob) {
                                        try {
                                            await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
                                            console.log("%cTable Screenshot:", "color: #9147ff", "Screenshot copied to clipboard.");
                                            showNotification("截图已成功复制到剪贴板", undefined, true);
                                        } catch (error) {
                                            console.log("%cTable Screenshot: Screenshot failed to copy to clipboard!", "color: #ff8080");
                                            showNotification("截图失败!");
                                        }
                                    });
                                });
                            }, 200); // 延迟 200 毫秒等待图片加载完毕
                        } catch (error) {
                            console.log('Image load error:', error);
                        }
                    } else {
                        console.log('Image element not found');
                    }
                } else {
                    console.log("Table does not have enough rows");
                }
            } else {
                console.log("Table element not found");
            }
        }

        loadHtml2Canvas(createCaptureScreenshotButton);
    }

    /*
    **************************
    自动激活排序窗口、点击保存
    **************************
    */
    function activateInputFieldAndSave(event) {
        if (true) {
            const click_itemId = handleCellClick(event);
            if (temp_itemId != click_itemId) {
                if (itemSort_countInputFun.getDivButtonState()) {
                    itemSort_countInputFun.updateCount(1, 'countInput');
                }
                temp_itemId = click_itemId;
            }
            // countText.textContent = countNum_Sort + 1;
            // console.log('计数:'+countNum_Sort);
            const popover = document.querySelector('.ant-popover:not(.ant-popover-hidden)');
            const inputField = popover.querySelector('.ant-input-number-input');
            if (inputField) {
                inputField.focus();
                inputField.click();
            } else {
                console.log('未找到输入字段');
            }
        }
    }

    // 查找点击的id
    function handleCellClick(event) {
        // 查找最接近的包含行元素的类
        let rowElement = event.target.closest('.ag-row');

        if (rowElement) {
            // 获取row-index属性
            let rowIndex = rowElement.getAttribute('row-index');
            // console.log('找到的行索引:', rowIndex);

            // 使用row-index属性查找行内的span标签
            let targetSpan = document.querySelector(`.ag-row[row-index="${rowIndex}"] span#MarkHighlight-upLiveId-upLiveId`);

            if (targetSpan) {
                return targetSpan.textContent;
                // 打印span的文本内容
                // console.log('目标span的文本内容:', targetSpan.textContent);
            } else {
                // console.log(`在行索引为${rowIndex}的行中未找到id为"MarkHighlight-upLiveId-upLiveId"的span标签。`);
            }
        } else {
            // console.log('未找到点击单元格对应的行。');
        }
    }

    /*
    =================
    打印标题优化
    =================
    */
    function titlePrint_extractData() {
        const dateElement = document.querySelector('.isSelect___qbUI1 .ant-space.css-9fw9up.ant-space-horizontal.ant-space-align-center.title___mA8xY .ant-space-item:nth-child(2) div');
        const sessionElement = document.querySelector('.truncate.sessionName___HUMKC.font-ma-semibold');

        if (dateElement && sessionElement) {
            const dateText = dateElement.textContent.trim();
            const sessionText = sessionElement.textContent.trim();

            GM_setValue('titlePrint_extractedDate', dateText);
            GM_setValue('titlePrint_extractedSession', sessionText);

            // console.log('Date extracted and stored:', dateText);
            // console.log('Session name extracted and stored:', sessionText);
        }
    }

    /*
    =================
    手卡标题优化
    =================
    */
    // 网址id提取
    function url_getSessionGoodsId() {
        const url = window.location.href;
        const match = url.match(/sessionGoodsId=(\d+)/);
        return match ? match[1] : null;
    }

    function modifyTableCardURL_title(itemId) {
        // console.log('itemId:', itemId);
        if (sAF_AdvanceTableCardTitle.getSwitchState()) {
            if (itemId && isTableCardURL()) {
                document.title = '【手卡 ' + itemId[0].toString().slice(-4) + '】' + itemId[1];
            }
        }

    }

    /*
    手卡附带检查链接功能
    */

    function isShowTableCheckedRow() {
        return tableCardPng_checkItemIdRow.getSwitchState();
    }

    function isCheckItemIdRow_onlyPng() {
        if(tableCardPng_checkItemIdRow_onlyPng.getSwitchState()) {
            return 'none';
        } else {
            return '';
        }
    }

    // 检查行生成函数
    function createTableCheckedRow(itemId, width1 = 0, width2 = 0) {
        // 创建 <tr> 元素
        const tr = document.createElement('tr');
        tr.id = 'checkedItemLink_TableCrad';
        tr.style.cssText = 'min-height: 30px; max-height: 30px;';
        tr.style.display = isCheckItemIdRow_onlyPng();

        // 创建第一个 <td> 元素
        const td1 = document.createElement('td');
        td1.colSpan = '3';
        td1.style.cssText = 'padding: 0px; height: 30px; align-items: center; justify-content: center; display: flex;';
        if (width1 > 0) td1.style.width = width1 + 'px';

        const marioSvg = createMarioSVGIconWrapper('svgCheckedRow', false, 16);

        // 创建第一个 <div> 元素
        const div1 = document.createElement('div');
        div1.style.cssText = 'display: flex; align-items: center; justify-content: center; height: 100%;';

        // 创建第一个 <span> 元素
        const span1 = document.createElement('span');
        span1.style.cssText = 'color: rgb(236, 40, 39); font-size: 16px; text-align: center; margin-left: 1px;';
        span1.textContent = '上播商品ID核对:';

        // 创建第二个 <span> 元素,放置链接内容
        const span2 = document.createElement('span');
        span2.id = 'cureentItem_upLiveId';
        span2.style.cssText = 'color: rgb(51, 51, 51); font-weight: bold;';
        span2.textContent = itemId;

        // 将 span2 放入 span1,然后放入 div1
        span1.appendChild(span2);
        div1.appendChild(marioSvg);
        div1.appendChild(span1);

        // 将 div1 放入 td1
        td1.appendChild(div1);

        // 创建第二个 <td> 元素
        const td2 = document.createElement('td');
        td2.colSpan = '7';
        td2.style.cssText = 'padding: 0px; align-items: center; justify-content: center; height: 30px;';
        if (width2 > 0) td2.style.width = width2 + 'px';

        // 创建第二个 <div> 元素
        const div2 = document.createElement('div');
        div2.style.cssText = 'display: flex; align-items: center; justify-content: center; height: 100%;';

        // 创建第三个 <span> 元素
        const span3 = document.createElement('span');
        span3.style.cssText = 'color: rgb(51, 51, 51); font-size: 16px; text-align: center;white-space: pre-wrap;';
        span3.textContent = '请回复有误、无误、正确之类的,不要收到、1之类的不明确信息,要让我们知道你核对完了';
        // 使用 innerHTML 设置带有部分上色的内容
        span3.innerHTML = `核对完所有栏目后,请回复<span style="color: rgb(13, 193, 97); font-weight: bold;">有误、无误、正确</span>之类的,不要<span style="color: rgb(236, 40, 39); font-weight: bold;">收到、1</span>之类的不明确信息,要让我们知道你核对完了`;

        // 将 span3 放入 div2,然后放入 td2
        div2.appendChild(span3);
        td2.appendChild(div2);

        // 将两个 td 放入 tr
        tr.appendChild(td1);
        tr.appendChild(td2);

        // 返回生成的 <tr> 元素
        return tr;
    }

    // 用于更新插入的行中的 id
    function changeNewTrItemId(itemId) {
        const idSpan = document.getElementById('cureentItem_upLiveId');
        if (idSpan) {
            idSpan.textContent = itemId;
        }
    }

    function insertCheckedRow(itemId) {
        if (!document.getElementById('checkedItemLink_TableCrad')) {
            const tableElement = document.querySelector('table');

            if (tableElement) {
                const rows = tableElement.querySelectorAll('tr');

                // 确保表格中至少有三个 <tr> 元素
                if (rows.length >= 3) {
                    // 获取第三个 <tr> 元素
                    const thirdRow = rows[2];

                    // 生成新的 <tr> 元素
                    const newRow = createTableCheckedRow(itemId);

                    // 在第三个 <tr> 元素的后面插入新行
                    thirdRow.parentNode.insertBefore(newRow, thirdRow.nextSibling);
                } else {
                    // console.log("表格中没有足够的行来插入新行。");
                }
            } else {
                // console.log("没有找到表格");
            }
        } else {
            changeNewTrItemId(upLiveIdArray[0]);
        }
    }

    /*
    用于打印页面的插入XHR数据获取
    */
    let upLiveIdArray = [];

    // 封装提取 upLiveId 的函数
    // 打印页面数据获取
    function extractUpLiveIdsFromResponse(responseText) {
        try {
            const response = JSON.parse(responseText);
            if (response.data && Array.isArray(response.data)) {
                upLiveIdArray = response.data.map(item => item.upLiveId).filter(id => id !== undefined);
            } else {
                console.error("响应中未找到有效的 data 字段");
            }
        } catch (error) {
            console.error("解析响应失败:", error);
        }
    }

    // 手卡页面数据获取
    function extractUpLiveIdFromTableCardResponse(responseText) {
        try {
            const response = JSON.parse(responseText);

            // 初始化 upLiveIdArray 数组
            upLiveIdArray = [];

            // 检查响应数据中的 data 字段
            if (response.data && typeof response.data === 'object') {
                // 提取 data 对象中的 upLiveId
                const upLiveId = response.data.upLiveId;
                if (upLiveId !== undefined) {
                    // 将 upLiveId 存储到数组中
                    upLiveIdArray[0] = upLiveId;
                } else {
                    // console.error("响应的 data 对象中未找到 upLiveId 字段");
                }
                // 解析 cardUseDataStyle 字符串为数组
                if (response.data.cardUseDataStyle) {
                    const cardUseDataStyle = JSON.parse(response.data.cardUseDataStyle);

                    // 在 cardUseDataStyle 数组中查找 useFieldName 为 'shopName' 的对象
                    const shopData = cardUseDataStyle.find(item => item.useFieldName === 'shopName');
                    if (shopData && shopData.useFieldValue) {
                        upLiveIdArray[1] = shopData.useFieldValue; // 存储 shopName 到数组的第 1 位置
                    } else {
                        // console.error("未找到 useFieldName 为 'shopName' 的对象或 useFieldValue 为空");
                    }
                } else {
                    // console.error("响应中未找到有效的 cardUseDataStyle 字符串");
                }
                modifyTableCardURL_title(upLiveIdArray);
            } else {
                // console.error("响应中未找到有效的 data 字段,或 data 不是对象");
            }
        } catch (error) {
            // console.error("解析响应失败:", error);
        }
    }

    // 手卡页面切换数据获取
    function extractUpLiveIdsFromNextTableCardResponse(responseText) {
        try {
            const response = JSON.parse(responseText);

            // 初始化 upLiveIdArray 数组
            upLiveIdArray = [];

            // 检查响应数据中的 data 字段
            if (response.data && typeof response.data === 'object') {
                // 提取 data 对象中的 upLiveId
                const upLiveId = response.data.linkHandCardDetailRespDTO.upLiveId;
                if (upLiveId !== undefined) {
                    // 将 upLiveId 存储到数组中
                    upLiveIdArray[0] = upLiveId;
                } else {
                    // console.error("响应的 data 对象中未找到 upLiveId 字段");
                }
                // 解析 cardUseDataStyle 字符串为数组
                if (response.data.linkHandCardDetailRespDTO.cardUseDataStyle) {
                    const cardUseDataStyle = JSON.parse(response.data.linkHandCardDetailRespDTO.cardUseDataStyle);

                    // 在 cardUseDataStyle 数组中查找 useFieldName 为 'shopName' 的对象
                    const shopData = cardUseDataStyle.find(item => item.useFieldName === 'shopName');
                    if (shopData && shopData.useFieldValue) {
                        upLiveIdArray[1] = shopData.useFieldValue; // 存储 shopName 到数组的第 1 位置
                    } else {
                        // console.error("未找到 useFieldName 为 'shopName' 的对象或 useFieldValue 为空");
                    }
                } else {
                    // console.error("响应中未找到有效的 cardUseDataStyle 字符串");
                }
                modifyTableCardURL_title(upLiveIdArray);
            } else {
                // console.error("响应中未找到有效的 data 字段,或 data 不是对象");
            }
        } catch (error) {
            // console.error("解析响应失败:", error);
        }
    }

    // 封装 XMLHttpRequest 的 open 方法重写
    function overrideOpenMethod() {
        const originalOpen = XMLHttpRequest.prototype.open;

        XMLHttpRequest.prototype.open = function (method, url) {
            this._method = method;
            this._url = url;
            originalOpen.apply(this, arguments);
        };
    }

    // 封装 XMLHttpRequest 的 send 方法重写
    function overrideSendMethod() {
        const originalSend = XMLHttpRequest.prototype.send;

        XMLHttpRequest.prototype.send = function (data) {
            const originalOnReadyStateChange = this.onreadystatechange;

            this.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                    if (this._method === "POST" && /https:\/\/gw\.linkmcn\.com\/live\/live\/card\/batchDetail.*/.test(this._url)) {
                        // console.log("匹配的POST请求URL:", this._url);

                        // 调用封装的函数来提取 upLiveId
                        extractUpLiveIdsFromResponse(this.responseText);
                        // console.log("提取的 upLiveId:", upLiveIdArray);
                    }
                    if (this._method === "POST" && /https:\/\/gw\.linkmcn\.com\/live\/live\/card\/detail.*/.test(this._url)) {
                        // console.log("匹配的POST请求URL:", this._url);

                        // 调用封装的函数来提取 upLiveId
                        extractUpLiveIdFromTableCardResponse(this.responseText);
                        // console.log("提取手卡的 upLiveId:", upLiveIdArray);
                    }
                    if (this._method === "POST" && /https:\/\/gw\.linkmcn\.com\/live\/live\/card\/nextLiveHandCard.*/.test(this._url)) {
                        // console.log("匹配的POST请求URL:", this._url);

                        // 调用封装的函数来提取 upLiveId
                        extractUpLiveIdsFromNextTableCardResponse(this.responseText);
                        // console.log("提取手卡切换的 upLiveId:", upLiveIdArray);
                    }
                }

                if (originalOnReadyStateChange) {
                    originalOnReadyStateChange.apply(this, arguments);
                }
            };

            originalSend.apply(this, arguments);
        };
    }

    // 判断是否是打印手卡页面,并重写 XMLHttpRequest 的方法
    if (isBatchPrintURL() || isTableCardURL()) {
        overrideOpenMethod();
        overrideSendMethod();
    }

    /*
    系统功能优化
    */
    // 创建一个MutationObserver实例
    // 绑定点击事件的外部函数
    function bindClickEventToNotification(url) {
        // console.log('绑定点击事件, 传入URL:' + url);
        const element = document.getElementById('showNotificationContainer');
        element.style.cursor = 'pointer';

        if (element) {
            // 定义点击事件处理函数
            function handleClick() {
                isHiding = true;
                NotificationContainer.style.animation = 'notificationButtonAnimation 0.5s forwards';

                window.location.href = url;

                setTimeout(function () {
                    NotificationContainer.style.display = 'none';
                    isHiding = false;
                    window.location.reload();
                }, 500);

                // 移除点击事件
                element.removeEventListener('click', handleClick);
                element.style.cursor = 'default';
            }

            // 绑定点击事件
            element.addEventListener('click', handleClick);
            setTimeout(function () {
                // 移除点击事件
                element.removeEventListener('click', handleClick);
                element.style.cursor = 'default';

                hideNotification();
            }, 5000);
        }
    }

    // 设置打印手卡的部分区域
    function setPrintTableCardArea(isActivated) {
        var isShow = isActivated ? 'flex' : 'none';
        var isInsertedShow = isActivated ? 'none' : 'flex'; // 与 isShow 相反的显示结果
        if(sAF_AdvanceBatchPrint_alwaysDisplay.getSwitchState()) isInsertedShow = 'flex'; // 强制显示上播ID核对行

        // 获取所有的table元素
        var tables = document.querySelectorAll('table');

        tables.forEach(function (table, i) {
            // 获取每个table中的tbody
            var tbody = table.querySelector('tbody');

            // 获取tbody中的所有tr行
            var rows = tbody.querySelectorAll('tr');

            // 遍历tr行并隐藏第三行之后的所有行
            rows.forEach(function (row, index) {
                if (index >= 3) { // 从第四行开始(索引为3),设置display为none
                    row.style.display = isShow;
                }
            });

            // 检查是否已经插入过具有相同id的tr
            var existingRow = tbody.querySelector('#checkedItemLink_TableCrad');
            if (existingRow) {
                existingRow.style.display = isInsertedShow; // 如果存在则更新显示状态
            } else {
                console.warn("不存在“上播ID核对”行,请检查是否已插入过该行。");
            }
        });
    }

    function insertCheckedRow_forBatchPrint(isActivated) {
        var isInsertedShow = isActivated ? 'flex' : 'none';

        // 获取所有的table元素
        var tables = document.querySelectorAll('table');

        tables.forEach(function (table, i) {
            // 获取每个table中的tbody
            var tbody = table.querySelector('tbody');

            // 获取tbody中的所有tr行
            var rows = tbody.querySelectorAll('tr');

            // 检查是否已经插入过具有相同id的tr
            var existingRow = tbody.querySelector('#checkedItemLink_TableCrad');
            if (!existingRow) {
                var newRow = createTableCheckedRow(upLiveIdArray[i], 276.8492, 878.8229); // 创建新行
                rows[2].insertAdjacentElement('afterend', newRow); // 插入新行到第三个 <tr> 元素之后
                newRow.style.display = isInsertedShow; // 设置新行的显示状态
            }
        });
    }

    // 判断是否是剧透时间
    function checkIfTrailer(storedDate) {
        const currentTime = new Date();
        let isTrailer = '';

        // 获取当前年份
        const currentYear = currentTime.getFullYear();

        // 将字符串日期转换为Date对象,加入当前年份
        let adjustedDate = new Date(`${currentYear}-${storedDate}T18:00:00`);

        // 将日期往前调整一天
        adjustedDate.setDate(adjustedDate.getDate() - 1);

        // 计算两个日期之间的差值(以天为单位)
        const timeDifference = Math.abs(currentTime - adjustedDate);
        const dayDifference = timeDifference / (1000 * 60 * 60 * 24);

        // 如果天数差值大于180天,考虑跨年情况,给adjustedDate加上1年
        if (dayDifference > 180) {
            adjustedDate.setFullYear(adjustedDate.getFullYear() + 1);
        }

        const printCard_switch = document.getElementById('button_setPrintTableCardArea_switch');
        const button_setPrintTableCardArea = document.getElementById('button_setPrintTableCardArea')

        if (button_setPrintTableCardArea && printCard_switch.getAttribute('aria-checked') === 'true') {
            isTrailer = "核对";
        } else {
            // 比较当前时间与截止时间
            if (currentTime < adjustedDate) {
                isTrailer = "剧透";
            } else {
                isTrailer = '';
            }
        }

        return isTrailer;
    }

    // 纯净模式函数
    function pureMode(isActivated) {
        var isShow = isActivated ? 'none' : '';
        var targetHeight = document.documentElement.scrollHeight - 238;

        // link帮助菜单
        const link_helpMenu = document.querySelector('.link-customer-service-only-help');
        // 直播场次日期切换菜单
        const liveDay_selectMenu = document.querySelector('.flex.justify-between.pb-12');
        // 直播场次总览
        const liveOverview_table = document.querySelector('.link-session-board-pai___eddtw').parentElement;

        const otherElements = document.querySelector('.ag-body-horizontal-scroll-viewport');

        // 设置元素的显示状态
        link_helpMenu.style.display = isShow;
        liveDay_selectMenu.style.display = isShow;
        liveOverview_table.style.display = isShow;
        // otherElements.style.display = isShow;

        // margin优化
        const margin_value = isActivated ? '0px' : '12px';
        const mainElement = document.querySelector('main');

        mainElement.style.marginTop = margin_value;
        mainElement.style.marginBottom = margin_value;

        // 商品操作框
        // 外部框
        const goodsOperation_div = document.querySelector('.ant-spin-container');
        // 内部表格
        const goodsOperation_table = document.getElementById('ag-grid-react-container');

        // 设置商品操作框的高度
        goodsOperation_div.style.height = isShow === 'none' ? targetHeight + 'px' : '944px';
        goodsOperation_table.style.height = isShow === 'none' ? targetHeight + 'px' : '548px';
    }

    // pureMode 场次信息辅助显示
    function pureMode_infoDisplay(isActivated) {
        // 场次信息数据
        const storedDate = GM_getValue('titlePrint_extractedDate', '');
        const storedSession = GM_getValue('titlePrint_extractedSession', '');

        if (isActivated && !getDropdownContentDisplayState()) {
            dropdownButton.textContent = storedDate + ' ' + storedSession;
        } else {
            dropdownButton.textContent = dropdownButtonName;
        }
    }

    const sys_auxiliaryFunctions = new MutationObserver((mutationsList) => {
        let urlChanged = false;

        // 场次信息数据
        const storedDate = GM_getValue('titlePrint_extractedDate', '');
        const storedSession = GM_getValue('titlePrint_extractedSession', '');

        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach((node) => {
                    // 系统通知位置优化
                    if (node.nodeType === 1 && node.classList.contains('ant-message') && node.classList.contains('ant-message-top') && node.classList.contains('css-190m0jy')) {
                        // 修改top值为64px
                        node.style.top = '64px';
                    }

                    if (sAF_closeBrowserUpdateDiv.getSwitchState()) {
                        // 关闭浏览器更新弹窗
                        if (node.nodeType === 1 && node.id === 'link-browser-update-global') {
                            const closeButton = node.querySelector('.link-browser-update-global-close');
                            if (closeButton) {
                                closeButton.click();
                                showNotification('已关闭浏览器更新弹窗', 1500);
                                // console.log('关闭了浏览器更新弹窗');
                            } else {
                                console.log('未找到关闭按钮');
                            }
                        }
                    }
                });

                // 检查URL是否变化
                if (isHomeURL()) {
                    pureMode(pureSwitch.getSwitchState());
                    pureMode_infoDisplay(pureSwitch.getSwitchState());

                    const currentURL = window.location.href; // 获取当前网址
                    titlePrint_extractData(); // 提取日期和场次信息

                    // 只有在 URL 发生变化时,才会执行以下代码
                    if (currentURL !== lastCurrentURL) {
                        // 在保存新的 currentURL 之前,更新 lastCurrentURL
                        const previousURL = lastCurrentURL;

                        // 将当前网址保存到 localStorage
                        localStorage.setItem('lastCurrentURL', currentURL);

                        if (sAF_backToLastCurrentURL.getSwitchState()) {
                            // 显示通知并绑定点击事件,传入 previousURL 而不是 currentURL
                            showNotification('上次浏览的页面:' + storedDate + ' ' + storedSession, 5000);

                            setTimeout(() => {
                                itemSort_countInputFun.countSort_reShowNotification();// 计数通知显示恢复
                            }, 4000);

                            setTimeout(() => {
                                bindClickEventToNotification(previousURL); // 绑定点击事件,传入 previousURL
                            }, 50);
                        }

                        // 更新 lastCurrentURL 为新的网址
                        lastCurrentURL = currentURL;
                        urlChanged = true;
                    }

                    GM_setValue('sAF_FristPhotoCopyForTmallAndTaobao', sAF_FristPhotoCopyForTmallAndTaobao.getSwitchState());
                }

                if (isTableCardURL()) {
                    // 调用函数添加SVG图标
                    addSVGToSpecificTDs();
                    // 一键着色按键显示
                    displayDrawColorButton();
                    if (tableCardPng_checkItemIdRow.getSwitchState()) {
                        // 插入检查行
                        insertCheckedRow(url_getSessionGoodsId());
                    }
                }

                if (isTmallItemURL()) {
                    const isCreateButton = GM_getValue('sAF_FristPhotoCopyForTmallAndTaobao', false);

                    if (isCreateButton) {
                        // 创建按钮
                        createGetTmallPngButton();
                    }
                }

                if (sAF_AdvanceBatchPrint.getSwitchState() && isBatchPrintURL() && storedDate && storedSession) {
                    // 批量打印标题优化
                    document.title = `${storedDate} ${storedSession}手卡${checkIfTrailer(storedDate)}`;

                    // 插入上播ID核对行
                    insertCheckedRow_forBatchPrint(sAF_AdvanceBatchPrint_alwaysDisplay.getSwitchState());

                    setTimeout(function () {
                        // 查找目标元素,即将新div插入到这个元素中
                        var targetElement = document.querySelector('.flex.justify-end');

                        // 检查是否已经存在id为button_setPrintTableCardArea的div,避免重复添加
                        if (!document.getElementById('button_setPrintTableCardArea')) {
                            // 创建一个新的div元素
                            var newDiv = document.createElement('div');
                            newDiv.id = 'button_setPrintTableCardArea';
                            newDiv.style.display = 'flex';
                            newDiv.style.justifyContent = 'space-between';
                            newDiv.style.alignItems = 'center';
                            newDiv.style.marginRight = '10px';

                            // 创建左侧的文本节点
                            var textNode = document.createElement('span');
                            textNode.textContent = '不打印红字和卖点区域';
                            textNode.style.fontSize = '14px';
                            textNode.style.marginRight = '10px';

                            // 创建右侧的开关按钮
                            var switchButton = document.createElement('button');
                            switchButton.type = 'button';
                            switchButton.id = 'button_setPrintTableCardArea_switch';
                            switchButton.setAttribute('role', 'switch');
                            switchButton.setAttribute('aria-checked', 'false');  // 默认未开启
                            switchButton.className = 'ant-switch ant-switch-small css-175k68i';

                            // 创建开关按钮内部的div(用于手柄)
                            var handleDiv = document.createElement('div');
                            handleDiv.className = 'ant-switch-handle';

                            // 将手柄div添加到按钮中
                            switchButton.appendChild(handleDiv);

                            // 添加点击事件,切换开关状态
                            switchButton.addEventListener('click', function () {
                                var isChecked = switchButton.getAttribute('aria-checked') === 'true';
                                if (isChecked) {
                                    // 如果是开启状态,关闭它
                                    switchButton.setAttribute('aria-checked', 'false');
                                    switchButton.classList.remove('ant-switch-checked');
                                    setPrintTableCardArea(true);
                                } else {
                                    // 如果是关闭状态,开启它
                                    switchButton.setAttribute('aria-checked', 'true');
                                    switchButton.classList.add('ant-switch-checked');
                                    setPrintTableCardArea(false);
                                }
                            });

                            // 将文本节点和开关按钮添加到div中
                            newDiv.appendChild(textNode);
                            newDiv.appendChild(switchButton);

                            // 将新创建的div添加到目标元素中
                            targetElement.appendChild(newDiv);
                        } else {
                            // console.log('开关已经存在,跳过创建。');
                        }
                    }, 1000); // 延迟1秒执行
                }
            }
        }

        // 处理URL变化
        if (urlChanged) {
            // 检查是否存在switchesContainer
            if (!document.getElementById('switchesContainer')) {
                if (isHomeURL()) {
                    document.body.appendChild(switchesContainer);
                }
            }
        }
    });

    // 观察目标节点的子节点添加和移除
    sys_auxiliaryFunctions.observe(document.body, { childList: true, subtree: true });

    /*
    一键着色
    */
    function createDrawColorButton() {
        const targetClass = '[class*="ant-space"][class*="css-9fw9up"][class*="ant-space-horizontal"][class*="ant-space-align-center"]';

        const drawColor_observer = new MutationObserver((mutationsList, observer) => {
            for (let mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    const targetElement = document.querySelector(targetClass);
                    if (targetElement) {
                        if (document.querySelector('.drawColor')) {
                            drawColor_observer.disconnect();
                            return;
                        }

                        var drawColor = document.createElement('div');
                        drawColor.classList.add('ant-space-item');

                        var drawColorButton = document.createElement('button');
                        drawColorButton.textContent = '一键着色';
                        drawColorButton.style.display = 'none';
                        drawColorButton.classList.add('ant-btn', 'css-9fw9up', 'ant-btn-default', 'drawColor');
                        drawColor.appendChild(drawColorButton);

                        targetElement.insertBefore(drawColor, targetElement.firstChild);

                        drawColorButton.addEventListener('click', startDrawColor);

                        drawColor_observer.disconnect();
                        break;
                    }
                }
            }
        });

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

    // 在页面上创建按钮
    createDrawColorButton();

    function displayDrawColorButton() {
        // 获取所有 class 为 'ant-btn-primary' 的按钮
        const buttons = document.querySelectorAll('.ant-btn-primary');
        const drawColorButtons = document.querySelector('.drawColor');

        // 遍历所有按钮
        for (const button of buttons) {
            // 找到按钮内部的 span 标签
            const span = button.querySelector('span');

            // 如果 span 标签存在,检查文本内容是否包含“编辑手卡”
            if (span && span.textContent.includes('编辑手卡') || !sonMain_drawColor.getSwitchState()) {
                drawColorButtons.style.display = 'none';
                // 更新 divWrapper 的显示状态
                updateDivWrapperDisplay(false);
                return true;
            }
        }

        // 如果没有找到包含“编辑手卡”的文本,返回 false
        drawColorButtons.style.display = 'inline-block';
        // 更新 divWrapper 的显示状态
        updateDivWrapperDisplay(true);
        return false;
    }

    //着色器执行函数
    function startDrawColor() {
        if (sonMain_drawColor.getSwitchState()) {
            if (sonMain_drawColor_skuDesc.getSwitchState()) {
                activateIframeAndModifyStyles('skuDesc');
            }
            if (sonMain_drawColor_livePrice.getSwitchState()) {
                activateIframeAndModifyStyles('livePrice');
            }
            if (sonMain_drawColor_liveGameplay.getSwitchState()) {
                activateIframeAndModifyStyles('liveGameplay');
            }
            if (sonMain_drawColor_preSetInventory.getSwitchState()) {
                activateIframeAndModifyStyles('preSetInventory');
            }
            if (sonMain_drawColor_inventory.getSwitchState()) {
                activateIframeAndModifyStyles('inventory');
            }
        }

        // 通知
        showNotification('着色成功', 5000);
    }

    /*字体颜色库*/
    const colorLibrary = {
        black: 'rgb(51, 51, 51)',
        gray: 'rgb(173, 173, 173)',
        red: 'rgb(236, 40, 39)',
        blue: 'rgb(0, 145, 255)',
        green: 'rgb(13, 193, 97)',
        orange: 'rgb(243, 141, 15)',
        yellow: 'rgb(228, 228, 50)',
        cyan: 'rgb(25, 229, 221)',
        purple: 'rgb(180, 95, 224)',
        pink: 'rgb(241, 66, 125)',
        bluePurple: 'rgb(106, 101, 227)',
        grassGreen: 'rgb(157, 189, 78)',
        skyBlue: 'rgb(79, 191, 227)',
        brown: 'rgb(161, 90, 28)',
    };

    // 替换内容词库
    const replacementMap = {
        "100%": "99.99%",
        "纯棉": "99.99%棉",
    };

    // 定义不同激活元素的颜色映射
    var colorMaps = {
        'skuDesc': [
            { regex: /([1-9]?[0-9])?个(sku|SKU|颜色|尺码|尺寸|组合|口味|规格|色号|款式|版型)/, color: colorLibrary.red },
            { regex: /.*总(价值|售价)(([\d\*\+\-\=\s\./]+)?[wW+]?)元?/, color: colorLibrary.bluePurple },
            { regex: /正装.*((([\d\*\+\-\=\s\./]+)?[wW+]?)元?)?.*价值(([\d\*\+\-\=\s\./]+)?[wW+]?)元?|日常售价(([\d\*\+\-\=\s\./]+)?[wW+]?)元?/, color: colorLibrary.skyBlue },
            { regex: /尺寸(:)?|重量(:)?|克重(:)?|长度(:)?|承重(:)?|容量(:)?|(?:适用|食用|服用|建议|用法)(?:人群|方法|说明|建议|用法|用量):?|链长:|产地:|.*((近|正|超)圆|(微|无)瑕|强光).*/, color: colorLibrary.blue },
            { regex: /规格:.*/, color: colorLibrary.blue },
            { regex: /.*医嘱.*|.*糖尿病.*|.*过敏.*|.*禁止.*|.*勿食.*|.*慎食.*|.*不(宜|要).*|.*监护.*|.*敏感.*|.*不建议.*|.*慎用.*|.*停止.*|材质(成分|信息)?(:)?|面料成分(:)?/, color: colorLibrary.red },
            { regex: /.*膜布(材质)?:.*|.*标配:.*|.*特证.*|.*内含.*|.*(物理|化学|物化结合)防晒.*|物化结合|.*(美白|防晒)特证.*|.*皂基.*/, color: colorLibrary.purple },
            { regex: /.*(UPF|SPF|PA\+).*/i, color: colorLibrary.orange },
            { regex: /.*坏果.*赔.*|.*超.*赔.*/i, color: colorLibrary.pink },
        ],
        'livePrice': [
            // { regex: /不沟通价值/, color: colorLibrary.green },
            { regex: /.*总价值(([\d\*\+\-\=\s\./]+)?[wW+]?)元?/, color: colorLibrary.bluePurple },
            { regex: /正装.*((([\d\*\+\-\=\s\./]+)?[wW+]?)元?)?.*价值(([\d\*\+\-\=\s\./]+)?[wW+]?)元?|非卖品/, color: colorLibrary.skyBlue },
            { regex: /.*折扣力度.*/, color: colorLibrary.purple },
            { regex: /(拍|买).*(送|赠).*/, color: colorLibrary.red },
            { regex: /.*(?:可|免费|支持)试用.*|.*88vip(到手)?.*/, color: colorLibrary.orange },
            {
                regex: /.*(?:件|套|量)!!.*|.*到手.*|.*再加赠.*|第一件.*|补贴.*|.*(?:蜜蜂|商家)客服.*|.*猫超卡.*|.*确收.*|.*相当于买.*/,
                color: colorLibrary.red
            },
            { regex: /(超正装量)|(近正装量)|(正装量)|^氨基酸$|同系列/, color: colorLibrary.orange },
            { regex: /.*件(0元|1元)/, color: colorLibrary.blue },
            { regex: /.*免息|.*赠品不叠加|(不同规格)/, color: colorLibrary.brown },
            { regex: /^相当于$/, color: colorLibrary.gray },
        ],
        'inventory': {
            default: colorLibrary.black, // 默认颜色
            color1: colorLibrary.green, // 颜色1
            color2: colorLibrary.blue, // 颜色2
        },
        'preSetInventory': colorLibrary.black, // 颜色用于preSetInventory
        'liveGameplay': [
            { regex: /详情.*券|.*百(亿)?补(贴)?.*|.*专属价.*|.*直播间.*/, color: colorLibrary.red },
            { regex: /拍(一|二|三|四|五|六|七|八|九|十|十一|十二|十三|十四|十五|[1-9]?[0-9])件?/, color: colorLibrary.blue },
            { regex: /.*合计.*/, color: colorLibrary.bluePurple },
        ]
    };

    var colorMaps2 = {
        'skuDesc': [
            { regex: /价值(([\d\*\+\-\=\s\./]+)?[wW+]?)元?|售价(([\d\*\+\-\=\s\./]+)?[wW+]?)元?|不沟通价值/, color: colorLibrary.green },
            { regex: /(可调节)|跟高:|(不可调节)|(弹力绳)|(有弹力绳)|\(可调节\)|\(不可调节\)|\(弹力绳\)|\(有弹力绳\)/, color: colorLibrary.orange },
            { regex: /^氨基酸$|.*调:|^(一|二)元罐$|.*一物一签.*|.*一物一证.*/, color: colorLibrary.orange },
            { regex: /材质(成(分|份))?(:)?|面料成(分|份)(:)?/, color: colorLibrary.blue },
        ],
        'livePrice': [
            { regex: /价值(([\d\*\+\-\=\s\./]+)?[wW+]?)元?|不沟通价值/, color: colorLibrary.green },
            { regex: /同款正装|同款|正装/, color: colorLibrary.blue },
            { regex: /相当于(([\d\*\+\-\=\s\./]+)?[wW+]?)元?/, color: colorLibrary.red },
        ],
        'liveGameplay': [
            { regex: /拍立减|直接拍|.*满减.*|\+/, color: colorLibrary.black },
        ]
    };

    var colorMaps3 = {
        'skuDesc': [
            { regex: /=([+-]?\d+\.?\d*)(?!元)([a-zA-Z\u4e00-\u9fa5]+)(?=\s*($|[\(\[]))/, color: colorLibrary.purple },
        ],
        'livePrice': [
            { regex: /=([+-]?\d+\.?\d*)(?!元)([a-zA-Z\u4e00-\u9fa5]+)(?=\s*($|[\(\[]))/, color: colorLibrary.purple },
        ],
        'liveGameplay': [

        ]
    };

    // 正则表达式:匹配纯数字和带有'w'的数字
    const numericRegex = /^([0-9]*\.?[0-9]*[wW]?\+?)件?$/;
    const priceGameplayRegex = /.*:(([\d\*\+\-\=\s\./]+)[wW+]?)元?/;
    const check_skuDescFirstLine = /([1-9]?[0-9])?个(sku|SKU|颜色|尺码|尺寸|组合|口味|规格|色号|款式|版型)/;

    // 移除所有元素的 style 和 data-mce-style 属性
    function removeStyles(element) {
        if (element.nodeType === Node.ELEMENT_NODE) {
            // 如果是 <p> 标签或其他容器标签
            if (element.tagName === 'P') {
                // 找到所有 <span> 标签
                const spans = element.querySelectorAll('span');
                spans.forEach(span => {
                    // 创建一个临时的文本节点,用于插入 <span> 标签的内容
                    const textNode = document.createTextNode(span.textContent);
    
                    // 用文本节点替换 <span> 标签
                    span.parentNode.replaceChild(textNode, span);
                });
            }
    
            // 递归处理子节点
            for (let i = 0; i < element.children.length; i++) {
                removeStyles(element.children[i]);
            }
        }
    }
    

    // 根据优先级排序colorMap,长文本优先
    function sortColorMap(colorMap) {
        return colorMap.slice().sort((a, b) => b.regex.source.length - a.regex.source.length);
    }

    // 应用颜色到现有的 span 或创建新的 span
    function applyColor(span, color) {
        if (!span.getAttribute('data-mce-style')) {
            span.style.color = color;
            span.setAttribute('data-mce-style', `color: ${color};`);
        }
    }

    // 添加新样式
    // 此函数用于向iframe文档中的所有<p>元素添加新的样式。
    // 参数:
    // - iframeDocument: iframe的文档对象
    // - colorMap: 包含正则表达式和对应颜色的映射对象
    function addNewStyles(iframeDocument, colorMap) {
        const ps = iframeDocument.querySelectorAll('p');
    
        ps.forEach(p => {
            const innerHTML = p.innerHTML;
            let newInnerHTML = '';
    
            // 先按照<span>标签进行分割
            const spanParts = innerHTML.split(/(<span[^>]*>.*?<\/span>)/);
    
            spanParts.forEach(spanPart => {
                if (spanPart.match(/^<span[^>]*>.*<\/span>$/)) {
                    // 如果是<span>标签包裹的部分,直接添加到 newInnerHTML
                    newInnerHTML += spanPart;
                } else {
                    // 处理不包含<span>的部分
                    const parts = spanPart.split(/(<br>|&nbsp;)/);
    
                    parts.forEach(part => {
                        if (part.match(/(<br>|&nbsp;)/)) {
                            // 如果是<br>或&nbsp;,直接添加到 newInnerHTML
                            newInnerHTML += part;
                        } else {
                            let styledPart = part;
                            const sortedColorMap = sortColorMap(colorMap);
    
                            sortedColorMap.forEach(map => {
                                if (map.regex.test(part)) {
                                    const color = map.color;
    
                                    // 仅对不在<span>标签内的内容进行着色处理
                                    const match = map.regex.exec(part);
                                    if (match) {
                                        const span = document.createElement('span');
                                        span.innerHTML = match[0];
                                        applyColor(span, color);
                                        styledPart = part.replace(map.regex, span.outerHTML);
                                    }
                                }
                            });
    
                            newInnerHTML += styledPart;
                        }
                    });
                }
            });
    
            p.innerHTML = newInnerHTML;
        });
    }    

    function applyPrefixSuffixColor(iframeDocument) {
        const ps = iframeDocument.querySelectorAll('p');

        ps.forEach(p => {
            const innerHTML = p.innerHTML;
            let newInnerHTML = '';

            const parts = innerHTML.split(/(<br>|&nbsp;)/);

            parts.forEach(part => {
                const testApply = part.indexOf('折扣');
                if (testApply !== -1 || priceGameplayRegex.test(part.textContent)) {
                    newInnerHTML += part;
                    return; // 跳过折扣行
                }

                const colonIndex = part.indexOf(':');

                if (colonIndex !== -1) {
                    const prefix = part.substring(0, colonIndex + 1); // 包含“:”
                    const suffix = part.substring(colonIndex + 1);

                    // 创建临时 div 用于获取后缀的纯文本
                    const tempDiv = document.createElement('div');
                    tempDiv.innerHTML = suffix;
                    const plainTextSuffix = tempDiv.textContent || tempDiv.innerText || "";

                    // 检查后缀并应用颜色
                    let styledSuffix = suffix;
                    const suffixSpan = document.createElement('span');
                    suffixSpan.innerHTML = suffix;

                    if (numericRegex.test(plainTextSuffix)) {
                        applyColor(suffixSpan, colorLibrary.red);
                        styledSuffix = suffixSpan.outerHTML;
                    } else {
                        applyColor(suffixSpan, colorLibrary.gray);
                        styledSuffix = suffixSpan.outerHTML;
                    }

                    // 创建前缀 span 并应用颜色
                    const prefixSpan = document.createElement('span');
                    prefixSpan.innerHTML = prefix;
                    if (numericRegex.test(plainTextSuffix)) {
                        applyColor(prefixSpan, colorLibrary.blue);
                    } else {
                        applyColor(prefixSpan, colorLibrary.gray);
                    }

                    newInnerHTML += prefixSpan.outerHTML + styledSuffix;
                } else {
                    newInnerHTML += part;
                }
            });

            p.innerHTML = newInnerHTML;
        });
    }

    // 危险内容替换函数
    function replaceTextContent(iframeDocument, replacementMap) {
        // 获取所有的 <p> 标签
        const ps = iframeDocument.querySelectorAll('p');
    
        // 遍历每一个 <p> 标签
        ps.forEach(p => {
            let innerHTML = p.innerHTML;
            let newInnerHTML = innerHTML;
    
            // 遍历 JSON 中的每个键值对
            Object.keys(replacementMap).forEach(key => {
                const value = replacementMap[key];
                // 使用正则表达式替换所有匹配的文本
                const regex = new RegExp(key, 'g'); // 'g' 标志用于全局替换
                newInnerHTML = newInnerHTML.replace(regex, value);
            });
    
            // 将新的 HTML 内容赋给 <p> 标签
            p.innerHTML = newInnerHTML;
        });
    }    

    const activateIframeAndModifyStyles = activateElementId => {
        const activateElement = document.querySelector(`#${activateElementId} .link-node-hover-text-container`);
        if (activateElement) {
            activateElement.click();
            activateElement.focus();

            const iframe = document.querySelector(`#${activateElementId} .tox-edit-area iframe`);
            if (iframe) {
                const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
                if (iframeDocument) {
                    // 清除原有的样式
                    if (sonMain_drawColor.getSwitchState()) {
                        removeStyles(iframeDocument.body);
                    }

                    if (sonMain_calculator.getSwitchState()) {
                        findAndCalculateExpressions(iframeDocument, calculate);
                    }

                    if (sonMain_smartReplace.getSwitchState()) {
                        replaceTextContent(iframeDocument, replacementMap);
                    }

                    if (sonMain_drawColor.getSwitchState()) {
                        // 第一行红色标记
                        if (activateElementId === 'livePrice') {
                            if (sonMain_smartReplace.getSwitchState()) {
                                autoWriteAvgPrice(iframeDocument);
                            }
                            modifyFirstPTagAndSpans(iframeDocument);
                            applyPrefixSuffixColor(iframeDocument);
                        }

                        // 规格首行非sku描述行蓝色
                        if (activateElementId === 'skuDesc') {
                            skuDescFirstLineBlue(iframeDocument);
                        }

                        // 获取对应的颜色映射
                        const colorMap = colorMaps[activateElementId];
                        const colorMap2 = colorMaps2[activateElementId];
                        const colorMap3 = colorMaps3[activateElementId];
                        if (colorMap) {
                            if (activateElementId === 'inventory') {
                                handleInventoryStyles(iframeDocument);
                            } else if (activateElementId === 'preSetInventory') {
                                handlePreSetInventoryStyles(iframeDocument);
                            }
                            else {
                                addNewStyles(iframeDocument, colorMap);
                                addNewStyles(iframeDocument, colorMap2);
                                addNewStyles(iframeDocument, colorMap3);
                            }
                        } else {
                            console.error('未找到对应的颜色映射。');
                        }

                        removeDataProcessed(iframeDocument);
                        isMarioShow.push(activateElementId);
                    }
                } else {
                    console.error('无法访问iframe文档。');
                }
            } else {
                console.error('未找到iframe元素。');
            }
        } else {
            console.error('未找到激活元素。');
        }
    };

    const removeDataProcessed = doc => {
        const replaceElements = doc.querySelectorAll('[data-replace="true"]');
        replaceElements.forEach(element => {
            const parentElement = element.parentElement;
            if (parentElement.tagName.toLowerCase() === 'p') {
                // 检查 <p> 标签是否只有一个子元素,并且是当前的 <span>
                const hasOnlySpanChild = parentElement.children.length === 1 && parentElement.children[0] === element;

                // 获取父 <p> 元素的纯文本内容(不包括子元素)
                const parentText = parentElement.textContent.trim();

                if (hasOnlySpanChild && parentText === '无效内容') {
                    // 如果 <p> 标签没有其他文本内容,移除整个 <p> 标签
                    parentElement.remove();
                } else {
                    // 否则,清空 <span> 的文本内容
                    element.textContent = '';
                }
            } else {
                // 如果父元素不是 <p>,清空 <span> 的文本内容
                element.textContent = '';
            }
        });
    };


    // 规格首行非sku描述行蓝色
    const skuDescFirstLineBlue = doc => {
        const firstPTag = doc.querySelector('#tinymce p');
        if (firstPTag) {
            if (!check_skuDescFirstLine.test(firstPTag.textContent)) {
                applyColor(firstPTag, colorLibrary.blue);
            }
            const spanTags = firstPTag.querySelectorAll('span');
            spanTags.forEach(spanTag => {
                if (!check_skuDescFirstLine.test(firstPTag.textContent)) {
                    applyColor(spanTag, colorLibrary.blue);
                }
            });
        }
    };

    // 到手价数字行红色
    const modifyFirstPTagAndSpans = doc => {
        const firstPTag = doc.querySelector('#tinymce p');
        if (firstPTag) {
            if (numericRegex.test(firstPTag.textContent)) {
                applyColor(firstPTag, colorLibrary.red);
            }
            const spanTags = firstPTag.querySelectorAll('span');
            spanTags.forEach(spanTag => {
                if (numericRegex.test(spanTag.textContent)) {
                    applyColor(spanTag, colorLibrary.red);
                }
            });
        }
    };

    // 预设库存样式修改
    const handlePreSetInventoryStyles = doc => {
        function check_content(content) {
            if (!numericRegex.test(content)) {
                if (content.includes('不可控')) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        }

        const pTags = doc.querySelectorAll('#tinymce p');
        pTags.forEach(pTag => {
            if (check_content(pTag.textContent)) {
                pTag.textContent = '拉满';
                const spanTags = pTag.querySelectorAll('span');
                spanTags.forEach(spanTag => {
                    if (check_content(spanTag.textContent)) {
                        spanTag.textContent = '拉满';
                    }
                });
            }
        });
    };

    // 现货库存样式修改
    const handleInventoryStyles = doc => {
        let firstPTagFound = false;
        const pTags = doc.querySelectorAll('#tinymce p');

        pTags.forEach(pTag => {
            // 获取 <p> 标签内的所有文本内容,并将连续的 &nbsp; 转换为 <br>
            let content = pTag.innerHTML.replace(/(&nbsp;)+/g, '<span data-replace="true">无效内容</span><br>');
            // 获取 <p> 标签内的所有文本内容,并按 <br> 标签分割成数组
            const segments = content.split('<br>');

            // 处理每个分割后的段落
            segments.forEach((segment, index) => {
                // 创建临时容器元素以便于操作 HTML 字符串
                const tempContainer = document.createElement('div');
                tempContainer.innerHTML = segment;

                // 获取段落的纯文本内容
                const segmentText = tempContainer.textContent;

                if (!firstPTagFound && segmentText.includes('预售')) {
                    firstPTagFound = true;
                }

                // 创建新的 <p> 标签用于包裹分隔后的段落
                const newPTag = document.createElement('p');
                newPTag.innerHTML = segment;

                if (numericRegex.test(segmentText) || segmentText.includes('--')) {
                    applyColor(newPTag, colorMaps.inventory.default);
                } else {
                    if (firstPTagFound) {
                        applyColor(newPTag, colorMaps.inventory.color2);
                    } else {
                        applyColor(newPTag, colorMaps.inventory.color1);
                    }
                }

                // 在原 <p> 标签位置插入新的 <p> 标签
                pTag.parentNode.insertBefore(newPTag, pTag);
            });

            // 移除原始的 <p> 标签
            pTag.parentNode.removeChild(pTag);
        });
    };

    function autoWriteAvgPrice(iframeDocument) {
        const ps = iframeDocument.querySelectorAll('p');

        // 更新检测输入格式的正则表达式,支持小数
        const pattern = /^\d+(\.\d+)?(\/\d+(\.\d+)?[^\d/\*]+)([/\*]\d+(\.\d+)?[^\d/\*]+)*$/;

        ps.forEach(p => {
            if (p.querySelector('span')) {
                // 情况 1: p 标签内有 span 标签,需要处理 span 内的文本
                processSpans(p, pattern);
            } else {
                // 情况 2: 只有 p 标签,没有嵌套的 span 标签
                let newInnerHTML = '';

                // 分割HTML内容
                const parts = p.innerHTML.split(/(<br>|&nbsp;)/);

                parts.forEach(part => {
                    let styledPart = part;
                    // console.log("styledPart:" + styledPart);

                    // 检查part是否符合格式
                    if (pattern.test(part)) {
                        // 调用parseInput来解析part并生成新内容
                        const { price, num, units } = parseInput(part);
                        styledPart = generateOutput(price, num, units);
                    }

                    newInnerHTML += styledPart;
                    // console.log("newInnerHTML:" + newInnerHTML);
                });

                // 更新p元素的内容
                p.innerHTML = newInnerHTML;
            }
        });

        function processSpans(element, pattern) {
            const spans = element.querySelectorAll('span');

            spans.forEach(span => {
                const textContent = span.textContent;

                // 检查textContent是否符合格式
                if (pattern.test(textContent)) {
                    // 调用parseInput来解析textContent并生成新内容
                    const { price, num, units } = parseInput(textContent);
                    const newContent = generateOutput(price, num, units);

                    // 更新span的内容
                    span.innerHTML = newContent;
                }
            });
        }
    }

    // 定义parseInput函数,用于解析输入
    function parseInput(input) {
        const pattern = /^\d+(\.\d+)?(\/\d+(\.\d+)?[^\d/\*]+)([/\*]\d+(\.\d+)?[^\d/\*]+)*$/;

        if (!pattern.test(input)) {
            throw new Error("输入格式不正确");
        }

        const priceMatch = input.match(/^\d+(\.\d+)?/);
        const price = priceMatch ? parseFloat(priceMatch[0]) : null;

        let rex = [];
        let num = [];
        let units = [];

        const matches = input.match(/([/\*])(\d+(\.\d+)?)([^\d/\*]+)/g);
        if (matches) {
            matches.forEach((match, index) => {
                const [, symbol, number, , unit] = match.match(/([/\*])(\d+(\.\d+)?)([^\d/\*]+)/);
                rex.push(symbol);

                let quantity = parseFloat(number);
                if (symbol === "*" && index > 0) {
                    quantity *= num[num.length - 1];
                }

                num.push(quantity);
                units.push(unit.trim());
            });
        }

        return { price, rex, num, units };
    }

    // 定义generateOutput函数,用于生成输出内容
    function generateOutput(price, num, units) {
        let output = `<span style="color: rgb(236, 40, 39);" data-mce-style="color: rgb(236, 40, 39);">到手共${num[0]}${units[0]}</span><br>`;

        for (let i = 0; i < num.length; i++) {
            let avgPrice = (price / num[i]).toFixed(2).replace(/\.?0+$/, ""); // 计算结果并去掉末尾多余的零
            output += `<span style="color: rgb(51, 51, 51);" data-mce-style="color: rgb(51, 51, 51);">平均每${units[i]}${avgPrice}</span><br>`;
            if (i < num.length - 1) {
                output += `<span style="color: rgb(236, 40, 39);" data-mce-style="color: rgb(236, 40, 39);">到手共${num[i + 1]}${units[i + 1]}</span><br>`;
            }
        }

        // style="color: rgb(236, 40, 39);" data-mce-style="color: rgb(236, 40, 39);" 添加红色

        // 去除末尾多余的 <br>
        output = output.replace(/<br>$/, "");

        return output;
    }

    /*
    计算器功能区
    */
    const calculate = [
        {
            regex: /折扣力度.*?(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/,
            replaceFunc: (text, result) => {
                // 替换文本中的折扣内容
                let updatedText = text.replace(/(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/, `${result}`);

                // 确保结果前有一个“约”字,并且前面有“:”或“:”
                if (!updatedText.includes('约')) {
                    // 检查是否已有“:”或“:”,防止重复添加
                    if (!updatedText.includes(':') && !updatedText.includes(':')) {
                        updatedText = updatedText.replace(/(折扣力度.*?)(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/, `$1:约${result}`);
                    } else {
                        updatedText = updatedText.replace(/(折扣力度.*?)(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/, `$1约${result}`);
                    }
                } else {
                    updatedText = updatedText.replace(/(:约|:约)?(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/, `:约${result}`);
                }

                // 确保结果后面有一个“折”字
                if (!updatedText.endsWith('折')) {
                    updatedText += '折';
                }

                return updatedText;
            },
            decimalPlaces: 1,
            multiplier: 10,
            trimTrailingZeros: true
        },
        // {
        //     regex: /平均.*?(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/,
        //     replaceFunc: (text, result) => text.replace(/(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/, `${result}`),
        //     decimalPlaces: 2,
        //     multiplier: 1,
        //     trimTrailingZeros: true
        // },
        // {
        //     regex: /到手.*?(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/,
        //     replaceFunc: (text, result) => text.replace(/(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))/, `${result}`),
        //     decimalPlaces: 2,
        //     multiplier: 1,
        //     trimTrailingZeros: true
        // },
        {
            regex: /.*?(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))==/,
            replaceFunc: (text, result) => text.replace(/(\d+[\d+\-*/().]*\d*|\([\d+\-*/().]+\))==/, `${result}`),
            decimalPlaces: 2,
            multiplier: 1,
            trimTrailingZeros: true
        },
    ];

    // 计算表达式的函数
    const calculateExpression = (expression, decimalPlaces = 2, multiplier = 1, trimTrailingZeros = false) => {
        try {
            let result = eval(expression); // 注意:eval() 存在安全性问题,确保传入的表达式是安全的。
            result = result * multiplier; // 放大结果
            let formattedResult = result.toFixed(decimalPlaces); // 保留指定的小数位数

            // 根据参数决定是否去除末尾的零
            if (trimTrailingZeros) {
                formattedResult = parseFloat(formattedResult).toString();
            }

            return formattedResult;
        } catch (e) {
            console.error('表达式计算错误:', e);
            return expression; // 如果计算错误,返回原表达式
        }
    };

    const findAndCalculateExpressions = (iframeDocument, calculate) => {
        const discountElements = iframeDocument.querySelectorAll('p, span');
        discountElements.forEach(element => {
            let text = element.textContent.replace(/。/g, '.'); // 替换所有中文小数点为英文小数点
            text = text.replace(/(/g, '(').replace(/)/g, ')'); // 替换中文括号为英文括号

            calculate.forEach(({ regex, replaceFunc, decimalPlaces, multiplier, trimTrailingZeros }) => {
                const match = text.match(regex);
                // console.log(match);
                if (match) {
                    const expression = match[1];

                    // 检查是否为“折扣力度”的正则表达式
                    if (regex.source.includes('折扣力度')) {
                        if (/[+\-*/()]/.test(expression)) {
                            // 如果表达式包含运算符,进行计算
                            const result = calculateExpression(expression, decimalPlaces, multiplier, trimTrailingZeros);
                            text = replaceFunc(text, result);
                        } else {
                            // 如果表达式不包含运算符,直接使用替换函数处理
                            text = replaceFunc(text, expression);
                        }
                    } else {
                        // 其他情况照常处理
                        // 检查表达式是否包含运算符
                        if (/[+\-*/()]/.test(expression)) {
                            const result = calculateExpression(expression, decimalPlaces, multiplier, trimTrailingZeros);
                            text = replaceFunc(text, result);
                        }
                    }

                    element.textContent = text;
                }
            });
        });
    };

    // 新增控制功能
    // 支持单个元素内容的着色
    // 封装函数返回包含SVG图标的div
    function createMarioSVGIconWrapper(id, isClick = true, size = 14) {
        // 创建一个 div 容器
        var divWrapper = document.createElement('div');
        divWrapper.className = 'svg-icon-wrapper'; // 添加一个类名,便于查找和样式控制
        divWrapper.style.cssText = 'align-items: center; cursor: pointer; display: none;'; // 样式控制';

        // 设置 div 的 id
        if (id) {
            divWrapper.id = id;
        }

        // 创建 SVG 图标
        var svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
        svgIcon.setAttribute('class', 'icon custom-mario-svg'); // 添加自定义类名
        svgIcon.setAttribute('viewBox', '0 0 1024 1024');
        svgIcon.setAttribute('width', size);
        svgIcon.setAttribute('height', size);
        svgIcon.innerHTML = '<path d="M288.581818 111.709091h55.854546v55.854545H288.581818V111.709091zM176.872727 595.781818h167.563637v55.854546H176.872727v-55.854546zM623.709091 502.690909h111.709091v55.854546h-111.709091v-55.854546zM679.563636 558.545455h111.709091v55.854545h-111.709091v-55.854545zM679.563636 614.4h167.563637v37.236364h-167.563637v-37.236364z" fill="#B8332B" p-id="3610"></path><path d="M176.872727 651.636364h167.563637v74.472727H176.872727v-74.472727zM176.872727 726.109091h111.709091v74.472727H176.872727v-74.472727zM735.418182 726.109091h111.709091v74.472727h-111.709091v-74.472727zM679.563636 651.636364h167.563637v74.472727h-167.563637v-74.472727zM363.054545 558.545455h55.854546v55.854545h-55.854546v-55.854545zM567.854545 558.545455h55.854546v55.854545h-55.854546v-55.854545z" fill="#FFF1E3" p-id="3611"></path><path d="M791.272727 595.781818h55.854546v18.618182h-55.854546v-18.618182zM735.418182 539.927273h37.236363v18.618182h-37.236363v-18.618182zM418.909091 446.836364h204.8v111.709091H418.909091v-111.709091zM232.727273 558.545455h111.709091v37.236363H232.727273v-37.236363zM344.436364 446.836364h18.618181v37.236363h-18.618181v-37.236363zM307.2 484.072727h55.854545v18.618182h-55.854545v-18.618182zM288.581818 502.690909h74.472727v37.236364H288.581818v-37.236364zM251.345455 539.927273h111.70909v18.618182H251.345455v-18.618182zM623.709091 111.709091h18.618182v55.854545h-18.618182V111.709091zM642.327273 148.945455h148.945454v18.618181h-148.945454V148.945455zM344.436364 93.090909h279.272727v74.472727H344.436364V93.090909z" fill="#B8332B" p-id="3612"></path><path d="M344.436364 55.854545h279.272727v37.236364H344.436364V55.854545zM642.327273 111.709091h148.945454v37.236364h-148.945454V111.709091zM288.581818 446.836364h55.854546v37.236363H288.581818v-37.236363zM735.418182 502.690909h55.854545v37.236364h-55.854545v-37.236364zM791.272727 558.545455h55.854546v37.236363h-55.854546v-37.236363zM288.581818 484.072727h18.618182v18.618182H288.581818v-18.618182zM772.654545 539.927273h18.618182v18.618182h-18.618182v-18.618182zM232.727273 539.927273h18.618182v18.618182H232.727273v-18.618182zM232.727273 502.690909h55.854545v37.236364H232.727273v-37.236364zM176.872727 558.545455h55.854546v37.236363H176.872727v-37.236363z" fill="#FF655B" p-id="3613"></path><path d="M288.581818 167.563636h55.854546v55.854546H288.581818V167.563636zM288.581818 335.127273h55.854546v55.854545H288.581818v-55.854545z" fill="#432E23" p-id="3614"></path><path d="M269.963636 856.436364h148.945455v55.854545H269.963636v-55.854545zM269.963636 912.290909h148.945455v55.854546H269.963636v-55.854546z" fill="#9F5A31" p-id="3615"></path><path d="M176.872727 912.290909h93.090909v37.236364H176.872727v-37.236364zM754.036364 912.290909h93.090909v37.236364h-93.090909v-37.236364z" fill="#F38C50" p-id="3616"></path><path d="M176.872727 949.527273h93.090909v18.618182H176.872727v-18.618182zM754.036364 949.527273h93.090909v18.618182h-93.090909v-18.618182zM605.090909 856.436364h148.945455v55.854545h-148.945455v-55.854545zM605.090909 912.290909h148.945455v55.854546h-148.945455v-55.854546z" fill="#9F5A31" p-id="3617"></path><path d="M363.054545 446.836364h55.854546v111.709091h-55.854546v-111.709091zM363.054545 614.4h316.509091v37.236364H363.054545v-37.236364zM344.436364 651.636364h335.127272v74.472727H344.436364v-74.472727zM288.581818 726.109091h446.836364v74.472727H288.581818v-74.472727zM418.909091 595.781818h148.945454v18.618182h-148.945454v-18.618182zM288.581818 800.581818h167.563637v55.854546H288.581818v-55.854546zM567.854545 800.581818h167.563637v55.854546h-167.563637v-55.854546zM623.709091 558.545455h55.854545v55.854545h-55.854545v-55.854545z" fill="#2E67B1" p-id="3618"></path><path d="M418.909091 558.545455h148.945454v37.236363h-148.945454v-37.236363z" fill="#66A8FF" p-id="3619"></path><path d="M344.436364 558.545455h18.618181v93.090909h-18.618181v-93.090909z" fill="#2E67B1" p-id="3620"></path><path d="M400.290909 279.272727h55.854546v55.854546h-55.854546v-55.854546zM400.290909 167.563636h55.854546v55.854546h-55.854546V167.563636zM344.436364 167.563636h55.854545v167.563637h-55.854545V167.563636zM623.709091 279.272727h55.854545v55.854546h-55.854545v-55.854546zM567.854545 223.418182h55.854546v55.854545h-55.854546v-55.854545zM567.854545 335.127273h223.418182v55.854545H567.854545v-55.854545z" fill="#432E23" p-id="3621"></path><path d="M288.581818 223.418182h55.854546v111.709091H288.581818v-111.709091zM456.145455 167.563636h223.418181v55.854546H456.145455V167.563636zM400.290909 223.418182h167.563636v55.854545h-167.563636v-55.854545zM456.145455 279.272727h167.563636v55.854546h-167.563636v-55.854546zM344.436364 335.127273h223.418181v55.854545H344.436364v-55.854545zM344.436364 390.981818h390.981818v55.854546H344.436364v-55.854546zM623.709091 223.418182h167.563636v55.854545h-167.563636v-55.854545zM679.563636 279.272727h167.563637v55.854546h-167.563637v-55.854546z" fill="#FFF1E3" p-id="3622"></path><path d="M232.727273 223.418182h55.854545v167.563636H232.727273v-167.563636z" fill="#432E23" p-id="3623"></path><path d="M232.727273 111.709091h55.854545v111.709091H232.727273V111.709091zM176.872727 223.418182h55.854546v167.563636H176.872727v-167.563636zM232.727273 390.981818h111.709091v55.854546H232.727273v-55.854546zM176.872727 800.581818h111.709091v55.854546H176.872727v-55.854546zM456.145455 800.581818h111.70909v55.854546h-111.70909v-55.854546zM176.872727 856.436364h93.090909v55.854545H176.872727v-55.854545zM121.018182 912.290909h55.854545v111.709091H121.018182v-111.709091zM847.127273 912.290909h55.854545v111.709091h-55.854545v-111.709091zM176.872727 968.145455h223.418182v55.854545H176.872727v-55.854545zM623.709091 968.145455h223.418182v55.854545H623.709091v-55.854545zM735.418182 800.581818h111.709091v55.854546h-111.709091v-55.854546zM754.036364 856.436364h93.090909v55.854545h-93.090909v-55.854545zM288.581818 55.854545h55.854546v55.854546H288.581818V55.854545zM232.727273 446.836364h55.854545v55.854545H232.727273v-55.854545zM176.872727 502.690909h55.854546v55.854546H176.872727v-55.854546zM791.272727 502.690909h55.854546v55.854546h-55.854546v-55.854546zM121.018182 558.545455h55.854545v242.036363H121.018182V558.545455zM418.909091 856.436364h55.854545v111.709091h-55.854545v-111.709091zM549.236364 856.436364h55.854545v111.709091h-55.854545v-111.709091zM847.127273 558.545455h55.854545v242.036363h-55.854545V558.545455zM791.272727 111.709091h55.854546v55.854545h-55.854546V111.709091zM791.272727 223.418182h55.854546v55.854545h-55.854546v-55.854545zM847.127273 279.272727h55.854545v55.854546h-55.854545v-55.854546zM791.272727 335.127273h55.854546v55.854545h-55.854546v-55.854545zM735.418182 390.981818h55.854545v55.854546h-55.854545v-55.854546zM623.709091 446.836364h167.563636v55.854545h-167.563636v-55.854545zM623.709091 55.854545h167.563636v55.854546h-167.563636V55.854545zM679.563636 167.563636h111.709091v55.854546h-111.709091V167.563636zM344.436364 0h279.272727v55.854545H344.436364V0z" fill="#10001D" p-id="3624"></path>';
        svgIcon.style.cssText = 'vertical-align: middle;'; // 垂直居中样式

        // 将 SVG 添加到 div 容器中
        divWrapper.appendChild(svgIcon);

        if (isClick) {
            // 根据 id 绑定点击事件
            divWrapper.addEventListener('click', function () {
                // 提取 id 中的 suffix 部分
                var idSuffix = id.replace('svg-icon-', '');

                // 根据 id 调用对应的函数
                switch (id) {
                    case 'svg-icon-skuDesc':
                        activateIframeAndModifyStyles('skuDesc');
                        break;
                    case 'svg-icon-livePrice':
                        activateIframeAndModifyStyles('livePrice');
                        break;
                    case 'svg-icon-liveGameplay':
                        activateIframeAndModifyStyles('liveGameplay');
                        break;
                    case 'svg-icon-preSetInventory':
                        activateIframeAndModifyStyles('preSetInventory');
                        break;
                    case 'svg-icon-inventory':
                        activateIframeAndModifyStyles('inventory');
                        break;
                    default:
                        break;
                }

                // 仅当 idSuffix 不在数组中时才添加
                if (!isMarioShow.includes(idSuffix)) {
                    isMarioShow.push(idSuffix);
                }
            });
        } else {
            divWrapper.style.display = 'flex';
            divWrapper.className = 'svg-icon-wrapper-no-data';
        }

        return divWrapper;
    }

    // 查找表格中的目标单元格并添加SVG图标
    function addSVGToSpecificTDs() {
        // 获取 class="card-content-container" 内的表格
        var container = document.querySelector('.card-content-container');
        if (!container) return;

        var table = container.querySelector('table');
        if (!table) return;

        var tbody = table.querySelector('tbody');
        if (!tbody) return;

        // 获取 tbody 内的第二个 tr
        var secondTR = tbody.querySelectorAll('tr')[1]; // 获取第二个 tr
        if (!secondTR) return;

        // 获取第二个 tr 中的所有 td
        var tds = secondTR.querySelectorAll('td');
        var idMap = {
            "规格": "svg-icon-skuDesc",
            "直播间到手价": "svg-icon-livePrice",
            "优惠方式": "svg-icon-liveGameplay",
            "预设库存": "svg-icon-preSetInventory",
            "现货库存": "svg-icon-inventory"
        }; // 文本内容与ID的映射

        tds.forEach(function (td) {
            // 检查 td 的文本内容是否在目标文本内容列表中
            var span = td.querySelector('.link-node-container > span');
            if (span && idMap.hasOwnProperty(span.textContent.trim())) {
                // 检查是否已经存在封装的 SVG 图标,避免重复添加
                if (!td.querySelector('.svg-icon-wrapper')) {
                    // 获取对应的 id
                    var id = idMap[span.textContent.trim()];
                    // 创建包含 SVG 图标的 div 容器并设置 id
                    var svgWrapper = createMarioSVGIconWrapper(id);
                    // 将 SVG 容器插入到 span 之后
                    span.parentNode.insertAdjacentElement('afterend', svgWrapper);
                }
            }
        });
    }

    // 初始化存储被点击事件的数组
    var isMarioShow = [];

    // 函数:控制每个 divWrapper 的显示状态
    function updateDivWrapperDisplay(isShow) {
        // 获取所有 class 为 'svg-icon-wrapper' 的元素
        const divWrappers = document.querySelectorAll('.svg-icon-wrapper');

        // 遍历所有 divWrapper
        for (const divWrapper of divWrappers) {
            if (isShow) {
                divWrapper.style.display = 'flex';
            } else {
                // 获取该 divWrapper 的 id
                var wrapperId = divWrapper.id;

                // 检查是否存在于 isMarioShow 数组中
                if (isMarioShow.includes(wrapperId.replace('svg-icon-', ''))) {
                    divWrapper.style.display = 'flex';
                } else {
                    divWrapper.style.display = 'none';
                }
            }
        }
    }

    /*
    淘宝、天猫主图复制到剪贴板功能
    */
    function createGetTmallPngButton() {
        // 找到匹配的元素的编号
        function findMatchingIndex(wrapperClass, imgClass) {
            for (let i = 0; i < wrapperClass.length; i++) {
                const wrapper = document.querySelector(wrapperClass[i]);
                if (wrapper) {
                    const img = wrapper.querySelector(imgClass[i]);
                    if (img) {
                        return i; // 返回匹配的编号
                    }
                }
            }
            return -1; // 如果没有找到匹配的元素,则返回 -1
        }

        if (!document.querySelector('#button_getTmallPngButton')) {
            const wrapperClass = ['.PicGallery--mainPicWrap--juPDFPo', '.picGallery--qY53_w0u', '.PicGallery--mainPicWrap--1c9k21r', '.item-gallery-top.item-gallery-prepub2'];
            const imgClass = ['.PicGallery--mainPic--34u4Jrw', '.mainPic--zxTtQs0P', '.PicGallery--mainPic--1eAqOie', '.item-gallery-top__img'];

            const matchingIndex = findMatchingIndex(wrapperClass, imgClass);

            if (matchingIndex !== -1) {
                const wrapper = document.querySelector(wrapperClass[matchingIndex]);
                // console.log("wrapper:", wrapper);

                if (wrapper) {
                    const button = document.createElement('button');
                    button.textContent = '复制图片';
                    button.id = 'button_getTmallPngButton';
                    button.style.cssText = `
                        position: absolute;
                        top: 50%;
                        left: 50%;
                        transform: translate(-50%, -50%);
                        padding: 5px 20px;
                        font-size: 16px;
                        background-color: rgba(0, 0, 0, 0.5);
                        color: #fff;
                        border: none;
                        border-radius: 999px;
                        font-family: AlibabaPuHuiTi_2_55_Regular;
                        backdrop-filter: blur(10px) brightness(90%); /* 添加模糊效果 */
                        -webkit-backdrop-filter: blur(10px); /* 兼容Safari浏览器 */
                        text-align: center; /* 文本居中显示 */
                        cursor: pointer;
                        opacity: 0;
                        transition: opacity 0.3s ease;
                        z-index: 999;
                    `;

                    // 控制按钮显示
                    wrapper.addEventListener('mouseenter', () => {
                        button.style.opacity = '1';
                    });

                    // 控制按钮隐藏
                    wrapper.addEventListener('mouseleave', () => {
                        button.style.opacity = '0';
                    });

                    button.addEventListener('click', async () => {
                        const img = wrapper.querySelector(imgClass[matchingIndex]);
                        // console.log("img:", img);
                        if (img) {
                            try {
                                const imageUrl = img.src;
                                const response = await fetch(imageUrl);
                                const blob = await response.blob();
                                const image = new Image();
                                image.src = URL.createObjectURL(blob);

                                image.onload = () => {
                                    const canvas = document.createElement('canvas');
                                    const ctx = canvas.getContext('2d');
                                    canvas.width = image.width;
                                    canvas.height = image.height;
                                    ctx.drawImage(image, 0, 0);
                                    canvas.toBlob(async (blob) => {
                                        try {
                                            await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]);
                                            showNotification("图片已成功复制到剪贴板", undefined, true);
                                            // alert('Image copied to clipboard!');
                                        } catch (error) {
                                            console.error('Failed to copy image to clipboard:', error);
                                            // alert('Failed to copy image to clipboard.');
                                            showNotification('图片复制失败!');
                                        }
                                    }, 'image/png');
                                };
                            } catch (error) {
                                showNotification('图片复制失败!');
                                console.error('Failed to fetch or process image:', error);
                                // alert('Failed to copy image to clipboard.');
                            }
                        } else {
                            // alert('Image not found!');
                        }
                    });

                    wrapper.style.position = 'relative'; // 确保按钮在图片上层
                    wrapper.appendChild(button);
                } else {
                    // console.error('Wrapper element not found.');
                }
            } else {
                // console.error('No matching element found.');
            }
        } else {
            // console.log('Button already exists, skipping creation.');
        }
    }

    /*
   用于主播排序的辅助函数
   */
    // 封装处理和生成多维数组数据的函数
    function generateJsonFromText(textInput) {
        // 简写与全称的对应表
        const shorthandToFull = {
            "野": "小野",
            "月": "小月",
            "霸": "小霸王",
            "迎": "小迎",
            "京": "京京",
            "祖": "阿祖",
            "凯": "凯子"
        };

        // 处理文本,去除“+”及其后的内容
        const processText = (text) => text.split('+')[0].trim();

        // 将简写转换为全称
        const getFullNames = (text) => {
            return text.split('').map(ch => shorthandToFull[ch] || ch);
        };

        // 生成多维数组格式数据
        const result = [];

        // 示例输入 [1][0][0]
        // 解释:第一个位置存储多个主播的排列组合,第二个位置0存储主播名字,其余位置存储其rowIndex值,第三个位置用于读取主讲或副讲
        // 获取主讲 resultArray[2][0][0];
        // 获取副讲 resultArray[2][0][1];
        // 获取其列 resultArray[2][i]

        const texts = textInput.trim().split('\n');

        texts.forEach((text, index) => {
            const processedText = processText(text);
            const fullNamesArray = getFullNames(processedText);

            // 查找是否已存在相同的 fullNamesArray
            const existingEntry = result.find(entry => {
                return JSON.stringify(entry[0]) === JSON.stringify(fullNamesArray);
            });

            if (existingEntry) {
                // 如果存在相同的组合,追加索引
                existingEntry.push(index);
            } else {
                // 如果不存在,创建一个新的组合
                result.push([fullNamesArray, index]);
            }
        });

        return result;
    }

    // 页面控制函数
    function itemPageScroll(height, addScroll = true) {
        return new Promise((resolve) => {
            // .rc-virtual-list-holder
            const viewport = document.querySelector('.ag-body-vertical-scroll-viewport'); // 获取页面滚动区域

            if (addScroll) {
                if (height != 0) {
                    viewport.scrollTop += height; // 向下滚动一屏
                } else {
                    viewport.scrollTop = 0; // 回到顶部
                }
            } else {
                viewport.scrollTop = height; // 直接滚动到指定位置
            }

            // console.log('scrolling to', viewport.scrollTop); // 打印当前滚动高度

            // 通过 setTimeout 来模拟等待页面加载完成
            setTimeout(() => {
                resolve(); // 滚动完成后继续执行
            }, 800); // 延迟时间可以根据实际加载时间调整
        });
    }

    // 商品选择函数
    function selectItemForRowIndex(rowIndex, retries = 5, delay = 500) {
        return new Promise((resolve, reject) => {
            // 找到带有指定 row-index 的 div
            const targetDiv = document.querySelector(`div[row-index="${rowIndex}"]`);

            // 在 targetDiv 内查找 col-id="choice" 的元素
            if (targetDiv) {
                const choiceElement = targetDiv.querySelector('div[col-id="choice"]');

                // 在 choiceElement 内找到唯一的 input 元素
                if (choiceElement) {
                    const inputElement = choiceElement.querySelector('input');
                    if (inputElement) {
                        inputElement.click(); // 点击 input 元素
                        // console.log(`Selected item for row-index="${rowIndex}"`);
                        resolve(); // 选择完成后继续执行
                    } else {
                        // input 元素未找到的情况
                        retryOrReject(`未找到 input 元素在 col-id="choice" 的元素内`);
                    }
                } else {
                    // console.log(`未找到 col-id="choice" 的元素在 row-index="${rowIndex}" 的 div 内`);
                    retryOrReject(`未找到 col-id="choice" 的元素在 row-index="${rowIndex}" 的 div 内`);
                }
            } else {
                // console.log(`未找到 row-index="${rowIndex}" 的 div`);
                retryOrReject(`未找到 row-index="${rowIndex}" 的 div`);
            }

            function retryOrReject(errorMessage) {
                if (retries > 0) {
                    setTimeout(() => {
                        // 递归调用自己,并减少重试次数
                        selectItemForRowIndex(rowIndex, retries - 1, delay).then(resolve).catch(reject);
                    }, delay);
                } else {
                    reject(errorMessage); // 达到最大重试次数后,返回错误
                }
            }
        });
    }

    // 模拟鼠标点击,激活主播选择框
    // 示例调用:点击 "主讲主播" 的选择框
    // clickSelectByTitle("主讲主播");

    // 示例调用:点击 "副讲主播" 的选择框
    // clickSelectByTitle("副讲主播");

    async function clickSelectByTitle(title, retries = 5, delay = 500) {
        // 重试机制,最多重试 `retries` 次,每次等待 `delay` 毫秒
        for (let i = 0; i < retries; i++) {
            const labelElement = document.querySelector(`label[title="${title}"]`);

            if (labelElement) {
                const parentElement = labelElement.parentElement;
                if (parentElement && parentElement.parentElement) {
                    const selectSelector = parentElement.parentElement.querySelector('.ant-select-selector');

                    if (selectSelector) {
                        // 模拟点击
                        const clickEvent = new MouseEvent('mousedown', {
                            bubbles: true,
                            cancelable: true
                        });
                        selectSelector.dispatchEvent(clickEvent); // 模拟点击事件

                        // console.log(`已激活并点击 ${title} 对应的选择框`);
                        return; // 成功找到并点击后直接返回
                    } else {
                        // console.log(`未找到 ${title} 对应的 .ant-select-selector 元素`);
                    }
                } else {
                    // console.log(`无法获取到 ${title} 的父元素`);
                }
            } else {
                // console.log(`未找到 title 为 "${title}" 的 label 元素`);
            }

            // 如果没找到,等待一段时间再重试
            if (i < retries - 1) {
                // console.log(`重试 ${i + 1}/${retries} 次后等待 ${delay} 毫秒...`);
                await new Promise(resolve => setTimeout(resolve, delay));
            }
        }

        // 如果所有重试都失败了,抛出一个错误
        throw new Error(`无法找到 title 为 "${title}" 的元素。`);
    }

    // 选择指定的主播
    function selectAnchor(anchor, primary = true) {
        return new Promise((resolve, reject) => {
            // 根据 primary 参数决定目标容器的选择器
            const targetDiv = primary ? '#primaryAnchor_list' : '#assistantAnchor_list';

            // 获取视窗元素
            const viewFather = document.querySelector(targetDiv).parentElement;
            const viewport = viewFather.querySelector('.rc-virtual-list-holder');

            // 定义一个异步函数来执行循环操作
            (async function trySelect() {
                for (let attempt = 0; attempt < 4; attempt++) {
                    // 查找目标选项
                    const targetOption = `.ant-select-item[title="${anchor}"]`;
                    const optionElement = document.querySelector(targetDiv).parentElement.querySelector(targetOption);

                    if (optionElement) {
                        // 如果找到选项,则点击并完成操作
                        optionElement.click();
                        // console.log(`已选择 ${anchor} 主播`);
                        resolve();
                        return; // 结束函数
                    }

                    // 如果没有找到,滚动视窗
                    viewport.scrollTop += 256;
                    // console.log(`未找到 ${anchor} 主播,正在尝试第 ${attempt + 1} 次滚动`);

                    // 等待一点时间以让页面加载新内容
                    await new Promise(r => setTimeout(r, 500));
                }

                // 如果经过多次尝试仍未找到,抛出错误或处理异常
                // console.log(`未能找到 ${anchor} 主播,已尝试 ${4} 次`);
                reject(new Error(`未能找到 ${anchor} 主播`));
            })();
        });
    }

    // 点击“计划主播排班”
    function clickScheduleButton() {
        return new Promise((resolve) => {
            const scheduleButtonClassName = '.ant-btn.css-9fw9up.ant-btn-default.primaryButton___N3z1x'
            clickButton(true, 0, document, scheduleButtonClassName, '计划主播排班');
            resolve();
        });
    }

    // 点击“排班页面”的“确定”按钮
    function clickConformButtonForSchedule() {
        return new Promise((resolve) => {
            const scheduleClassName = '.ant-modal-content';
            const conformButtonClassName = '.ant-btn.css-9fw9up.ant-btn-primary';

            clickButton(true, 0, scheduleClassName, conformButtonClassName);
            resolve();
        });
    }

    async function processItems() {
        // 返回输入文本中对应的商品个数
        function countAllItemNum(resultArray){
            var countNum = 0;

            for(var i = 0; i < resultArray.length; i++){
                countNum += resultArray[i].length - 1;
            }

            // console.log('countNum:', countNum);
            return countNum;
        }

        // 返回当前已经排序的最大商品序号
        function getMaxForScheduledItemIndex() {
            if (scheduledItems.length === 0) {
                return 0;
            }

            // 对已排班的商品序号进行排序
            scheduledItems.sort((a, b) => a - b);

            // 遍历数组,找到最小的缺失序号
            for (let i = 0; i < scheduledItems.length; i++) {
                if (scheduledItems[i] !== i) {
                    // console.log('Missing index:', i);
                    return i; // 一旦发现某个序号不连续,返回这个序号
                }
            }

            // 如果所有序号都连续,则返回下一个未使用的序号
            return scheduledItems.length;
        }

        let scheduledItems = []; // 已排班的商品序号
        let rounder = 0; // 轮次计数器
        try {
            const elements = document.getElementsByClassName('fontLinkd-[#333_20_20_Bold]');
            const textContent = elements[0].innerText || elements[0].textContent;
            const countItem = parseInt(textContent, 10); // link上的商品总数

            // 原生浏览器弹窗提示
            // const textInput = prompt('请输入主播排班表格内容,一行对应一个商品');

            const textInput = await createDropdownModal(dropdownContainer, '主播排序');

            const resultArray = generateJsonFromText(textInput);
            // console.log(resultArray);

            // 商品数检查
            if (countAllItemNum(resultArray) > countItem) {
                click_itemSort_anchorSort();
                setTimeout(() => {
                    showNotification('输入了过多的主播,请检查后重新输入!');
                }, 1500);

                return;
            }

            while (rounder < resultArray.length) {
                if (!itemSort_anchorSort.getDivButtonState()) return; // 跳出循环,如果主播排序未打开,则不执行任何操作
                await itemPageScroll(120 * getMaxForScheduledItemIndex(), false); // 回到合适的位置或许是顶部

                showNotification('正在处理第 ' + (rounder + 1) + '/' + resultArray.length + ' 轮次 ' + loadImageIcon(), 0);

                await new Promise(resolve => setTimeout(resolve, 500)); // 等待500毫秒,可根据需要调整

                let index = 1;
                let checkCount = 0;

                for (let i = getMaxForScheduledItemIndex(); i < countItem; i++, checkCount++) {
                    if (!itemSort_anchorSort.getDivButtonState()) return; // 跳出循环,如果主播排序未打开,则不执行任何操作

                    if (resultArray[rounder][index] == i) {
                        await selectItemForRowIndex(i); // 选择指定的行
                        scheduledItems.push(i); // 记录已排班的商品序号
                        index++;
                    }

                    if ((checkCount + 1) % 4 === 0) await itemPageScroll(480); // 每处理4行,滚动页面

                    // console.log('Index:', index, 'Length:', resultArray[rounder].length);

                    if (index === resultArray[rounder].length) {
                        // console.log('Executing scheduling...');

                        await new Promise(resolve => setTimeout(resolve, 500)); // 等待500毫秒,可根据需要调整
                        await clickScheduleButton();
                        await clickSelectByTitle("主讲主播");
                        await selectAnchor(resultArray[rounder][0][0], true);
                        await clickSelectByTitle("副讲主播");
                        if (resultArray[rounder][0][1]) {
                            await selectAnchor(resultArray[rounder][0][1], false);
                        }
                        await clickConformButtonForSchedule();

                        rounder++;
                        break; // 跳出循环,继续处理下一个商品
                    }
                }

                // 确保整个循环内容都执行完再进入下一次迭代
                await new Promise(resolve => setTimeout(resolve, 500)); // 等待500毫秒,可根据需要调整
            }
            setTimeout(() => {
                click_itemSort_anchorSort();
            }, 1500);
            showNotification('全部处理完成!');
        } catch (error) {
            // 捕获任何异常,并显示错误通知
            click_itemSort_anchorSort();
            setTimeout(() => {
                showNotification('处理过程中出现错误!');
            }, 1500);
            console.error('Error occurred:', error);
        } finally {
            // 可选的: 在所有操作完成后执行清理工作
            // console.log('处理流程结束');
        }
    }

    function click_itemSort_anchorSort() {
        document.getElementById('itemSort_anchorSort_divButton').click();
    }

    // 输入弹窗
    function createDropdownModal(dropdownContainer, titleText) {
        return new Promise((resolve, reject) => {
            // 检查是否已有弹窗存在,如果有则移除
            const existingModal = dropdownContainer.querySelector('.dropdown-modal');
            if (existingModal) {
                dropdownContainer.removeChild(existingModal);
            }

            dropdownButton.style.display = 'none'; // 隐藏按钮
    
            // 创建弹窗容器
            var dropdownDivModal = document.createElement('div');
            dropdownDivModal.classList.add('dropdown-modal'); // 添加一个类以便于识别
            dropdownDivModal.style.cssText = `
                position: absolute;
                top: 0;
                margin: 14px;
                width: 172px;
                height: 108px;
                background-color: rgba(233, 233, 233, 0.7);
                backdrop-filter: blur(8px) brightness(90%);
                border: 1px solid rgba(255, 98, 0, 0.25);
                border-radius: 10px;
                box-sizing: border-box;
                display: flex;
                flex-direction: column;
                z-index: 3;
                transform-origin: top center;
            `;
    
            // 创建标题行
            const title = document.createElement('div');
            title.textContent = titleText || '弹窗标题';
            title.style.cssText = `
                padding: 8px 10px;
                font-size: 14px;
                font-weight: bold;
                color: rgb(51, 51, 51);
                border-bottom: 0px solid #ccc;
                text-align: left;
                flex-shrink: 0;
            `;
            dropdownDivModal.appendChild(title);
    
            // 创建富文本框
            const textarea = document.createElement('textarea');
            textarea.style.cssText = `
                width: calc(100% - 20px);
                background-color: rgba(249, 249, 249, 0.7);
                height: 30px;
                margin: 0px 10px;
                padding: 5px;
                font-size: 12px;
                border: 0px solid #ccc;
                border-radius: 4px;
                resize: none;
                line-height: 1.2;
                box-sizing: border-box;
                flex-grow: 1;
            `;
            dropdownDivModal.appendChild(textarea);
    
            // 创建按钮容器
            const buttonContainer = document.createElement('div');
            buttonContainer.style.cssText = `
                display: flex;
                justify-content: space-between;
                padding: 8px 10px;
                border-top: 0px solid #ccc;
                flex-shrink: 0;
            `;
    
            // 创建“取消”按钮
            const cancelButton = document.createElement('button');
            cancelButton.textContent = '取消';
            cancelButton.style.cssText = `
                padding: 3px 8px;
                font-size: 12px;
                color: #fff;
                background-color: #f44336;
                border: none;
                border-radius: 5px;
                cursor: pointer;
                flex-basis: 48%;
            `;
            cancelButton.onclick = () => {
                dropdownContainer.removeChild(dropdownDivModal);
                dropdownButton.style.display = ''; // 恢复按钮
                reject('用户取消了输入');
            };
            buttonContainer.appendChild(cancelButton);
    
            // 创建“确认”按钮
            const confirmButton = document.createElement('button');
            confirmButton.textContent = '确认';
            confirmButton.style.cssText = `
                padding: 3px 8px;
                font-size: 12px;
                color: #fff;
                background-color: #4CAF50;
                border: none;
                border-radius: 5px;
                cursor: pointer;
                flex-basis: 48%;
            `;
    
            confirmButton.onclick = () => {
                const textInput = textarea.value;
                dropdownContainer.removeChild(dropdownDivModal);
                dropdownButton.style.display = ''; // 恢复按钮
                resolve(textInput); // 在确认时返回输入的内容
            };
            buttonContainer.appendChild(confirmButton);
    
            dropdownDivModal.appendChild(buttonContainer);
    
            dropdownContainer.appendChild(dropdownDivModal);
        });
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址