闲鱼管家上货助手

在商品页面复制商品信息并在闲鱼管家后台上架页面插入“一键填充”按钮,添加下载商品详情图片的功能。

目前為 2024-06-23 提交的版本,檢視 最新版本

// ==UserScript==
// @name         闲鱼管家上货助手
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  在商品页面复制商品信息并在闲鱼管家后台上架页面插入“一键填充”按钮,添加下载商品详情图片的功能。
// @author       阿阅 wx:pangyue2    mail:[email protected]
// @match        https://h5.m.goofish.com/item?id=*
// @match        https://www.goofish.pro/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=goofish.pro
// @grant        GM_setValue
// @grant        GM_getValue
// @require      https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function showToast(message, type = 'success') {
        Swal.fire({
            toast: true,
            position: 'top-end',
            icon: type,
            title: message,
            showConfirmButton: false,
            timer: 1500
        });
    }

    function downloadImage(url, fileName) {
        fetch(url)
            .then(response => response.blob())
            .then(blob => {
                const a = document.createElement('a');
                a.href = URL.createObjectURL(blob);
                a.download = `${fileName}.jpg`;
                a.click();
                URL.revokeObjectURL(a.href);
            })
            .catch(error => console.error('图片下载失败:', error));
    }

    // 商品页面逻辑
    if (window.location.href.includes('https://h5.m.goofish.com/item?id=')) {
        if (window.top === window.self) {
            const button = document.createElement('button');
            button.innerText = '复制商品信息 & 下载图片';
            button.style.position = 'fixed';
            button.style.bottom = '20px';
            button.style.right = '20px';
            button.style.zIndex = 1000;
            button.style.padding = '10px 20px';
            button.style.backgroundColor = '#28a745';
            button.style.color = 'white';
            button.style.border = 'none';
            button.style.borderRadius = '5px';
            button.style.cursor = 'pointer';

            document.body.appendChild(button);

            button.addEventListener('click', () => {
                copyGoods()
                downloadImages()
            });

            // 复制商品信息
            function copyGoods() {
                const iframe = document.querySelector('iframe');
                if (iframe) {
                    const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
                    const detailTextElements = iframeDocument.querySelectorAll('.detailDesc--descText--1FMDTCm');
                    const priceElements = iframeDocument.querySelectorAll('.priceMod--soldPrice--20BWwRm');

                    if (detailTextElements.length > 0 && priceElements.length > 0) {
                        const detailText = detailTextElements[0].innerText;
                        const indexOfFirstNewLine = detailText.indexOf('\n');
                        let productName, productDescription;

                        if (indexOfFirstNewLine !== -1) {
                            productName = detailText.slice(0, indexOfFirstNewLine).trim();
                            productDescription = detailText.trim();
                        } else {
                            productName = detailText.trim();
                            productDescription = '';
                        }

                        // 裁剪商品标题到30个中文字以内
                        const maxTitleLength = 30;
                        productName = productName.slice(0, maxTitleLength);

                        const productPrice = priceElements[0].innerText.trim();

                        GM_setValue('productName', productName);
                        GM_setValue('productDescription', productDescription);
                        GM_setValue('productPrice', productPrice);

                        console.log('商品信息已存储');
                        console.log('商品名:', productName);
                        console.log('商品详情:', productDescription);
                        console.log('商品价格:', productPrice);

                        showToast('商品信息已复制');
                    } else {
                        console.error('未找到商品详情或价格元素');
                        showToast('未找到商品详情或价格元素', 'error');
                    }
                } else {
                    console.error('未找到iframe');
                    showToast('未找到iframe', 'error');
                }
            }

            // 下载商品图片
            function downloadImages () {
                const iframe = document.querySelector('iframe');
                if (iframe) {
                    const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
                    const detailTextElements = iframeDocument.querySelectorAll('.detailDesc--descText--1FMDTCm');
                    let productName = '';

                    if (detailTextElements.length > 0) {
                        const detailText = detailTextElements[0].innerText;
                        const indexOfFirstNewLine = detailText.indexOf('\n');
                        if (indexOfFirstNewLine !== -1) {
                            productName = detailText.slice(0, indexOfFirstNewLine).trim();
                        } else {
                            productName = detailText.trim();
                        }

                        const goofishImages = iframeDocument.querySelectorAll('img[quality="normal"]');
                        goofishImages.forEach((img, index) => {
                            setTimeout(() => {
                                let src = img.src.replace(/_webp$/, ''); // 去掉_webp并下载jpg格式
                                downloadImage(src, `${productName}/Goofish图-${index + 1}`);
                            }, index * 100); // 每个下载任务之间延迟100毫秒
                        });
                        showToast('图片下载开始');
                    } else {
                        console.error('未能找到商品描述文本元素');
                        showToast('未能找到商品描述文本元素', 'error');
                    }
                } else {
                    console.error('未找到iframe元素');
                    showToast('未找到iframe元素', 'error');
                }
            }
        }
    }

    // 闲鱼管家上架页面逻辑
    if (window.location.href.includes('https://www.goofish.pro')) {
        const button = document.createElement('button');
        button.innerText = '一键填充';
        button.style.position = 'fixed';
        button.style.bottom = '60px';
        button.style.right = '20px';
        button.style.zIndex = 1000;
        button.style.padding = '10px 20px';
        button.style.backgroundColor = '#28a745';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';

        document.body.appendChild(button);

        button.addEventListener('click', () => {
            // 检查当前页面是否是商品添加页面
            if (!window.location.href.includes('/sale/product/add')) {
                window.location.href = 'https://www.goofish.pro/sale/product/add?from=%2Fon-sale';
                setTimeout(() => fillProductInfo(), 1500);
                return;
            } else {
                fillProductInfo();
            }
        });
    }

    function fillProductInfo() {
        // 从 Tampermonkey 存储中获取商品信息
        const productName = GM_getValue('productName', '');
        const productDescription = GM_getValue('productDescription', '');
        const productPrice = parseFloat(GM_getValue('productPrice', '100')) - 0.1;

        if (!productName || !productDescription || isNaN(productPrice)) {
            showToast('请先去闲鱼详情页复制商品信息', 'warning');
            return;
        }

        console.log('读取到的商品名:', productName);
        console.log('读取到的商品详情:', productDescription);
        console.log('读取到的商品价格:', productPrice);

        // 选择类目
        const categoryElements = document.querySelectorAll('.release-history');
        if (categoryElements.length > 0) {
            categoryElements[0].click();
        } else {
            console.error('未找到类目选择元素');
            showToast('未找到类目选择元素', 'error');
        }

        setTimeout(() => {
            // 选择店铺
            const shopList = document.querySelectorAll('ul.auth-list li');
            if (shopList.length > 0) {
                shopList[0].click();
            } else {
                console.error('未找到目标店铺元素');
                showToast('未找到目标店铺元素', 'error');
            }

            setTimeout(() => {
                // 填充商品名
                const inputElements = document.querySelectorAll('.el-input__inner');
                if (inputElements.length > 2) {
                    inputElements[2].value = productName;
                    const event = new Event('input', { bubbles: true });
                    inputElements[2].dispatchEvent(event);
                } else {
                    console.error('未找到商品标题输入框');
                    showToast('未找到商品标题输入框', 'error');
                }

                // 填充商品详情
                const descriptionElements = document.querySelectorAll('.el-textarea__inner');
                if (descriptionElements.length > 0) {
                    descriptionElements[0].value = productDescription;
                    const event = new Event('input', { bubbles: true });
                    descriptionElements[0].dispatchEvent(event);
                } else {
                    console.error('未找到商品描述输入框');
                    showToast('未找到商品描述输入框', 'error');
                }

                // 填充价格
                if (inputElements.length > 4) {
                    inputElements[4].value = productPrice.toFixed(2);
                    const event = new Event('input', { bubbles: true });
                    inputElements[4].dispatchEvent(event);
                } else {
                    console.error('未找到价格输入框');
                    showToast('未找到价格输入框', 'error');
                }

                // 选择发布商品时机
                const radioElements = document.querySelectorAll('.el-radio');
                if (radioElements.length > 11) {
                    radioElements[11].click();
                } else {
                    console.error('未找到发布商品时机的单选框');
                    showToast('未找到发布商品时机的单选框', 'error');
                }

                // 清空商品信息
                GM_setValue('productName', '');
                GM_setValue('productDescription', '');
                GM_setValue('productPrice', '');

                showToast('商品信息已填充');
            }, 500);
        }, 500);
    }
})();

QingJ © 2025

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