Closed kmoschcau closed 2 years ago
Took a quick stab at this but things are more complicated than I expected. Sorry, I'm not invested enough in firefox to keep going down that rabbit hole. The ID is selectable wherever it can be one-click-copied so at least there's that.
Very interested if anyone out there knows how to get around this with some degree of security/efficiency.
Just for posterity to not only have it in Discord, this is how we do it at my workplace:
/**
* @param {HTMLElement} element - the element whose content to copy
*/
// eslint-disable-next-line no-unused-vars
function copyToClipboard(element) {
// From: https://stackoverflow.com/questions/36639681/how-to-copy-text-from-a-div-to-clipboard/36640126#36640126
if (document.selection) {
let range = document.body.createTextRange();
range.moveToElementText(element);
range.select().createTextRange();
document.execCommand('copy');
} else if (window.getSelection) {
window.getSelection().removeAllRanges();
let range = document.createRange();
range.selectNode(element);
window.getSelection().addRange(range);
document.execCommand('copy');
}
}
woof. I had hoped to avoid doing the whole 'fake select and execute a copy' thing. But if that's the only way to make firefox happy these days I think it would be best to put that behind a guard that checks if the permission thing we're currently using exists.
btw this works on Firefox in a secure context. This can be enabled either by using HTTPS or by accessing FVTT at localhost
instead of with an IP address.
Yeah I've realized the same thing with Vivaldi since I've switched to it. It works on localhost or via HTTPS, but not over the local network without it. I think with that we can close this.
Thanks for following up about this folks!
It makes me think that this is actually on purpose with Firefox. But the error is really misleading. Oh well, another nail in it's coffin. :/
It keeps throwing this error:
I looked some more into this and Firefox doesn't seem to support this permission yet at all. (Which is kinda surprising to me.) There's a ticket in their bug tracker which is now 3 years old and has had the last comment a year ago.
So I guess to get this to work on Firefox it seems like you should just simply write to the clipboard without asking for permission.