HY temu批量属性

根据页面变化循环执行操作

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

// ==UserScript==
// @name         HY temu批量属性
// @namespace    http://tampermonkey.net/
// @license MIT
// @version      1.0
// @description  根据页面变化循环执行操作
// @author       lyw
// @match        https://seller.kuajingmaihuo.com/*
// @run-at       document-end
// @grant        GM_addStyle
// @require      https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.15.6/index.min.js
// @resource     element-ui-css https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.15.6/theme-chalk/index.min.css
// ==/UserScript==

(function () {
    'use strict';

    // Inject Element UI CSS
    GM_addStyle('@import url("https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.15.6/theme-chalk/index.min.css");');

    // Create a Vue instance for Element UI
    const app = document.createElement('div');
    document.body.appendChild(app);

    // Define Vue component with Element UI dialog
    new Vue({
        el: app,
        data: {
            spu1: null,
            previousSpu1Value: '',
            loadIntervalId: null, // 用来保存监控 load 元素的 setInterval ID
            spuChangeIntervalId: null, // 用来保存检测 spu1 变化的 setInterval ID
            escListenerAdded: false, // 用于标识是否已添加 ESC 键监听
        },
        methods: {
            // meth 函数,用于执行特定的页面操作
            meth() {
                const atr = document.querySelector('.show-property-edit-drawer_container__3hhJI form .product-property_propertyBlock__3hQXd .Form_itemContentCenter_5-113-0 .IPT_inputBlockCell_5-113-0.ST_inputBlockCell_5-113-0 input');
                atr.click();
                const atr1 = document.querySelector('.PT_portalMain_5-113-0.PP_dropdownMain_5-113-0.ST_dropdownMain_5-113-0 ul li .name-label_enName__3YVad');
                if (atr1 && atr1.innerHTML === 'Smooth fabric') {
                    atr1.click();
                }
                const element = document.querySelector('.product-property_propertyBlock__3hQXd [data-testid="beast-core-input-htmlInput"]');
                if (element) {
                    element.focus();
                    document.execCommand('selectAll', false, null);
                    document.execCommand('insertText', false, '220');
                }
                console.log("Meth function executed");

                let submitButton = document.querySelector('[data-tracking-id="JUh96Eg0NggWr04P"]');
                if (submitButton) {
                    submitButton.click();
                    console.log("Submit button clicked");
                }

                // 在 meth() 执行完成后,启动一个 setInterval 监测 spu1 的变化
                this.spuChangeIntervalId = setInterval(() => {
                    this.loop();
                }, 1000); // 每秒检查一次 spu1 的变化
            },

            // 循环函数,根据 Spu1 的变化来执行操作
            loop() {
                console.log("Checking spu1 change...");
                if (this.spu1 && this.spu1.innerHTML !== this.previousSpu1Value) {
                    // 停止监测 spu1 变化的循环
                    clearInterval(this.spuChangeIntervalId);
                    // 停止 spu1 的检测
                    console.log("Spu1 value has changed!");
                    this.previousSpu1Value = this.spu1.innerHTML; // 更新保存的值
                    // 监测 load 元素消失
                    this.monitorLoad();
                    console.log("Spu1 check loop stopped.");
                }
            },

            // 监控 load 元素的变化
            monitorLoad() {
                this.loadIntervalId = setInterval(() => {
                    const load = document.querySelector('.index-module__drawer-body___Qj4d- .index-module__content___7Av7B .show-property-edit-drawer_container__3hhJI form .Spn_container_5-113-0.Spn_spinning_5-113-0');
                    if (!load) {
                        clearInterval(this.loadIntervalId); // 结束监测 load 的循环
                        console.log("Load element has disappeared, calling meth()");
                        this.meth(); // 调用 meth 函数
                        console.log("Load element check stopped.");
                    }
                }, 1000); // 每秒检查一次
            },

            // 启动循环的逻辑
            startLoop() {
                // 监听 F4 键触发循环开始
                window.addEventListener('keydown', (event) => {
                    if (event.key === 'F4') {
                        this.spu1 = document.querySelector('.show-property-edit-drawer_container__3hhJI .copy-text_copy__3Cie7');
                        if (this.spu1) {
                            this.spu1 = this.spu1.previousElementSibling;
                            this.previousSpu1Value = this.spu1 ? this.spu1.innerHTML : '';
                            console.log("F4 pressed, starting loop");
                            this.meth();
                        } else {
                            console.error('Spu1 元素未找到!');
                        }
                    }
                    // 监听 ESC 键退出脚本
                    if (event.key === 'Escape') {
                        this.stopScript();
                    }
                });

                // 如果 ESC 键监听器尚未添加,进行添加
                if (!this.escListenerAdded) {
                    this.escListenerAdded = true;
                    window.addEventListener('keydown', (event) => {
                        if (event.key === 'Escape') {
                            this.stopScript();
                        }
                    });
                }
            },

            // 停止脚本的执行
            stopScript() {
                console.log("ESC pressed, stopping the script.");
                // 清除所有定时器
                clearInterval(this.spuChangeIntervalId);
                clearInterval(this.loadIntervalId);
                // 移除所有事件监听器
                window.removeEventListener('keydown', this.stopScript);
                // 这里可以清理其他资源(如果需要)
                console.log("Script has been stopped.");
            }
        },

        // 页面加载后执行
        mounted() {
            console.log("Vue instance mounted, starting loop on F4");
            this.startLoop();
        }
    });
})();

QingJ © 2025

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