检索和修改XMLHttpRequest的内容 [英] Retrieve and Modify content of an XMLHttpRequest
问题描述
var meth = {
replaceInElement:function(element,find,replace ){
//迭代子节点并替换
},
run:function(evt){
// doc是触发run事件的文档
if(!(evt.target.nodeName ===#document)){return; }
var doc = evt.target; //触发onload事件的文档
if(!(doc instanceof HTMLDocument)){return; }
if(!doc.location){return; }
//在已经加载的文档中执行替换
var find = / regex / gi
meth.replaceInElement(doc.body,find,function(match) {
var new_content;
//做东西
return new_content;
});
//content.document.addEventListener('DOMNodeInserted',ezcall.node_inserted,false);
window.addEventListener(load,meth.run,false);
这适用于静态页面,但是对于使用ajax调用的任何东西,它都会失败。我无法找到正确的侦听器,也无法弄清楚如何拦截XMLHttpRequest。
我试过XMLHttpRequest类似的事件侦听器,没有运气。
XMLHttpRequest.addEventListener('load',meth.run,false);
我想拦截请求并修改内容。或者找到已更新的目标,并在ajax调用完成后对其进行扫描。
更新:
将接受一个答案,说这是不能做的,但我将需要一些支持的数据,为什么它不能做。 解决方案
相当脏,但我认为你可以覆盖 XMLHttpRequest.prototype.open
,如果你只需要这个工作在FF和WebKit。这是 演示 页面。
(function(){
//保存对本地方法的引用
var oldOpen = XMLHttpRequest.prototype.open;
//用自己的函数覆盖open
XMLHttpRequest.prototype.open = function(method,url,async,user,pass){
//拦截readyState更改
this.addEventListener(readystatechange,函数(){
//你的代码在这里...
console.log(Interception :)+ this.readyState);
},false);
//终于调用原来的打开方法
oldOpen.call(this,method,url,async,user,pass);
};
})();
在此之后,我可以做任何事情。替换 instance.readystatechange
,替换 instance.addEventListener
,或者听取突变事件(虽然它们是 deprecated )。
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);
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.
I have tried similar event listeners for XMLHttpRequest with no luck.
XMLHttpRequest.addEventListener('load', meth.run, false);
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.
UPDATE:
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.
Rather dirty but I think you can overwrite XMLHttpRequest.prototype.open
if you only need this to work on FF and WebKit. Here is a Demo page.
(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);
};
})();
After this you can do anything I guess. Replace instance.readystatechange
, replace instance.addEventListener
, or listen to mutation events (although they are deprecated).
这篇关于检索和修改XMLHttpRequest的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!