Greasy Fork

来自缓存

Greasy Fork is available in English.

填充颜色分类

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

当前为 2024-08-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         填充颜色分类
// @namespace    none
// @version      0.1.2
// @description  商品编辑批量填充颜色分类
// @author       鹿秋夏
// @include      https://sell.publish.tmall.com/tmall/publish.htm?*
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  // 包含指定字符串时执行代码
  if (window.location.href.includes('https://sell.publish.tmall.com/tmall/publish.htm?')) {

    // 每隔500ms检测页面是否加载了按钮
    var checkButtonInterval = setInterval(function() {
      var customButton = document.getElementById('custom-button');

      if (!customButton) {
        // 等待指定元素加载完毕后执行代码
        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 customButton = document.createElement('button');
            customButton.id = 'custom-button';
            customButton.textContent = '填充颜色分类';
            customButton.style.borderRadius = '5px'; // 圆角
            customButton.style.width = '100px'; // 宽度
            customButton.style.height = '32px'; // 高度
            customButton.style.marginLeft = '-360px'; // 左边距
            customButton.style.backgroundColor = 'rgb(234, 244, 253)'; // 背景颜色
            customButton.style.color = 'rgb(76, 148, 253)'; // 字体颜色
            customButton.style.outline = 'none'; // 去除焦点样式
            customButton.style.border = '1px solid rgb(69, 143, 236)'; // 边框样式
            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('//div[@id="struct-p-1627207"]//span[@class="next-btn-helper" and text()="新增规格项"]', 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);
                        inputElement.focus();
                        inputElement.blur();
                      });
                    });
                  });
                })
                .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();
        });
      } else {
        // 清除所有重复的按钮,只保留一个
        var buttons = document.querySelectorAll('#custom-button');
        if (buttons.length > 1) {
          for (var i = 1; i < buttons.length; i++) {
            buttons[i].parentNode.removeChild(buttons[i]);
          }
        }
      }
    }, 500);
  }
})();