我试图在我的铬扩展中使用socket.io。我已经安装了快速服务器,它已经启动并完美运行,包括socket.io,如下所示:
const express = require("express");
const cors = require("cors");
const http = require("http");
const { Server } = require("socket.io");
const app = express();
app.use(cors());
const httpServer = http.createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "*",
},
});
io.on('connection', (socket) => {
//all of our things we are going to use in sockets, different pipelines
console.log("User connected: " + socket.id);
socket.on("Alert", (data) => {
console.log(data);
})
});
我尝试将socket.io添加到清单content_scripts js
中,如下所示:
"content_scripts": [
{
"js": [
"socket.io.4.5.1.js"
]
}
]
content.js
文件包括以下代码:
const socket = io('ws://localhost:3001');
socket.on('connection');
在完成上述设置之后,当我尝试加载chrome扩展时,它将显示Uncaught ReferenceError: io is not defined
在content.js中的一个错误。除此之外,我还在我的Uncaught SyntaxError: Unexpected token 'export'
文件中得到了另一个socket.io错误。
我试着在社区里问过问题,但没能解决这个问题。我从wOxxOm here的答案中得到的一个假设是,如果需要使用MV3,如果我不能使用socket.io,那么最好的方法是什么,因为我需要多次与服务器通信,并且由于页面内容的频繁更新而不能使用API。
发布于 2022-08-08 18:54:25
您可以在服务工作人员本身中使用socketio,要使服务工作者继续工作,您需要打开一个窗口线程,您可以使用chrome.windows.create函数创建它,请参阅有关javascript文档的更多信息。我们正以同样的方式使用它,这种方法对我们是有效的。
此外,如果socket io使用窗口或文档,则使用jsdom使窗口和文档可用。
https://stackoverflow.com/questions/73266900
复制相似问题