wfzh 发表于 2025-8-21 14:32:49

// @match 和 @connect 已经设置, 使用GM_xmlhttpRequest发送请求,调用接口

```js
// ==UserScript==
// @name      一键辅检(优化版)
// @namespace   kingsware
// @version   2.1
// @author      wfzh
// @match       *://10.11.5.105:18882/*
// @match       *://10.11.5.187:10082/*
// @connect   10.11.5.187:10082
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// ==/UserScript==

/*
1、修改match,改成你需要运行的站点
2、修改connect,改成你需要调用的接口地址
*/

(() => {
'use strict';

/* 1. 常量配置 */

      //参数设置
         const payload = {
            userIds: '13641225a0724418a05e3b2c6a3adc5e',
            title: 'RPA执行结果通知2',
            content: '会服公告下载流程执行完成'
      };

/* 2. 工具 */
const debounce = (fn, delay = 200) => {
    let t; return (...a) => { clearTimeout(t); t = setTimeout(() => fn(...a), delay); };
};

/* 3. 创建按钮 */
let btn = null;
function createBtn() {
    if (btn) return btn;
    btn = document.createElement('button');
    btn.textContent = '一键辅检';
    Object.assign(btn.style, {
      position: 'fixed',
      top: '600px',
      right: '30px',
      zIndex: 2147483647,
      padding: '6px 12px',
      background: '#ff5722',
      color: '#fff',
      border: 'none',
      borderRadius: '4px',
      fontSize: '14px',
      cursor: 'pointer'
    });
    document.body.appendChild(btn);

    /* 4. 点击事件 */
    btn.addEventListener('click', () => {
      GM_xmlhttpRequest({
                  method:'POST',
            url:   'http://10.11.5.187:10082/api/fmp/rpa/notice', // ← 换成你要调用的接口
            headers: { 'Content-Type': 'application/json' },
            data:    JSON.stringify(payload),
          onload(res)   {
                  console.log('onload1',   res);
                  console.log('接口返回:', res.responseText);
                  const json = JSON.parse(res.responseText); // 把字符串
                  const code = json?.code;                   // 取出 code
                  console.log('接口返回 code =', code);
                   },
            onerror(err){
                  console.log('onerror2',err);
                  },
            ontimeout()   {
                  console.log('ontimeout3');
               },
             onloadend()   {
                  console.log('onloadend4');   
               }
      

      });
    });
    return btn;
}

/* ---------- 5. 检测“欢迎登录”再显示按钮 ---------- */
function showIfNeeded() {
if (!document.body) return;
const show = /欢迎登录/i.test(document.body.innerText);
createBtn().style.display = show ? 'block' : 'none';
}

/* ---------- 6. 防抖 ---------- */
const run = debounce(showIfNeeded, 300);

/* ---------- 7. 入口:多种触发方式 ---------- */
// 1. 首屏
if (document.readyState === 'complete') {
run();
} else {
window.addEventListener('load', run, { once: true });
}

// 2. SPA 路由切换
['pushState', 'replaceState', 'popstate'].forEach(evt =>
window.addEventListener(evt.toLowerCase(), () => setTimeout(run, 0))
);

// 3. DOM 内容变化(应对异步渲染)
const ob = new MutationObserver(() => run());
ob.observe(document.body, { childList: true, subtree: true });


})();
```

王一之 发表于 2025-8-21 15:58:35

试试 @connect 10.11.5.187 不要端口呢?
页: [1]
查看完整版本: // @match 和 @connect 已经设置, 使用GM_xmlhttpRequest发送请求,调用接口