How to see the link of the website it redirects to while using GM_xmlhttpRequest

421 views Asked by At

Imagine you are on a website called "www.yourWebsite.com", and you're using userscripts in Tampermonkey to get information from another website. In this case, you are using the GM_xmlhttpRequest function.

When you're using the GM_xmlhttpRequest function to go to "exampleWebsite.com", sometimes it redirects to "exampleWebsite.com/partOne", and sometimes it redirects to "exampleWebsite.com/partTwo".

Your goal is to find out whether it redirected to "exampleWebsite.com/partOne" or "exampleWebsite.com/partTwo". Now I've been trying to use window.location.href to find out where it redirected to, but all I've been getting is the website I'm on, which is "www.yourWebsite.com".

How I do fix this?

     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({
1

There are 1 answers

1
CertainPerformance On

In Tampermonkey, at least, you can check the finalUrl property of the response to identify where the redirect leads to. For example, with a PHP page of

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

then the following userscript will result in https://www.google.com/ being logged to the console:

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