gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:53 27 4
gpt4 key购买 nike

我通过更改 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com