Wednesday, March 30, 2016

Using javascript to save XHR file

Leave a Comment

I would like to save an JSON file which is requested on an after-login page with a POST request. Because there is no API available, I would like to download the response of an script called when loading the page as JSON file using Greasemonkey.

The page is like domain.com/map, which calls domain.com/map/script during the load. The response of domain.com/map/script is the actual response I would like to save.

1 Answers

Answers 1

full demo code below:

(function (fetch, console) {        fetch('https://api.stackexchange.com/2.2/questions/36132760?site=stackoverflow')          .then(res => res.json())          .then(data => console.save(data));        console.save = function (data, filename) {                if (!data) {                  console.error('Console.save: No data')                  return;              }                if (!filename) filename = 'console.json'                if (typeof data === "object") {                  data = JSON.stringify(data, undefined, 4);              }                var blob = new Blob([data], {type: 'text/json'}),                  e    = document.createEvent('MouseEvents'),                  a    = document.createElement('a');                a.download = filename;              a.href = window.URL.createObjectURL(blob);              a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':');              e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);              a.dispatchEvent(e);      };    }).call(this, window.fetch, window.console || {});

checkout devtools-snippets#console-save

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment