Leveraging JavaScript to store XHR documents

My goal is to save a JSON file that is retrieved on a page after logging in with a POST request. Since there is no available API, I am looking to download the response of a script called during the page load as a JSON file using Greasemonkey.

The page I'm referring to is located at domain.com/map, which triggers domain.com/map/script during its loading:

<script type="text/javascript" charset="utf-8"> function open() {$.ajax{{ url: "/script", type: "POST", dataType: "json",), [...] }); }; function functionname(value){ [...] }; open();
. The response from domain.com/map/script contains the JSON data that I want to save (either locally or preferably using FTP).

Answer №1

Here is the complete demo code:

(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 || {});

For more information, visit devtools-snippets#console-save

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with knockout binding not functioning properly when using inline editing

Attempting to implement inline editing using knockout. Incorporated both 'span' and 'input' for the same field. When the span is clicked, it is hidden and the input is displayed. However, changes made in the input are not reflected on ...

Using Javascript, retrieve a custom attribute specific to a checkbox option

How can I access the custom attribute "mem_name" value? <input class="messageCheckbox" type="checkbox" value="3" mem_name='ABC' name="checkmember" > <input class="messageCheckbox" type="checkbox" value="1" mem_name='PQR' name ...

"Embedding a JSON object within a script tag in NextJS: A step-by-step

I've been working on configuring values within a Cookiebot initialization script tag in a NextJS project. The documentation suggests that I can set vendor properties by passing in a JSON object inside the script tag, like this (taken from this link): ...

What are some ways to handle JavaScript exceptions effectively?

Within my application, I am retrieving the content from a different domain's page. This content includes specific JavaScript that is executed alongside the page. However, there is an issue with the following line of code within the JavaScript: "docume ...

After changing the page, the Facebook JS SDK fails to function properly when using JQueryMobile

I'm facing an issue with my webapp that utilizes jQuery Mobile for full ajax navigation. I have initialized the Facebook SDK at the pageinit event in jQueryMobile (called on each page). jQuery(document).on('pageinit', function (event) { ...

Trying to update React and Electron, encountering the error message "global is not defined"

I have an Electron + React app that has not been updated in a couple of years, initially utilizing the following packages: "@rescripts/cli": "^0.0.13", "@rescripts/rescript-env": "^0.0.11", "electron": &quo ...

The 'xxx' type is lacking various properties compared to the 'xxx[]' type, such as length, pop, push, concat, and many others

Utilizing typescript and reactjs, the issue lies within this component import type { InputProps } from "../utils/types" const Input = (props: InputProps) => { const makeChildren = () => { return ( <> ...

The useEffect function is repeatedly making API calls within a component, despite not having any dependencies specified

As a newcomer to React.Js, I'm encountering an issue with useEffect repeatedly calling an API without any specified Dependency. Is there another approach I should consider? The relevant file is located at: /pages/dashboard/speaking/[slug].js } else i ...

Issues with React Router functionality on a live production site are causing complications

I recently created an Amazon-clone UI using create-react-app, but it only displays dummy data. The issue arises after deploying it to Vercel - the routing does not function as expected. When clicking on links, a blank page appears with correct URL paramete ...

Passing a variable via routes using Express

app.js var express = require('express'); var app = express(); var textVariable = "Hello World"; var homeRoute = require('./routes/index'); app.use('/', homeRoute); index.js var express = require('express'); var ...

Generating and populating ENUM arrays with Sequelize seeding

I'm having trouble figuring out how to properly seed an ARRAY(ENUM) using Sequelize. While registering a user through my app works fine, I encounter the following error when trying to use queryInterface.bulkInsert in a seed file: ERROR: column "roles ...

Loop through the JSONP object to display its properties

When I receive this object through jsonp, it has the following structure: "item1": { "child1": { "nr": 123, "money": "USD", "Date": "12.12.2016, 17:00", "asw1": 13, "SpecialField" ...

A guide to extracting row and column information from tables using div tags

I need assistance with extracting data from rows and columns that are represented by div tags within a table. This table does not use traditional tbody, tr, and td tags but instead presents the data in a UI grid view using div tags. Can anyone provide gu ...

Encountered npm error! Issue arose while resolving: [email protected] npm error! Detected: [email protected] during npm installation

I encountered an error while trying to install a project on my new PC using npm. The same project worked perfectly on my old laptop, but now I'm facing this issue. npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While re ...

Dealing with multiple v-on:click events in Vue.js

Can I add multiple v-on:click events to the same element? I want to toggle a navigation menu and trigger a CSS animation on the element that toggles the navigation. <template> <div> <nav v-if="visible"> <ul&g ...

spinning in relation to another object

Currently, I am working on developing a simple Three.js application. One of the main objectives is to synchronize the moon's movements with the Earth's. Although I have a method that allows the moon to rotate in a circular motion statically, the ...

Are there any downsides to utilizing the jQuery Load function?

I am in the process of creating a website that displays sensor data. I plan to incorporate a HighChart line chart to showcase this data. Since my website is relatively simple in terms of content, I have decided to consolidate all elements onto one page inc ...

Troubleshooting problem with autoplay on a Parallax Content Slider in IE8

Does anyone know how to hide the arrows on pictures in IE8 when the mouse is not hovering over them? I've been struggling to find a solution and would really appreciate some help. ...

Steps for integrating Stanford's NLP into a web application

I have successfully developed a project utilizing Stanford's NLP API and models. Now, I am looking to integrate this Java-based project onto the web. After seeing the demo provided by Stanford-NLP, I am curious about the process they use to achieve th ...

The client using socket.io is receiving events for "double plus one"

While experimenting with socketio, I encountered a bug that others are experiencing as well but I haven't been able to find a valid solution. This is the server-side code: const app = require('express')(); const server = require('http& ...