拦截在Chrome AJAX请求为iOS?Chrome、AJAX、iOS

2023-09-10 17:34:19 作者:带刀爷们儿

我拦截在我的网站的AJAX请求通过修改 XMLHtt prequest.prototype 打开发送的方法。这种方法的工作没有在所有我测试浏览器的任何麻烦。但是,当涉及到Chrome浏览器的iOS(iPhone)的code的最奇怪的错误:它喜欢它不断地激发了code我在原型改变(最后崩溃,很明显)

I intercept AJAX requests in my site by altering the XMLHttpRequest.prototype open and send methods. This method worked without any troubles in all the browsers I tested. However, when it comes to Chrome for iOS (iPhone) the code has the weirdest bug: it's like it continuously fire the code I changed in the prototype (ending up crashing, obviously).

下面是我在做什么超小例子:

Here's a super-minimal example of what I am doing:

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正是这样做的,你可以与你的Chrome浏览器在iOS上访问: 演示

根据这个回答中,code我使用(基本上是一样的一个OP这个问题的答案将使用)是安全的,不应该有任何理由担心。而且,事实上,Chrome浏览器的iOS是行为古怪的浏览器。

According to this answer, the code I'm using (essentially the same as the one OP in that answer is going to use) is safe and there should be no reason to worry. And, as a matter of fact, Chrome for iOS is the only browser which behaves weirdly.

这是我发疯了两天,任何建议或解决方法AP preciated。

This has been driving me nuts for two days, any suggestion or workaround appreciated.

推荐答案

这是XMLHtt prequest拦截code在大多数浏览器工作:

How to Intercept AJAX requests on Chrome for iOS

This is XMLHttpRequest interception code that works on most browsers:

(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();

有一个在Chrome浏览器适用于iOS的一个问题。它已被提出并在下面研究。我将提供一个解释重复的open()所说的错误,示范,和解决方法。

There is a problem in Chrome for iOS. It has been raised and investigated below. I will provide an explanation for the "repeated open() calls" bug, a demonstration, and a workaround.

修改原型XMLHtt prequest使得它在Chrome浏览器连续火的iOS

疯狂的事情我们发现开发了New Relic的浏览器

Chrome浏览器在iOS可能会吃你的全局变量!

从最后一个引用:

在页面加载,Chrome浏览器提出了两个异步请求的服务,   这是presumably本地运行。通过它的URL的声音   请求,这些服务使用,以确保在页面的安全   您正在访问。

多设备无缝连接 Chrome for iOS试用

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正在试图访问(演示)的截图:

And here is a screenshot of one such local URL Chrome is trying to access (Demo):

Chrome浏览器调用 XMLHtt prequest.open()定期它自己。这些重复调用拦截code不是由C本身拦截$ C $所致;它们所造成的无关的和重复的的从Chrome浏览器调用。我已经确定了两个这样的网址。有可能是别人。

Chrome calls XMLHttpRequest.open() periodically on it's own. These repeated calls to the interception code are not caused by the interception code itself; they are caused by unrelated and repeated calls from the Chrome browser. I've identified two such URLs. There may be others.

/ chromeforiossecurity / B86 ... 98D / 的https:// localhost:0程序/ chromecheckurl

从我的研究,这种解决方法使得XMLHtt prequest code截取的工作在Chrome浏览器的iOS。看到这个 JSBin 的测试演示。这将展示刚刚的如何的这些重复的电话打进来约了。从本质上讲,拦截code应该忽略使用Chrome浏览器的URL。

From my research, this workaround makes the XMLHttpRequest code interception work on Chrome for iOS. See this JSBin testing demo. It will demonstrate just how these repeated calls come about too. Essentially, the interception code should ignore URLs used by Chrome.

(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()呼吁在Chrome漏洞为iOS和解决方法。

This is my best attempt at an explanation about this "repeated open() calls" bug on Chrome for iOS, and a workaround.