如何在 iOS 版 Chrome 上拦截 AJAX 请求
这是适用于大多数浏览器的 XMLHttpRequest 拦截代码:
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
// Intercept the open request here
alert("Intercepted: " + url);
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://google.com",true);
xmlhttp.send();
iOS 版 Chrome 存在问题。下面对此进行了提出和研究。我将对“重复”进行解释open()
调用”错误、演示和解决方法。
修改 XMLHttpRequest 的原型使其在 iOS 版 Chrome 中持续触发 http://code.google.com/p/chromium/issues/detail?id=471591
开发新的 Relic 浏览器时我们发现的疯狂事情 http://blog.newrelic.com/2014/04/11/crazy-things-found-developing-new-relic-browser/
iOS 上的 Chrome 可能会吃掉你的全局变量! http://jamesallardice.com/chrome-on-ios-might-eat-your-global-variable/
从最后一个参考:
页面加载时,Chrome 向服务发出两个异步请求
它大概是在本地运行的。从 URL 的声音来看,它是
请求时,这些服务用于确保页面的安全
您正在访问。
这是 Chrome 尝试访问的一个此类本地 URL 的屏幕截图(Demo http://jsbin.com/rekozo/1/):
Chrome 通话XMLHttpRequest.open()
定期自行。这些对拦截代码的重复调用并不是由拦截代码本身引起的;它们是由不相关且重复的来自 Chrome 浏览器的调用。我已经识别出两个这样的 URL。可能还有其他人。
- /chromeforiossecurity/b86 ... 98d/
- https://localhost:0/chromecheckurl https://localhost:0/chromecheckurl
根据我的研究,这种解决方法使得 XMLHttpRequest 代码被拦截work在 iOS 版 Chrome 上。看到这个JSBin http://jsbin.com/rekozo/1/测试演示。它将展示刚刚how这些重复的电话也会发生。本质上,拦截代码应该忽略 Chrome 使用的 URL。
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
var d1 = document.getElementById('urls');
// Avoid intercepting Chrome on iOS local security check urls
if(url.indexOf("/chromecheckurl") < 0 && url.indexOf("/chrome") !== 0) {
// These are what we want to intercept
d1.insertAdjacentHTML('beforeend', '<b>'+url+'</b><br/>');
} else {
// These are the internal Chrome requests - we can ignore them
d1.insertAdjacentHTML('beforeend', '<i>'+url+'</i><br/>');
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://google.com",true);
xmlhttp.send();
这是我对这个“重复”的解释的最好尝试open()
iOS 版 Chrome 上的“调用”错误以及解决方法。