闲管家自动填写表单

自动填写设置的默认值,帮助更快的上传商品。

当前为 2023-07-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         闲管家自动填写表单
// @namespace    Take
// @version      1.21
// @description  自动填写设置的默认值,帮助更快的上传商品。
// @match        https://www.goofish.pro/sale/product/*
// @homepage     https://www.aiapply.cn
// @grant        GM_getValue
// @grant        GM_setValue
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    console.log('脚本开始执行');

    // 从本地存储中获取配置
    let defaultCategory = GM_getValue('defaultCategory', '');
    let defaultStore = GM_getValue('defaultStore', '');
    let defaultTitle = GM_getValue('defaultTitle', '');
    let defaultDescription = GM_getValue('defaultDescription', '');
    let defaultCategories = []
    let defaultStores = []
    let ok = 0

    // 获取页面的分类、店铺数据
    function getCategories(){
        defaultCategories = []
        defaultStores = []


        // 获取包含所有分类
        const elementDiv = document.querySelectorAll('div.release-history');
        console.log(elementDiv)
        if(elementDiv){
            // 获取所有的<span>元素
            //const spanElements = element.querySelectorAll('span.color-blue');
            // 遍历每个<span>元素,获取文本内容并添加到数组中
            elementDiv.forEach(div => {
                const text = div.textContent.trim();
                if(text!='上传方式' && text!='仓储管理'){
                    defaultCategories.push(text.replace("、", ""));
                }
            });
            console.log(defaultCategories)
        }else{
            defaultCategories = ["请先添加商品后,才能读取到历史类别"]
        }
        

        var parentElement = document.querySelector('.auth-list'); // 使用class选择器选取父元素
        var liElements = parentElement.querySelectorAll('li');
        liElements.forEach(function(li) {
            var text = li.textContent.trim(); // 提取文本内容并去除首尾空格
            if(text!='创建闲鱼店铺'){
                defaultStores.push(text);
            }

        });
        console.log(defaultStores)
        updateConfigPopupOptions()
    }

    // 创建一键赋值按钮
    const button = document.createElement('button');
    button.textContent = '一键赋值';
    button.style.position = 'fixed';
    button.style.bottom = '100px';
    button.style.right = '120px';
    button.style.zIndex = '9999';
    button.style.padding = '10px';
    button.style.fontSize = '16px';
    button.style.backgroundColor = '#4CAF50';
    button.style.color = '#fff';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.2)';
    button.addEventListener('click', fillForm);
    document.body.appendChild(button);

    // 创建配置按钮
    const configIcon = document.createElement('button');
    configIcon.textContent = '设置';
    configIcon.style.position = 'fixed';
    configIcon.style.bottom = '100px';
    configIcon.style.right = '50px';
    configIcon.style.zIndex = '9999';
    configIcon.style.padding = '10px';
    configIcon.style.fontSize = '16px';
    configIcon.style.backgroundColor = '#4CAF50';
    configIcon.style.color = '#fff';
    configIcon.style.border = 'none';
    configIcon.style.borderRadius = '5px';
    configIcon.style.cursor = 'pointer';
    configIcon.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.2)';
    configIcon.addEventListener('click', openConfigPopup);
    document.body.appendChild(configIcon);

    // 创建配置弹窗的 HTML 代码
    const configPopupHTML = `
    <div id="configPopup" style="
        display: none;
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 9999;
        width: 400px;
        padding: 20px;
        background-color: #fff;
        border: 1px solid #ccc;
        border-radius: 5px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    ">
        <h3 style="margin-top: 0;">设置</h3>
        <form style="height: 100%;">
            <label style="margin: 10px 0; display: block;">
                <span style="font-weight: bold;">选中类别:</span>
                <select id="categoryInput" style="width: 100%;" style='margin-top: 10px;'>
                    ${renderOptions(defaultCategories, defaultCategory)}
                </select>
            </label>
            <label style="margin: 10px 0; display: block;">
                <span style="font-weight: bold;">选中店铺:</span>
                <select id="storeInput" style="width: 100%;"placeholder="请选择默认店铺">
                    ${renderOptions(defaultStores, defaultStore)}
                </select>
            </label>
            <label style="margin: 10px 0; display: block;">
                <span style="font-weight: bold;">标题前缀:</span>
                <input id="titleInput" type="text" style="width: 100%;"placeholder="请输入标题开头" style='margin-top: 10px;'>
            </label>
            <label style="margin: 10px 0; display: block;">
                <span style="font-weight: bold;">商品描述:</span>
                <textarea id="descriptionInput" rows="8" cols="50"placeholder="请输入默认描述" style='margin-top: 10px;'></textarea>
            </label>
            <div style="text-align: right;">
            <div  id="cancelConfigButton" style="padding: 10px 50px;
    display: inline-block;
    border-radius: 3px;
    border: 1px solid #dcdfe6;
    margin-right: 10px;">取消</div>
                <div id="saveConfigButton" style="padding: 10px 50px;
    display: inline-block;
    background-color: #0daeff;
    color: white;
    border-radius: 3px;">保存</div>

            </div>
        </form>
    </div>`;

    // 辅助函数:根据数组渲染下拉选择器选项
    function renderOptions(options, selectedValue) {
        return options
            .map(option => `<option value="${option}" ${option === selectedValue ? 'selected' : ''}>${option}</option>`)
            .join('');
    }

    // 更新配置弹窗中的选项
    function updateConfigPopupOptions() {
        const categoryInput = document.getElementById('categoryInput');
        const storeInput = document.getElementById('storeInput');
        const updatedCategoryOptions = renderOptions(defaultCategories, defaultCategory);
        const updatedStoreOptions = renderOptions(defaultStores, defaultStore);
        categoryInput.innerHTML = updatedCategoryOptions;
        storeInput.innerHTML = updatedStoreOptions;
    }

    // 插入配置弹窗到页面中
    document.body.insertAdjacentHTML('beforeend', configPopupHTML);

    // 获取配置弹窗元素
    const configPopup = document.getElementById('configPopup');



    // 创建配置按钮点击事件处理函数
    function openConfigPopup() {
        getCategories()

        configPopup.style.display = 'block';
        // 创建一键赋值按钮点击事件处理函数

        const titleInput = document.getElementById('titleInput');
        const descriptionInput = document.getElementById('descriptionInput');


        // 填充表单字段

        titleInput.value = defaultTitle;
        descriptionInput.value = defaultDescription;
    }

    // 创建保存配置按钮点击事件处理函数
    function saveConfig() {
        const categoryInput = document.getElementById('categoryInput');
        const storeInput = document.getElementById('storeInput');
        const titleInput = document.getElementById('titleInput');
        const descriptionInput = document.getElementById('descriptionInput');

        // 将配置保存到本地存储
        defaultCategory = categoryInput.value
        defaultStore = storeInput.value
        defaultTitle = titleInput.value
        defaultDescription = descriptionInput.value
        GM_setValue('defaultCategory', categoryInput.value);
        GM_setValue('defaultStore', storeInput.value);
        GM_setValue('defaultTitle', titleInput.value);
        GM_setValue('defaultDescription', descriptionInput.value);

        // 提示保存成功,并关闭配置弹窗
        alert('保存成功');
        closeConfigPopup();
    }

    // 创建取消配置按钮点击事件处理函数
    function closeConfigPopup() {

        configPopup.style.display = 'none';
    }


    // 监听保存配置按钮点击事件
    const saveConfigButton = document.getElementById('saveConfigButton');
    saveConfigButton.addEventListener('click', saveConfig);

    // 监听取消配置按钮点击事件
    const cancelConfigButton = document.getElementById('cancelConfigButton');
    cancelConfigButton.addEventListener('click', closeConfigPopup);

    // 更新商品标题
    function updateProductTitle(isbnInput,titleInput,publisherInput,authorInput) {
        var isbn = isbnInput.value || '';
        var title = titleInput.value || '';
        var publisher = publisherInput.value || '';
        var author = authorInput.value || '';

        var productTitle = defaultTitle + title + ' ' + author + ' ' + publisher + ' ' + isbn;


        // 填写商品标题
        var newtitle = document.querySelector('input[placeholder="请输入商品标题,最多允许输入30个汉字"]');
        if (newtitle) {
            console.log('找到商品标题输入框',productTitle);
            newtitle.value = productTitle;
            triggerInputEvent(newtitle);
        } else {
            console.log('未找到商品标题输入框');
        }
    }

    // 填写商品标题和商品描述
    function fillForm() {

        // 获取立即发布按钮的元素
        const publishButton = document.querySelectorAll('label.el-radio');
        if (publishButton[5]) {
            // 模拟点击立即发布按钮
            console.log('选中立即发布',publishButton);
            publishButton[5].click();
        } else {
            console.log('未找到立即发布按钮');
        }

        

        getCategories()
        var genreElements = document.querySelectorAll('div.release-history');
        console.log(genreElements)
        for (var i = 0; i < genreElements.length; i++) {
            var genreElement = genreElements[i];
            var genreTitle = genreElement.textContent.trim();
            if (genreTitle.includes(defaultCategory)) {

                
                genreElement.click();
                console.log('成功点击"' + defaultCategory + '"');

              
                
                // 延迟执行
                setTimeout(function() {

                    
                    // 选择店铺
                    var storeElements = document.querySelectorAll('.auth-list li:not(.sku-add-btn)');

                    for (var ii = 0; i < storeElements.length; ii++) {
                        var storeElement = storeElements[ii];
                        var storeTitle = storeElement.querySelector('.auth-left p').textContent.trim();

                        if (storeTitle == defaultStore) {

                            storeElement.click();
                            console.log('成功点击"' + defaultStore + '"');

                            break;
                        }
                    }
                  

                    // 延迟执行
                    setTimeout(function() {

                        // 监听输入框的值变化并更新商品标题
                        var isbnInput = document.querySelector('input[placeholder="请输入ISBN编码"]');
                        var titleInput = document.querySelector('input[placeholder="请输入书名"]');
                        var publisherInput = document.querySelector('input[placeholder="请输入出版社"]');
                        var authorInput = document.querySelector('input[placeholder="请输入作者"]');
                        const button = document.querySelector('button.el-button');

                        button.addEventListener('click', function() {
                            console.log('查询按钮被点击了');
                            setTimeout(function() {
                                isbnInput.value = isbnInput.value;
                                triggerInputEvent(isbnInput);
                                updateProductTitle(isbnInput,titleInput,publisherInput,authorInput)
                            }, 200);
                        });
                        updateProductTitle(isbnInput,titleInput,publisherInput,authorInput)
                        if (isbnInput && titleInput && publisherInput && authorInput) {
                            isbnInput.addEventListener('input', function() {
                                updateProductTitle(isbnInput,titleInput,publisherInput,authorInput);
                            });
                            titleInput.addEventListener('input', function() {
                                updateProductTitle(isbnInput,titleInput,publisherInput,authorInput);
                            });
                            publisherInput.addEventListener('input', function() {
                                updateProductTitle(isbnInput,titleInput,publisherInput,authorInput);
                            });
                            authorInput.addEventListener('input', function() {
                                updateProductTitle(isbnInput,titleInput,publisherInput,authorInput);
                            });
                        }

                        // 填写商品描述
                        var descriptionTextarea = document.querySelector('textarea[placeholder="请输入商品描述"]');
                        if (descriptionTextarea) {
                            console.log('找到商品描述输入框');
                            descriptionTextarea.value = defaultDescription;
                            triggerInputEvent(descriptionTextarea);
                        } else {
                            console.log('未找到商品描述输入框');
                        }
                     }, 100); // 延迟1秒执行填写操作

                }, 100); // 延迟1秒执行填写操作



                return;
            }
        }
        console.log('未找到"' + defaultCategory + '"');


    }



    // 触发输入事件,以便网页响应
    function triggerInputEvent(element) {
        var inputEvent = new Event('input', {
            bubbles: true,
            cancelable: true,
        });
        element.dispatchEvent(inputEvent);
    }
})();

QingJ © 2025

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