-2

I've tried using JS's copy() and creating an element/populating it with the data/copying the result. I cannot figure out how to get the response.text() to the clipboard.

The actual fetch is an XHR but the below example should suffice:

fetch('/readme.txt')
    .then(response => response.text())
    .then(data => console.log(data));
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mech
  • 3,952
  • 2
  • 14
  • 25

1 Answers1

0

You need to get response text and then copy it to the clipboard using another function:

fetch('/readme.txt').then(
    response => response.text()
).then(
    text => {
        navigator.clipboard.writeText(text).then(
            () => console.log("Success!"),
            (err) => console.log("Error copying response text", err)
        )
    }
)

In old browsers navigator.clipboard can be unavailable, you can get more info about copying in this answer

Nikitiy
  • 37
  • 4