When I run this script (embedded in a HTML page) 'GG' isn't copied to the clipboard. Can someone help/explain why and assist me in getting it working? Thanks.
document.getElementById("blah3");
let s ="GG";
navigator.clipboard.writeText(s);
When I run this script (embedded in a HTML page) 'GG' isn't copied to the clipboard. Can someone help/explain why and assist me in getting it working? Thanks.
document.getElementById("blah3");
let s ="GG";
navigator.clipboard.writeText(s);
Copyright © 2021 Jogjafile Inc.
Like the link in the comments says, you need to attach a clipboard write to a user action for security reasons. Usually you make a user click a button and add an event listener for it.
DISCLAIMER: THE FOLLOWING IS FOR EDUCATIONAL PURPOSES. PLEASE DO NOT TRY TO DODGE SECURITY FEATURES.
If you really want to shimmy around security you can do some questionable things.
You could keep trying to write until the user does something and it succeeds. Using a timeout will prevent the thread from blocking. Best to wait a few ms before polling again so as not to make the CPU go brrr
or you could attach it to events that are likely to happen
You'll always be restricted to the user interacting with the page in some way before you can write.