0

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({
frosty
  • 2,559
  • 8
  • 37
  • 73

1 Answers1

2

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);
  }
});
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • This nominally also works with *Violentmonkey* and it worked with Greasemonkey prior to the colossal mess that is Greasemonkey 4. – Brock Adams Aug 22 '18 at 05:43