上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖
楼主: wfzh - 

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

[复制链接]

该用户从未签到

1

主题

1

回帖

4

积分

助理工程师

积分
4
发表于 2025-8-21 14:32:49 | 显示全部楼层 | 阅读模式
悬赏2油猫币未解决
// ==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 });

})();

  • TA的每日心情
    郁闷
    2025-7-22 00:22
  • 签到天数: 221 天

    [LV.7]常住居民III

    311

    主题

    4909

    回帖

    4576

    积分

    管理员

    积分
    4576

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2025-8-21 15:58:35 | 显示全部楼层
    试试 @connect 10.11.5.187 不要端口呢?
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表