【发布时间】:2023-03-15 12:36:02
【问题描述】:
我正在尝试使用 websocket 的 onMessage 函数,以便当它收到“go”这个词时,它将通过当前选项卡的端口发布消息。当前选项卡 ID 有效,但我不断收到错误 Uncaught TypeError: Cannot read property 'postMessage' of undefined.
ws.onmessage = function (evt) {
if(evt.data == "connect"){
rpwd = "helloworld";
ports[curTabID].postMessage({text: rpwd});
}
};
【问题讨论】:
-
ports是如何构建、维护的,您从哪里获得curTabID? -1 目前信息不足;阅读this help article,并相应地编辑您的帖子。
标签: javascript google-chrome-extension websocket