Greasy Fork

来自缓存

Greasy Fork is available in English.

填充颜色分类

商品编辑批量填充颜色分类

当前为 2023-11-29 提交的版本,查看 最新版本

// ==UserScript==
// @name         填充颜色分类
// @namespace    none
// @version      0.0.3
// @description  商品编辑批量填充颜色分类
// @author       鹿秋夏
// @include      /^https:\/\/sell\.publish\.tmall\.com\/tmall\/publish\.htm\?id=\d{12}$/
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  // 在页面加载完成后执行代码
  window.addEventListener('load', function() {
    // 等待指定元素加载完毕后执行代码
    var waitForElement = function(callback) {
      var targetElement = document.querySelector("div[class='front']");
      if (targetElement !== null) {
        callback(targetElement);
      } else {
        setTimeout(function() {
          waitForElement(callback);
        }, 100); // 等待0.1秒后重新尝试获取
      }
    };

    // 处理逻辑
    waitForElement(function(targetElement) {
      function addCustomButton() {
        var xpathExpression = "//div[@class='front']";
        var result = document.evaluate(xpathExpression, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        var targetElement = result.singleNodeValue;

        var customButton = document.createElement('button');
        customButton.textContent = '填充颜色分类';
        customButton.style.borderRadius = '5px'; // 圆角
        customButton.style.width = '100px'; // 宽度
        customButton.style.height = '32px'; // 高度
        customButton.style.marginLeft = '-165px'; // 左边距
        customButton.style.backgroundColor = 'rgb(234, 244, 253)'; // 背景颜色
        customButton.style.color = 'rgb(76, 148, 253)'; // 字体颜色
        customButton.style.outline = 'none'; // 去除焦点样式
        customButton.style.border = 'none'; // 去除边框样式
        customButton.style.boxShadow = 'none'; // 去除阴影样式
        customButton.addEventListener('click', function() {
          navigator.clipboard.readText()
            .then(text => {
              // 对剪贴板内容进行分割与过滤操作,得到处理后的列表
              const processedList = text.split('\n').filter(item => item.trim() !== ''); // 示例分割与过滤操作

              // 循环处理列表中的每个元素
              processedList.forEach((value, index) => {
                setTimeout(() => {
                  const addButton = document.evaluate('(//span[@class="next-btn-helper" and text()="新增规格项"])[last()]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                  addButton.click();

                  // 等待指定元素加载完毕
                  const waitForElement = function(callback) {
                    const inputElement = document.evaluate('//input[@placeholder="主色(必选)" and @value=""]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                    if (inputElement !== null) {
                      callback(inputElement);
                                        } else {
                                            setTimeout(function() {
                                                waitForElement(callback);
                                            }, 100); // 等待0.1秒后重新尝试获取
                                        }
                                    };

                                    // 处理当前元素
                                    waitForElement(function(inputElement) {
                                        inputElement.value = value;

                                        var event = document.createEvent('HTMLEvents');
                                        event.initEvent("input", true, true);
                                        inputElement.dispatchEvent(event);
                                    });
                                });
                            });
                        })
                        .catch(error => {
                            console.error(error);
                        });
                });

                // 鼠标悬停时修改鼠标指针和样式
                customButton.addEventListener('mouseenter', function() {
                    customButton.style.cursor = 'pointer';
                    customButton.style.backgroundColor = 'rgb(204, 214, 223)'; // 悬停时的背景颜色
                });

                // 鼠标离开时恢复原样式
                customButton.addEventListener('mouseleave', function() {
                    customButton.style.cursor = 'auto';
                    customButton.style.backgroundColor = 'rgb(234, 244, 253)';
                });

                targetElement.parentNode.insertBefore(customButton, targetElement.nextSibling);
            }

            addCustomButton();
        });
    });
})();