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

[油猴脚本] GM_postMessage: 用户端-伺服器(连WebWorker) 的双向沟通

[复制链接]

该用户从未签到

4

主题

3

回帖

18

积分

助理工程师

积分
18
发表于 2025-11-24 23:33:02 | 显示全部楼层 | 阅读模式

本帖最后由 cyfung1031 于 2025-11-25 00:37 编辑

本帖最后由 cyfung1031 于 2025-11-25 00:23 编辑

原文: https://github.com/scriptscat/scriptcat/issues/883
脚本猫:https://scriptcat.org/zh-CN/script-show-page/4710
测试:
https://example.com/?GM_postMessage_server
https://example.com/?GM_postMessage_client

(这是用一个前台脚本做的示例)
(后台脚木的话要做一个前台一个后台,然后用同一个储存空间。请参考相关的资料说明)

// ==UserScript==
// @name         New Userscript X02O-1
// @namespace    https://docs.scriptcat.org/
// @version      0.1.1
// @description  try to take over the world!
// @author       You
// @match        https://*/*?GM_postMessage*
// @grant        GM.setValue
// @grant        GM_addValueChangeListener
// @require      https://scriptcat.org/lib/4710/0.1.0/GM_postMessage.js?sha384-U9kZ0JxuJEQFA7LMejrhTcel/fMoH/g434fOTpP0a/fBXxMnutQ7/L+WOiA1p9UT
// @noframes
// ==/UserScript==

// 使用模板字符串定义 Worker
const createWorker = (fn) => new Worker(URL.createObjectURL(new Blob([`(${fn})()`], { type: 'text/javascript' })));

// https://example.com/?GM_postMessage_server
// https://example.com/?GM_postMessage_client

if (location.href.includes("GM_postMessage_server")) {

    console.log('我是接收端 (Server)');

    // 使用
    const worker = createWorker(function () {
        console.log('我是工作端 (Server Worker)');
        self.addEventListener('message', (event) => {
            const { type, data } = event.data;
            if (type === 'sum') {
                const result = data.reduce((a, b) => a + b, 0);
                self.postMessage({ type: 'sum', result });
            }
            if (type === 'multiply') {
                const result = data.reduce((a, b) => a * b, 1);
                self.postMessage({ type: 'multiply', result });
            }
        });
    });

    worker.addEventListener('message', (event) => {
        console.log('[3] Web Worker 处理完成并传回结果:', event.data);
        GM_postMessage("worker-response", event.data);
    });

    GM_onMessage("worker-request", ({ message }) => {
        console.log('[2] 收到并转发要求至 Web Worker:', message);
        worker.postMessage({ type: message.type, data: message.data });
    });

} else if (location.href.includes("GM_postMessage_client")) {

    console.log('我是请求端 (Client)');

    GM_onMessage("worker-response", ({ message }) => {
        console.log('[4] 收到 Web Worker 结果:', message);
    });

    console.log('[1] 发送要求至Web Worker:');
    GM_postMessage("worker-request", { type: 'sum', data: [1, 2, 3, 4, 5] });
    GM_postMessage("worker-request", { type: 'multiply', data: [2, 3, 4] });

}
  • TA的每日心情
    难过
    昨天 00:20
  • 签到天数: 10 天

    [LV.3]偶尔看看II

    1

    主题

    7

    回帖

    13

    积分

    助理工程师

    积分
    13
    发表于 2025-12-5 20:54:09 | 显示全部楼层
    大佬,这个会有登录校验吗?
    回复

    使用道具 举报

    该用户从未签到

    4

    主题

    3

    回帖

    18

    积分

    助理工程师

    积分
    18
    发表于 2025-12-6 07:19:43 | 显示全部楼层
    subot 发表于 2025-12-5 20:54
    大佬,这个会有登录校验吗?

    为什么会有登录校验?
    回复

    使用道具 举报

  • TA的每日心情
    难过
    昨天 00:20
  • 签到天数: 10 天

    [LV.3]偶尔看看II

    1

    主题

    7

    回帖

    13

    积分

    助理工程师

    积分
    13
    发表于 2025-12-6 10:33:37 | 显示全部楼层
    作为接口公网部署的话,可能会有被攻击风险
    回复

    使用道具 举报

  • TA的每日心情
    开心
    昨天 10:18
  • 签到天数: 222 天

    [LV.7]常住居民III

    312

    主题

    5115

    回帖

    4718

    积分

    管理员

    积分
    4718

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

    发表于 2025-12-6 11:26:09 | 显示全部楼层
    subot 发表于 2025-12-5 20:54
    大佬,这个会有登录校验吗?

    我也没有理解你说的,这只是一个库,你是不是弄错帖子了?
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。
    回复

    使用道具 举报

  • TA的每日心情
    难过
    昨天 00:20
  • 签到天数: 10 天

    [LV.3]偶尔看看II

    1

    主题

    7

    回帖

    13

    积分

    助理工程师

    积分
    13
    发表于 6 天前 | 显示全部楼层
    王一之 发表于 2025-12-6 11:26
    我也没有理解你说的,这只是一个库,你是不是弄错帖子了?

    好吧 应该是我搞错了
    回复

    使用道具 举报

    发表回复

    本版积分规则

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