Foobar
Foobar

Reputation: 8487

Firefox/Chrome web extension - how do I save outputs that are too big for the console?

I tried to log the entire source of a webpage with console.log() in a chrome web extension. Unfortunately, the output was too big to save in the console. Even if it wasn't too big, I'd like to save the output in a separate text file so I can view it in the future.

Here's my current code:

fetch(newURL)
        .then((resp) => resp.text())
        .then(responseText => { console.log(responseText)}

The console crashes/says the output is too big when the program calls console.log(responseText)

How can I save "responseText" to a text file, for easier manipulation, and so the console doesn't crash?

Upvotes: 1

Views: 157

Answers (1)

Aaron
Aaron

Reputation: 409

If you're already using Node.js, you can use the File object like in this Gist: https://gist.github.com/Arahnoid/9925725

If not, try this:

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

// Start file download.
download("hello.txt","This is the content of my file :)");

Source: https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server

Upvotes: 1

Related Questions