Greasy Fork

WorkTile通知脚本

监听消息通知进行提示

目前为 2022-10-20 提交的版本。查看 最新版本

// ==UserScript==
// @name         WorkTile通知脚本
// @namespace    YXKJ.WorkTile.lzg
// @license      MIT
// @version      0.3
// @description  监听消息通知进行提示
// @author       Lzg
// @match        https://*.worktile.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=worktile.com
// @grant        GM_xmlhttpRequest
// @connect      qyapi.weixin.qq.com
// ==/UserScript==
(function() {
  var _a, _b;
  "use strict";
  var r = (_a = document.__monkeyWindow) != null ? _a : window;
  r.GM;
  r.unsafeWindow = (_b = r.unsafeWindow) != null ? _b : window;
  r.unsafeWindow;
  r.GM_info;
  r.GM_cookie;
  var b = (...e) => r.GM_xmlhttpRequest(...e);
  function getApiData(url, method = "GET", data) {
    let header = {};
    if (method === "POST") {
      header = { "Content-Type": "application/json" };
    }
    return new Promise((resolve) => b({
      header,
      method,
      url,
      data,
      onload: function(response) {
        if (response.status >= 200 && response.status < 400) {
          resolve(response.responseText);
        } else {
          console.error(`Error getting ${url}:`, response.status, response.statusText, response.responseText);
          resolve();
        }
      },
      onerror: function(response) {
        console.error(`Error during GM.xmlHttpRequest to ${url}:`, response);
        resolve(response);
      }
    }));
  }
  const originUrl = window.location.origin;
  let timeStamp = new Date().getTime();
  const time = 5e3;
  var userinfo = false;
  var projectName = "";
  const tipsObj = { infodata: [] };
  let notificationPush = false;
  var setIntervalFunc = null;
  const WebHookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8cf1621a-d9f3-47cb-8a94-7692aee68f90";
  setIntervalFunc && clearInterval(setIntervalFunc);
  async function tesingLoginFuc() {
    var _a2, _b2, _c;
    try {
      const userData = await getApiData(originUrl + "/api/user/me?t=" + timeStamp);
      const userJsonData = userData && JSON.parse(userData);
      userinfo = (_b2 = (_a2 = userJsonData == null ? void 0 : userJsonData.data) == null ? void 0 : _a2.me) != null ? _b2 : false;
      const teamData = await getApiData(originUrl + "/api/team?t=" + timeStamp);
      const teamJsonData = teamData && JSON.parse(teamData);
      projectName = (_c = teamJsonData == null ? void 0 : teamJsonData.data) == null ? void 0 : _c.name;
      setIntervalFunc = setInterval(() => {
        if (userinfo) {
          handleNotificationData();
          timeStamp = new Date().getTime();
        } else {
          setIntervalFunc && clearInterval(setIntervalFunc);
        }
      }, time);
    } catch (error) {
      setIntervalFunc && clearInterval(setIntervalFunc);
    }
  }
  tesingLoginFuc();
  async function handleNotificationData() {
    var _a2, _b2;
    try {
      const data = await getApiData(originUrl + "/api/team/chats?t=" + timeStamp);
      const jsonData = data && JSON.parse(data);
      const infodata = [];
      let info = projectName + "\n\n";
      (_b2 = (_a2 = jsonData == null ? void 0 : jsonData.data) == null ? void 0 : _a2.sessions) == null ? void 0 : _b2.map((item) => {
        var _a3, _b3;
        if (item.unread) {
          infodata.push({ name: (_a3 = item == null ? void 0 : item.to) == null ? void 0 : _a3.display_name, unread: item.unread });
          info += ((_b3 = item == null ? void 0 : item.to) == null ? void 0 : _b3.display_name) + "\u53D1\u6765" + item.unread + "\u6761\u6D88\u606F\n";
        }
      });
      info += "\n\u8BF7\u6CE8\u610F\u67E5\u770B";
      if (!userinfo.mobile) {
        info += "\n@" + userinfo.display_name + "(\u8BF7\u7ED1\u5B9A\u624B\u673A\u53F7)";
      }
      if (infodata.length > 0) {
        infodata.map((item) => {
          if (!tipsObj.infodata.find((item1) => item.name === item1.name && item.unread === item1.unread)) {
            notificationPush = true;
          }
        });
        notificationPush = notificationPush ? notificationPush : tipsObj.infodata.length !== infodata.length;
      }
      tipsObj.infodata = infodata;
      if (notificationPush) {
        notificationPush = false;
        const key = "yxkj@" + userinfo.display_name + "@" + projectName + "notificationPush";
        if (getCookie(key) !== info) {
          setCookie(key, info);
          const webhook_data = { "msgtype": "text", "text": { "content": info, "mentioned_mobile_list": [userinfo.mobile] } };
          handleWebHook(JSON.stringify(webhook_data));
        }
      }
      if (jsonData.code !== 200) {
        setIntervalFunc && clearInterval(setIntervalFunc);
      }
    } catch (error) {
      setIntervalFunc && clearInterval(setIntervalFunc);
    }
  }
  function handleWebHook(data) {
    getApiData(WebHookUrl, "POST", data).catch(() => {
      setIntervalFunc && clearInterval(setIntervalFunc);
    });
  }
  function setCookie(name, value) {
    const hours = 8;
    let exp = new Date();
    exp.setTime(exp.getTime() + hours * 60 * 60 * 1e3);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  }
  function getCookie(name) {
    let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
      return unescape(arr[2]);
    else
      return null;
  }
})();