我正在为 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 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)