gpt4 book ai didi

javascript - 如何在使用 GM_xmlhttpRequest 时查看其重定向到的网站的链接

转载 作者:行者123 更新时间:2023-12-03 01:17:01 26 4
gpt4 key购买 nike

假设您正在访问一个名为“www.yourWebsite.com”的网站,并且您正在 Tampermonkey 中使用用户脚本从另一个网站获取信息。在本例中,您使用的是 GM_xmlhttpRequest 函数。

当您使用 GM_xmlhttpRequest 函数转到“exampleWebsite.com”时,有时它会重定向到“exampleWebsite.com/partOne”,有时会重定向到“exampleWebsite.com/partTwo”。

您的目标是查明它是否重定向到“exampleWebsite.com/partOne”或“exampleWebsite.com/partTwo”。现在我一直在尝试使用 window.location.href 来找出它重定向到的位置,但我得到的只是我所在的网站,即“www.yourWebsite.com”。

我该如何解决这个问题?

     var GoToURL = "exampleWebsite.com"; 
GM_xmlhttpRequest({
method: "GET",
url: GoToURL,
onload: function(response) {

alert(window.location.href);

} //end of onload: function(response) {
}); //end of GM_xmlhttpRequest({

最佳答案

至少在 Tampermonkey 中,您可以检查响应的 finalUrl 属性来识别重定向通向的位置。例如,PHP 页面为

<?php
header('Location: https://www.google.com/');
?>

那么以下用户脚本将导致 https://www.google.com/ 被记录到控制台:

GM_xmlhttpRequest({
method: "GET",
url: GoToURL,
onload: function(response) {
console.log(response.finalUrl);
}
});

关于javascript - 如何在使用 GM_xmlhttpRequest 时查看其重定向到的网站的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51960139/

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