// ==UserScript== // @name gorgias ticket prod // @namespace gorgias // @version 3.3 // @description gorgias 增加基本信息 易仓补发 // @author jerry // @include https://y.gorgias.com/app/* // @include https://youniverse.feishu.cn/sheets/* // @require https://code.jquery.com/jquery-3.7.1.slim.min.js // @grant none // @license MIT // @downloadURL none // ==/UserScript== //扩展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( `
` ); 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文件中--------------------**/