In order to copy some text into the clipboard, I have some code in a content script like this:
let copyBox = document.createElement("input");
copyBox.type = "text";
copyBox.style.display = "block";
copyBox.style.position = "absolute";
copyBox.style.top = "-1000px";
copyBox.style.left = "-1000px";
copyBox.style.opacity = "0";
copyBox.value = url;
document.body.appendChild(copyBox);
copyBox.select();
document.execCommand("copy");
document.body.removeChild(copyBox);
(BTW, is there a better way to do this than going through an invisible element?)
This is run ultimately from a context menu command. It works fine as long as the target is in the same document. But if it’s in an iframe, I find the selection usually doesn’t work, even though the element being created is outside the iframe. Or maybe because of that? What can I do to fix this?