Here is my js:
try{
let input = document.createElement("input")
input.style="display:none; white-space: pre-wrap"
input.value = message
document.body.appendChild(input)
var copyText = input
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
navigator.clipboard.writeText(copyText.value);
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
document.body.innerHTML+=copyText.value
} catch(e){console.log(e)}
I think it should work, but it only copies single-line strings. When I try to copy a multi-line string, such as
let message = `Hello
What is your name?
`
I paste it as
`Hello What is your name?`
Why is it not allowing multi-line strings?
The value of an input can't have newlines in it. If you assign a string containing newlines to an input's value, the newlines get stripped out:
const input = document.createElement("input");
input.type = "text";
const original = "one\ntwo";
input.value = original;
console.log("original: " + JSON.stringify(original));
console.log("value: " + JSON.stringify(input.value));
Instead of passing it through an input, just pass message directly to writeText. (Or if there's some particular reason for passing it through an element first, use a textarea instead of an input.)