Greasy Fork

Greasy Fork is available in English.

gorgias ticket prod

gorgias 增加基本信息 易仓补发

目前为 2023-04-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         gorgias ticket prod
// @namespace    gorgias
// @version      3.0
// @description  gorgias 增加基本信息 易仓补发
// @author       jerry
// @include      https://y.gorgias.com/app/*
// @include      https://youniverse.feishu.cn/sheets/*
// @require      http://code.jquery.com/jquery-3.3.1.min.js
// @grant        none
// @license      MIT
// ==/UserScript==

(function ($) {
  console.log("脚本执行了,版本3.0.0");

  var env = localStorage.getItem("env-info") || "prod"; // 默认生产环境
  console.log(env);

  const envMap = {
    prod: "https://ccm.youniverse.cc",
    test: "https://test.ccm.youniverse.cc",
    devEnv: "https://ccm-web-dev.oss-cn-shenzhen.aliyuncs.com",
  };

  $("body").append(
    `
      <!-- 实际脚本内容-通过script引入 代码 方便代码管理维护 -->
      <script src=${
        envMap[env] || "https://ccm.youniverse.cc"
      }/gorgiasScript/index.js></script>
      <script src=${
        envMap[env] || "https://ccm.youniverse.cc"
      }/gorgiasScript/warning.js></script>
      `
  );

  $.extend({ envMap });
  // 这个 script 是中转 文件 原则上不改变
})(jQuery);

//扩展jquery功能
(function ($) {
  console.log("基础数据脚本");
  if (
    window.location.href.indexOf("https://youniverse.feishu.cn/sheets/") === -1
  )
    return;
  const envMap = $["envMap"]; //从index.js里取值
  var env = localStorage.getItem("env-info") || "prod"; // 默认生产环境
  $("body").append(
    `
      <!-- 按钮-start -->
      <div id="ccm-Operation-data-sync-button">
      <img id="ccm-close-img" style="position: absolute;right: 10px;top: 10px; width: 60px; display: none" src="https://cdn.resource.youniverse.cc/ccm-app/yAh-5RbXCiOANm41pM8q-.png" />
      </div>
      <script type="module" crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">
      <!-- 按钮-end -->

      `
  );

  var ccmIframeContainer = $("#ccm-Operation-data-sync-button");
  var close = false;
  var prevSheetId = null;

  $.extend({
    getSheetTokenAndSheetId: function () {
      var url = window.location.pathname;
      var sheetToken = url.substring(url.lastIndexOf("sheets/") + 7).trim();
      const params = new URLSearchParams(window.location.search);
      const sheetId = params.get("sheet");

      return {
        sheetToken,
        sheetId: sheetId || "",
      };
    },
    // 创建 iframe
    operationDataSyncCreateIframe: function () {
      var iframe = document.createElement("iframe");
      // 取出环境
      var env = localStorage.getItem("env-info") || "prod";
      var envUrl = envMap?.[env];
      var token = $.getSheetTokenAndSheetId();
      iframe.setAttribute(
        "src",
        `${
          envUrl || "https://ccm.youniverse.cc"
        }/operation?lastUrl=operation&sheetToken=${token.sheetToken}&sheetId=${
          token.sheetId || ""
        }`
      );
      iframe.setAttribute("id", `ccm-grogias-iframe`);
      iframe.style.width = "110px";
      iframe.style.height = "32px";
      iframe.style.overflow = "hidden";
      iframe.style.border = "0";
      ccmIframeContainer.append(iframe);
      return this;
    },
  });

  $.fn.extend({
    // 初始化
    operationDataSync: (function () {
      let called = false;
      return function (again) {
        if (!again) {
          if (called) return; // 执行过不在执行
        }
        var _this = this;
        called = true;
        this.initCss()
          .css("position", "fixed")
          .css("top", "16px")
          .css("left", "505px")
          .css("z-index", "90")
          .css("overflow", "hidden")
          .css("background", "#ffffff")
          .css("transition-property", "width,height")
          .css("transition-duration", ".7s,0.7s")
          .css("border-radius", "4px");

        $("#ccm-close-img").hide();

        $("#ccm-close-img").on("click", function () {
          close = false;
          _this
            .initCss()
            .css("box-shadow", "none")
            .css("border", "0px solid rgba(255, 255, 255, 0)");
          $("#ccm-grogias-iframe").initCss();
          $("#ccm-grogias-iframe").attr("src", $("#ccm-grogias-iframe").attr("src"));
          $(this).hide();
        });

        window.addEventListener("message", (event) => {
          const { data } = event;
          const { type, data: dataMsg } = data;
          if (type === "operationLogin—open") {
            close = true;
            _this
              .css("width", "500px")
              .css("height", "90%")
              .css("box-shadow", "var(--shadow-s4-down)")
              .css("border", "1px solid var(--line-border-card)");
            $("#ccm-grogias-iframe").css("width", "500px").css("height", "90%");
            $("#ccm-close-img").show();
          }
          if (type === "operationLogin—close") {
            if (close) {
              close = false;
              _this
                .initCss()
                .css("box-shadow", "none")
                .css("border", "0px solid rgba(255, 255, 255, 0)");
              $("#ccm-grogias-iframe").initCss();
              $("#ccm-close-img").hide();
            }
          }
          if (type === "operationDataSync-start") {
            toastr["info"]("数据更新中, 请稍等。。。");
          }
          if (type === "operationDataSync-login") {
            location.reload();
            // toastr["info"]("登录完成,请点击数据更新更新数据");
          }
          if (type === "operationDataSync-closed") {
            // 关闭弹窗
            toastr.clear();
          }
          if (type === "operationDataSync-success") {
            toastr["success"]("数据更新完成");
          }
          if (type === "operationDataSync-err") {
            const { msg, code } = dataMsg;
            if (code) {
              return toastr["error"](`数据更新异常 msg:${msg} code:${code}`);
            }
            toastr["error"](`数据更新异常`);
          }
        });

        return this;
      };
    })(),

    initCss: function () {
      this.css("width", "110px").css("height", "32px");
      return this;
    },
  });

  let interval = setInterval(() => {
    const op = $(".note-title__input-and-star");
    const ifr = $("#ccm-grogias-iframe");
    // 初始化
    if (op.text() && op.text().match("运营基础数据表")) {
      $("#ccm-Operation-data-sync-button").operationDataSync();
      if (prevSheetId !== $.getSheetTokenAndSheetId().sheetId) {
        if (ifr.length === 1) {
          // 存在 刷新
          var iframeUrl = $("#ccm-grogias-iframe").attr("src");
          var newIframeUrl = iframeUrl.replace(
            `sheetId=${prevSheetId}`,
            `sheetId=${$.getSheetTokenAndSheetId().sheetId}`
          );
          $("#ccm-grogias-iframe").attr("src", newIframeUrl);
        } else {
          // 不存在 新建
          $.operationDataSyncCreateIframe();
        }
        prevSheetId = $.getSheetTokenAndSheetId().sheetId;
      }
    } else if (op.val()) {
      clearInterval(interval);
      interval = null;
    }
  }, 100);

  window.addEventListener("beforeunload", () => {
    clearInterval(interval);
    interval = null;
  });
})(jQuery);

/**------------------- 以上代码可放入单独的js文件中--------------------**/