拦截 AJAX 对 Chrome 的 iOS 请求?
Intercepting AJAX requests on Chrome for iOS?
我通过更改 XMLHttpRequest.prototype
open
和 send
方法拦截我网站中的 AJAX 请求。这种方法在我测试的所有浏览器中都没有任何问题。然而,当谈到 Chrome for iOS (iPhone) 时,代码有一个最奇怪的错误:它就像它不断触发我在原型中更改的代码(显然最终崩溃) .
这是我正在做的一个超级简单的例子:
var open = XMLHttpRequest.prototype.open; // Caching the original
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
alert('open'); // Here is my code
open.call(this, method, url, async, user, pass); // Calling the original
};
我组装了一个小 JSBin,您可以在 iOS 上使用 Chrome 访问:Demo
根据 this 的回答,我正在使用的代码(基本上与该回答中将要使用的一个 OP 相同)是安全的,没有理由担心。而且,事实上,Chrome for iOS 是唯一行为怪异的浏览器。
这让我抓狂了两天,欢迎任何建议或解决方法。
如何拦截 Chrome 上对 iOS
的 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 中存在问题。它已被提出并在下面进行了调查。我将提供 "repeated open()
calls" 错误的解释、演示和解决方法。
来自上次参考:
On page load, Chrome makes two asynchronous requests to services that
it is presumably running locally. By the sound of the URLs it’s
requesting, these services are used to ensure the safety of the page
you are accessing.
这是一个这样的本地 URL Chrome 正在尝试访问 (Demo) 的屏幕截图:
Chrome 定期自行调用 XMLHttpRequest.open()
。这些对拦截代码的重复调用并不是拦截代码本身造成的;它们是由来自 Chrome 浏览器的 不相关且重复的 调用引起的。我已经确定了两个这样的 URL。可能还有其他人。
- /chromeforiossecurity/b86 ... 98d/
- https://localhost:0/chromecheckurl
根据我的研究,此解决方法使 XMLHttpRequest 代码拦截 工作 在 Chrome 上 iOS。请参阅此 JSBin 测试演示。它将仅演示 如何 这些重复调用也是如此。本质上,拦截代码应该忽略 Chrome 使用的 URLs。
(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();
这是我对 Chrome 上针对 iOS 的 "repeated open()
calls" 错误的解释以及解决方法的最佳尝试。
我通过更改 XMLHttpRequest.prototype
open
和 send
方法拦截我网站中的 AJAX 请求。这种方法在我测试的所有浏览器中都没有任何问题。然而,当谈到 Chrome for iOS (iPhone) 时,代码有一个最奇怪的错误:它就像它不断触发我在原型中更改的代码(显然最终崩溃) .
这是我正在做的一个超级简单的例子:
var open = XMLHttpRequest.prototype.open; // Caching the original
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
alert('open'); // Here is my code
open.call(this, method, url, async, user, pass); // Calling the original
};
我组装了一个小 JSBin,您可以在 iOS 上使用 Chrome 访问:Demo
根据 this 的回答,我正在使用的代码(基本上与该回答中将要使用的一个 OP 相同)是安全的,没有理由担心。而且,事实上,Chrome for iOS 是唯一行为怪异的浏览器。
这让我抓狂了两天,欢迎任何建议或解决方法。
如何拦截 Chrome 上对 iOS
的 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 中存在问题。它已被提出并在下面进行了调查。我将提供 "repeated open()
calls" 错误的解释、演示和解决方法。
来自上次参考:
On page load, Chrome makes two asynchronous requests to services that it is presumably running locally. By the sound of the URLs it’s requesting, these services are used to ensure the safety of the page you are accessing.
这是一个这样的本地 URL Chrome 正在尝试访问 (Demo) 的屏幕截图:
Chrome 定期自行调用 XMLHttpRequest.open()
。这些对拦截代码的重复调用并不是拦截代码本身造成的;它们是由来自 Chrome 浏览器的 不相关且重复的 调用引起的。我已经确定了两个这样的 URL。可能还有其他人。
- /chromeforiossecurity/b86 ... 98d/
- https://localhost:0/chromecheckurl
根据我的研究,此解决方法使 XMLHttpRequest 代码拦截 工作 在 Chrome 上 iOS。请参阅此 JSBin 测试演示。它将仅演示 如何 这些重复调用也是如此。本质上,拦截代码应该忽略 Chrome 使用的 URLs。
(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();
这是我对 Chrome 上针对 iOS 的 "repeated open()
calls" 错误的解释以及解决方法的最佳尝试。