检索和修改 XMLHttpRequest 的内容

Retrieve and Modify content of an XMLHttpRequest(检索和修改 XMLHttpRequest 的内容)
本文介绍了检索和修改 XMLHttpRequest 的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a browser plugin for Firefox, Safari, Chrome that will intercept data on the page, run it against a regex and then if it matches - reformat it. I have this working on page load using:

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);

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

This is working for static pages, but for anything using ajax calls, it fails. I cannot find the right listener or figure out how to intercept the XMLHttpRequest.

我为 XMLHttpRequest 尝试了类似的事件侦听器,但没有成功.

I have tried similar event listeners for XMLHttpRequest with no luck.

XMLHttpRequest.addEventListener('load', meth.run, false);

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

I would like to either intercept the request and modify the content. Or find the target that was updated and scan it after the ajax call is finished.

更新:

我会接受无法完成的回答,但我需要一些支持数据来说明为什么无法完成.

I will accept an answer that says it cannot be done, but I will need some supporting data as to why it cannot be done.

推荐答案

比较脏但是你可以覆盖XMLHttpRequest.prototype.open.这是一个演示页面.由于您正在编写扩展程序,因此您必须将此代码放在页面上下文中:

Rather dirty but you can overwrite XMLHttpRequest.prototype.open. Here is a Demo page. Since you're writing an extension, you must put this code in page context:

(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);
    };
})();

在这之后你可以做任何我猜的事情.替换 instance.readystatechange,替换 instance.addEventListener,或者监听突变事件(虽然它们是 已弃用).

After this you can do anything I guess. Replace instance.readystatechange, replace instance.addEventListener, or listen to mutation events (although they are deprecated).

这篇关于检索和修改 XMLHttpRequest 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)