javascript - 扩展如何监听 WebSocket? (如果 WebSocket 在 iframe 中怎么办?)

标签 javascript google-chrome-extension websocket firefox-addon-webextensions intercept

我正在编写一个需要监听浏览器和服务器之间通信的 Firefox 扩展。对于 HTTP 通信,我在后台脚本中使用 webRequest 库。但是根据this Mozilla bug report ,我不能使用 webRequest 拦截 WebSocket 消息。我的扩展如何监听 WebSocket 通信?

更新:

我已经 injected我的WebSocket wrapper ,但它从未被调用过!我正在尝试收听的 WebSocket 位于 iframe 中。这重要吗?

最佳答案

监听WebSocket通信的唯一方法是inject一个WebSocket wrapper进入网站的代码并让它向您转发消息。您的代码应如下所示:

list .json

{
  ...
  "content_scripts": [
    {
      "matches": [
        "<website-url>",    
      ],
      "js": ["content/syringe.js"]
    }
  ],
  "web_accessible_resources": ["lib/socket-sniffer.js"]
}

内容/注入(inject)器.js

var s = document.createElement('script');
s.src = browser.runtime.getURL('lib/socket-sniffer.js');
s.onload = function() {
    this.remove();
};

lib/socket-sniffer.js

(function() {
  var OrigWebSocket = window.WebSocket;
  var callWebSocket = OrigWebSocket.apply.bind(OrigWebSocket);
  var wsAddListener = OrigWebSocket.prototype.addEventListener;
  wsAddListener = wsAddListener.call.bind(wsAddListener);
  window.WebSocket = function WebSocket(url, protocols) {
    var ws;
    if (!(this instanceof WebSocket)) {
      // Called without 'new' (browsers will throw an error).
      ws = callWebSocket(this, arguments);
    } else if (arguments.length === 1) {
      ws = new OrigWebSocket(url);
    } else if (arguments.length >= 2) {
      ws = new OrigWebSocket(url, protocols);
    } else { // No arguments (browsers will throw an error)
      ws = new OrigWebSocket();
    }

    wsAddListener(ws, 'message', function(event) {
      console.log("Received:", event);
    });
    return ws;
  }.bind();
  window.WebSocket.prototype = OrigWebSocket.prototype;
  window.WebSocket.prototype.constructor = window.WebSocket;

  var wsSend = OrigWebSocket.prototype.send;
  wsSend = wsSend.apply.bind(wsSend);
  OrigWebSocket.prototype.send = function(data) {
    console.log("Sent:", data);
    return wsSend(this, arguments);
  };
})();

(document.head || document.documentElement).appendChild(s);

上述代码的所有功劳显然归功于上面链接的海报。为了方便起见,我只是将 tt 复制到这里。

更新:

是的,WebSocket 在 iframe 中确实很重要!默认情况下,扩展仅加载在最顶层的框架中。要将其加载到 iframe,您必须将 "all_frames": true 添加到您的 manifest.json:

list .json

{
  ...
  "content_scripts": [
    {
      "matches": [
        "<website-url>",    
      ],
      "all_frames": true,      
      "js": ["content/syringe.js"]
    }
  ],
  "web_accessible_resources": ["lib/socket-sniffer.js"]
}

如果你想阅读更多,这里是 the documentation对于 all_frames

关于javascript - 扩展如何监听 WebSocket? (如果 WebSocket 在 iframe 中怎么办?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62798510/

相关文章:

javascript - 解析数据库作业未从 http 请求获取所有结果

Javascript:编辑表单字段的值?

c# - 如何通过 WebSocket4Net 库使用代理

javascript - 正则表达式 : Convert a numeric string from camelCase to regular

javascript - 按钮中的闪烁文本

node.js - google chrome 扩展中远程 Node 服务器上的 socket.io

javascript - 同步多个 Socket.io 客户端

无法查看recv返回的特定字符

javascript - 将过滤器添加到 Extjs 网格

google-chrome - 在 Firefox/Chrome 扩展程序中使用 Java/Python 库