检索和修改XMLHttpRequest的内容

Geo*_*tte 7 javascript xmlhttprequest firefox-addon addeventlistener google-chrome-extension

我正在开发一个针对Firefox,Safari,Chrome的浏览器插件,它将拦截页面上的数据,针对正则表达式运行它,然后如果它匹配 - 重新格式化它.我有这个工作在页面加载使用:

var meth = {
  replaceInElement : function(element, find, replace) {
        // iterate over child nodes and replace
  },
  run : function(evt){
    // doc is the document that triggered "run" event
    if (!(evt.target.nodeName === "#document")) { return; }
    var doc = evt.target; // document that triggered "onload" event
    if (!(doc instanceof HTMLDocument)) { return; }
    if (!doc.location) { return; }

    // perform substitutions on the loaded document
    var find = /regex/gi

    meth.replaceInElement(doc.body, find, function(match) {
        var new_content;
        //do stuff
        return new_content;
    });

    //content.document.addEventListener('DOMNodeInserted', ezcall.node_inserted, false);
  }
}

window.addEventListener("load", meth.run, false);
Run Code Online (Sandbox Code Playgroud)

这适用于静态页面,但对于使用ajax调用的任何内容,它都会失败.我找不到合适的监听器或弄清楚如何拦截XMLHttpRequest.

我已经为XMLHttpRequest尝试过类似的事件监听器而没有运气.

XMLHttpRequest.addEventListener('load', meth.run, false);
Run Code Online (Sandbox Code Playgroud)

我想拦截请求并修改内容.或者找到已更新的目标并在ajax调用完成后扫描它.

更新:

我会接受一个说不能做的答案,但我需要一些支持数据来说明为什么不能这样做.

gbl*_*zex 10

相当脏,但我认为你可以覆盖,XMLHttpRequest.prototype.open如果你只需要这个工作FF和WebKit.这是一个演示页面.

(function() {
    // save reference to the native method
    var oldOpen = XMLHttpRequest.prototype.open;
    // overwrite open with our own function
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
        // intercept readyState changes
        this.addEventListener("readystatechange", function() {
            // your code goes here...
            console.log("Interception :) " + this.readyState);
        }, false);
        // finally call the original open method
        oldOpen.call(this, method, url, async, user, pass);
    };
})();
Run Code Online (Sandbox Code Playgroud)

在此之后你可以做任何我猜的事情.替换instance.readystatechange,替换instance.addEventListener或侦听突变事件(尽管它们已被弃用).