League-of-Foundry-Developers / foundryvtt-devMode

A module with some tools to help enable Foundry VTT package developers.
MIT License
12 stars 15 forks source link

Copying the ID of a document to clipboard doesn't work in Firefox #27

Closed kmoschcau closed 2 years ago

kmoschcau commented 2 years ago

It keeps throwing this error:

TypeError: 'clipboard-write' (value of 'name' member of PermissionDescriptor) is not a valid value for enumeration PermissionName.

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.

akrigline commented 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.

kmoschcau commented 2 years ago

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');
  }
}
akrigline commented 2 years ago

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.

arcanistzed commented 2 years ago

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.

kmoschcau commented 2 years ago

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.

akrigline commented 2 years ago

Thanks for following up about this folks!

kmoschcau commented 2 years ago

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. :/