出于开发目的在 Mozilla Firefox 中禁用 CORS:Tampermonkey 中的本地 Websocket 连接

Lio*_*ion 6 javascript firefox cors socket.io tampermonkey

我有一个 Tampermonkey 插件使用 Websockets 联系我的本地开发服务器。这不起作用并抛出以下错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://127.0.0.1:3000/socket.io/?EIO=3&transport=polling&t=N9Bvvb3. (Reason: CORS request did not succeed).
Run Code Online (Sandbox Code Playgroud)

所以我尝试了通过CORS搜索找到的前三个插件:

它们似乎都不起作用。我怎样才能解决这个问题?

我了解 CORS 背后的安全思想。由于我的软件面向开发人员并且仅在本地使用,因此我不需要它。因此,我正在寻找一种简单的解决方案,以允许我的 tampermonkey 脚本在访问https://example.com等网页时连接到本地主机。

我还在服务器端尝试了不同的方法来使 CORS 像这样这样正常工作,但仍然收到错误。

服务器

let app = express();
app.use(express.static(path.resolve(`${__dirname}/../dist`)));
const certDir = "certs";
let httpOpts = {
  key: fs.readFileSync(`${certDir}/localhost.key`),
  cert: fs.readFileSync(`${certDir}/localhost.crt`),
  requestCert: false,
  rejectUnauthorized: false
};
var http = require("https").createServer(httpOpts, app);
var io = require("socket.io")(http);
Run Code Online (Sandbox Code Playgroud)

Tampermonkey 客户端脚本

const localDevUrl = "https://127.0.0.1:3000";
this.socket = io(localDevUrl, { secure: true });
Run Code Online (Sandbox Code Playgroud)