javascript - 在 iOS 版 Chrome 上拦截 AJAX 请求?

标签 javascript ios ajax chrome-ios

我通过更改 XMLHttpRequest.prototype opensend 方法拦截我站点中的 AJAX 请求。这种方法在我测试的所有浏览器中都没有任何问题。然而,当谈到 Chrome for iOS (iPhone) 时,代码有一个最奇怪的错误:它就像它不断触发我在原型(prototype)中更改的代码(显然最终崩溃)。

这是我正在做的一个极简示例:

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 相同)是安全的,没有理由担心。而且,事实上,iOS 版 Chrome 是唯一行为怪异的浏览器。

这让我抓狂了两天,感谢任何建议或解决方法。

最佳答案

如何拦截 Chrome for 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 存在问题。它已被提出并在下面进行了调查。我将提供对“重复 open() 调用”错误的解释、演示和解决方法。

来自上次引用:

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.

这是 Chrome 尝试访问的一个这样的本地 URL 的屏幕截图 (Demo):

Repeated Chrome calls

Chrome 会定期自行调用 XMLHttpRequest.open()。这些对拦截代码的重复调用并不是拦截代码本身造成的;它们是由来自 Chrome 浏览器的不相关和重复调用引起的。我已经确定了两个这样的 URL。可能还有其他人。

根据我的研究,此变通方法使 XMLHttpRequest 代码拦截在 iOS 版 Chrome 上有效。看这个JSBin测试演示。它也将展示这些重复调用是如何产生的。本质上,拦截代码应该忽略 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();

这是我对 iOS 版 Chrome 上的“重复 open() 调用”错误的解释以及解决方法的最佳尝试。

关于javascript - 在 iOS 版 Chrome 上拦截 AJAX 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29310298/

相关文章:

ios - swift:我的字典中的应用内购买价格

ios - 为什么 UITableView 在从远程服务器加载更多行时自动调整行高 "scrolls up and then down"?

javascript - 在此 JS slider 中的图像下方添加文本

javascript - 在容器内显示的 CSS 幻灯片。如何?

javascript - 检查样式表是否加载然后渲染元素

ios - 如何在收到本地通知数据后创建传递给使用 objective-c 的主视图 Controller ?

javascript - jQuery Ajax 请求 (CORS) 返回未定义

javascript - 如何格式化 JSON 响应中的时间戳?

java - 如果 jquery ajax 调用出错应重定向到其他 jsp 页面

javascript - 如何设置apollo客户端:check in react?