BTA Text

根据蓝湖剪切板生成Bta-Text 组件

Fra 01.02.2023. Se den seneste versjonen.

// ==UserScript==
// @name         BTA Text
// @namespace    *://lanhuapp.com/*
// @version      0.2
// @description  根据蓝湖剪切板生成Bta-Text 组件
// @author       Bajn
// @match        *://lanhuapp.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mozilla.org
// @grant        none
// @license MIT 
// ==/UserScript==

(function () {
  "use strict";

  const colorMap = {
    "#FFFFFF": "grey-white",
    "#FAFAFA": "grey-2",
    "#F5F5F5": "grey-3",
    "#F2F2F2": "grey-4",
    "#E0E0E0": "grey-5",
    "#C4C4C4": "grey-6",
    "#9E9E9E": "grey-7",
    "#757575": "grey-8",
    "#212121": "grey",
    "#000000": "grey-black",
    "#E6EEFF": "primary-2",
    "#CCDEFF": "primary-3",
    "#99BDFF": "primary-4",
    "#669CFF": "primary-5",
    "#337AFF": "primary-6",
    "#0055FF": "primary",
    "#0048D9": "primary-8",
    "#003399": "primary-9",
    "#002266": "primary-10",
    "#000C24": "primary-dark",
    "#FFFBFB": "error-light",
    "#FFECEE": "error-2",
    "#FFD9DC": "error-3",
    "#FFB3B9": "error-4",
    "#FF8D97": "error-5",
    "#FF6774": "error-6",
    "#FF4252": "error",
    "#E53B49": "error-8",
    "#992731": "error-9",
    "#661A20": "error-10",
    "#330D10": "error-dark",
    "#FAFEFC": "success-light",
    "#E6FAF0": "success-2",
    "#CCF5E2": "success-3",
    "#99ECC5": "success-4",
    "#66E3A8": "success-5",
    "#33DA8B": "success-6",
    "#00D16E": "success",
    "#00BB62": "success-8",
    "#007D42": "success-9",
    "#00532C": "success-10",
    "#002916": "success-dark",
    "#FFFDFA": "warn-1",
    "#FFF5E6": "warn-2",
    "#FFECCC": "warn-3",
    "#FFDA99": "warn-4",
    "#FFC766": "warn-5",
    "#FFB533": "warn-6",
    "#FFA300": "warn",
    "#E59200": "warn-8",
    "#996100": "warn-9",
    "#664100": "warn-10",
    "#332000": "warn-dark",
  };

  const fontFamily = {
    // 粗体
    "PingFangSC-Medium, PingFang SC": "--v-font-family-bold",
    // 数字字体
    "WEMONum-Bold, WEMONum": "--v-font-family-number",
    // 普通字体
    "PingFangSC-Regular, PingFang SC": "--v-font-family",
  };

  const BtaTextContent = "";
  let style = "";

  document.addEventListener("keydown", async function (e) {
    if (e.keyCode === 187) {
      style = await navigator.clipboard.readText();
      const styleObj = format(style);
      createProps(styleObj);
    }
  });
  function format(str) {
    const rows = str.replace(/;|px/gi, "").split("\n");
    const styleObj = {};
    rows.forEach((row) => {
      const sp = row.split(":");
      styleObj[sp[0].trim()] = sp[1].trim();
    });
    return styleObj;
  }

  function createProps(styleObj) {
    const props = {};
    Object.keys(styleObj).forEach((attr) => {
      const value = styleObj[attr];
      if (attr === "font-size") {
        props.size = `{${value}}`;
      }
      if (attr === "font-weight") {
        if (Number(value) >= 500) {
          props.bold = "";
        }
      }
      if (attr === "color") {
        props.color = `"${colorMap[value]}"`;
      }

      if (attr === "line-height") {
        const size = styleObj["font-size"];
        const isSingleLine = Number(value) / size <= 1.25;
        if (!isSingleLine) {
          props.multipleLines = "";
        }
      }
    });

    const propsStr = Object.keys(props).map((key) => {
      const value = props[key];
      if (value === "") {
        return `${key}`;
      } else {
        return `${key}=${props[key]}`;
      }
    });
    const content = document.querySelector(".item_one.item_content");
    const btaTextRes = `<BtaText ${propsStr.join(" ")} align="left" >${
      content?.innerText || "XXXXXXX"
    }</BtaText>`;
    console.log(btaTextRes);
    navigator.clipboard.writeText(btaTextRes).then((res) => {
      displayMessage("success", "BTA TEXT 复制成功。", 1500);
    });
  }

  function displayMessage(type, data, time) {
    var lunbo = document.createElement("div");

    if (type == "success") {
      lunbo.style.backgroundColor = "rgba(0, 209, 110, 0.9)";
    } else if (type == "error") {
      lunbo.style.backgroundColor = "#990000";
    } else if (type == "info") {
      lunbo.style.backgroundColor = " #e6b800";
    } else {
      console.log("入参type错误");
      return;
    }

    lunbo.id = "lunbo";
    lunbo.style.position = "fixed";
    lunbo.style.width = "200px";
    lunbo.style.height = "60px";
    lunbo.style.transform = "translate(-50%, -50%)";
    lunbo.style.zIndex = "999999";
    lunbo.style.left = "50%";
    lunbo.style.top = "25%";
    lunbo.style.color = "white";
    lunbo.style.fontSize = "16px";
    lunbo.style.borderRadius = "20px";
    lunbo.style.textAlign = "center";
    lunbo.style.lineHeight = "60px";

    if (document.getElementById("lunbo") == null) {
      document.body.appendChild(lunbo);
      lunbo.innerHTML = data;
      setTimeout(function () {
        document.body.removeChild(lunbo);
      }, time);
    }
  }
})();