Skip to content

Handle missing "navigator.clipboard" #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2025
Merged

Conversation

dm-xai
Copy link
Contributor

@dm-xai dm-xai commented Jan 27, 2025

In some cases navigator.clipboard can be undefined. For example on HTTP site (happens in DMZ environments).

In such cases copy will fail. However, it can always be remediated on user side by using good old execCommand.

This commit modifies code to avoid exception on copy.

In some cases navigator.clipboard can be undefined. For example on HTTP site (happens in DMZ environments).

In such cases copy will fail. However, it can always be remediated on user side by using good old execCommand. 

This commit modifies code to avoid exception on copy.
Copy link
Owner

@CarlosNZ CarlosNZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. I wonder if you know an easy way to create an environment where navigator.clipboard is not defined?

@dm-xai
Copy link
Contributor Author

dm-xai commented Jan 28, 2025

You can create a record in /etc/hosts (mac, linux) to point to your localhost and then open it with port and HTTP. I think windows has something similar.

@CarlosNZ
Copy link
Owner

You can create a record in /etc/hosts (mac, linux) to point to your localhost and then open it with port and HTTP. I think windows has something similar.

Okay, thanks. I managed to test it out and I can reproduce the issue and see your solution! I wonder though -- with your solution, I still get the enableClipboard function running even though the navigator.clipboard is skipped, so it looks as though the clipboard has copied even though it hasn't. I wonder if we should prevent that? Or were you suggesting that you can use execCommand within the enableClipboard function to get this functionality back?

If so, what function are you using?

@dm-xai
Copy link
Contributor Author

dm-xai commented Jan 29, 2025

yeah, my solution is to run copy code inside enableClipboard
but it may be good idea to move it inside your library

export default function copyToClipboard(value: string) {
  const textArea = document.createElement('textarea');
  textArea.value = value;
  // Avoid scrolling to bottom
  textArea.style.position = 'fixed';
  textArea.style.opacity = '0';
  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();

  try {
    const successful = document.execCommand('copy');
    console.log('Copying text command was ' + (successful ? 'successful' : 'unsuccessful'));
  } catch (err) {
    console.error('Oops, unable to copy', err);
  }

  document.body.removeChild(textArea);
}

@CarlosNZ
Copy link
Owner

yeah, my solution is to run copy code inside enableClipboard
but it may be good idea to move it inside your library

Thanks for the info. Apparently the execCommand is considered deprecated , and I don't love the extra boilerplate of creating a "textarea" in order to copy it, so probably won't include that natively.

I'll merge your PR though and I'll start a new issue to:

  • check if navigator?.clipboard.writeText() is successful
  • include the success/failure as an additional input property for the enableClipboard function, so you can handle it yourself if it fails.

Appreciate the contribution!

@CarlosNZ CarlosNZ merged commit faf7be4 into CarlosNZ:main Jan 29, 2025
@CarlosNZ CarlosNZ mentioned this pull request Jan 29, 2025
@CarlosNZ
Copy link
Owner

Hi @dm-xai -- this is now released in v1.22.0, along with some additional error handling I did in #162

Thanks again for your contribution 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants