7

I have a page index.html which calls an external URL. This external URL is a JSON endpoint. In other words, I see two resources when I open DevTools (with F12): index.html and myJSONEndpoint.

I want to be able to grab that JSON each time I load index.html and do something with it.

Would Greasemonkey or Tampermonkey be able to achieve this?

Page example:

<!doctype html>
<html>

<head>
  <title>Weather</title>
  <script>
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var myObj = JSON.parse(this.responseText);
      }
    };
    xmlhttp.open("GET", "https://api.weather.gov/points/39.7456,-97.0892", true);
    xmlhttp.send();
  </script>
</head>

<body>
  <p>Page Loaded...</p>
</body>

</html>

When I load that page, two requests appear in DevTools. Base index page and the JSON request.

I want to grab the JSON content and push it onto the DOM. The User can then copy/paste from there.

double-beep
  • 5,031
  • 17
  • 33
  • 41
A. Gardner
  • 571
  • 2
  • 7
  • 17
  • Yes, Tampermonkey can do that. But this question is way to broad; you need to provide details and either a link to the page or a [mcve]. – Brock Adams Oct 30 '18 at 05:58
  • Added sample to my answer – A. Gardner Nov 01 '18 at 08:05
  • If you want to save a file, your options are limited. You can use either [Tampermonkey's GM_download function](https://tampermonkey.net/documentation.php#GM_download), send the data to your own webapp, or try the deprecated FileWriter API (Chrome only).. – Brock Adams Nov 02 '18 at 05:38
  • OK, so perhaps it's easier just to grab the JSON content and push onto the DOM? User can then copy/paste from there. I'm not precious about the downloading part. Create a new `
    ` then stick the contents in there? Struggling to see how I grab the URL and content of the JSON call. Could I be cheeky and ask for a sample script?
    – A. Gardner Nov 02 '18 at 06:08

0 Answers0