synchronously set clipboard, then store value in variable, then set that variable to local storage in javascript

Here's an example code block that demonstrates synchronously setting the clipboard, storing the value in a variable and then setting that variable to local storage:

index.tsx
//get the text to copy
const copyText = "example text";
//create a textarea to hold it
const tempTextArea = document.createElement("textarea");
//set the textarea text to our copied text
tempTextArea.value = copyText;
//append the textarea to the document
document.body.appendChild(tempTextArea);
//select the text in the textarea
tempTextArea.select();
//copy the selected text to the clipboard synchronously
document.execCommand("copy");
//remove the textarea from the document
document.body.removeChild(tempTextArea);

//store the copied text in local storage
localStorage.setItem("copiedText", copyText);

//get the copied text from local storage
const storedText = localStorage.getItem("copiedText");
console.log(storedText); //outputs: "example text" 
758 chars
22 lines

In this code block, we first create a variable copyText with the text we want to copy. We then create a temporary textarea element, set its value to the copyText variable, and attach it to the document. We then select the text within the textarea and copy it to the clipboard using the document.execCommand("copy") method.

Next, we store the copied text value in local storage using the .setItem() method. We then retrieve the stored value from local storage using the .getItem() method and store it in the storedText variable.

Finally, we log the storedText variable to the console to verify that the value was successfully stored in local storage.

gistlibby LogSnag