Retrieve and Modify content of an 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 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检索和修改 XMLHttpRequest 的内容
基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01