Skip to content

A fix for CopyToClipboard functionality that does not work #31

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

Closed
wants to merge 1 commit into from

Conversation

balex13
Copy link

@balex13 balex13 commented Jul 13, 2024

Fix CopyToClipboard Functionality

Issue:

The CopyToClipboard functionality does not work in Chrome Version 126.0.6478.127 (Official Build) (arm64), with no error messages provided.

Root Cause:

The execCommand function, which was previously used for copying text to the clipboard, is deprecated and no longer recommended as per MDN documentation. This deprecation affects its reliability and compatibility across browsers.

Solution:

To address this issue, the fix implements a conditional approach:

  • Primary Method: Use the new Clipboard API if it is available. This API is more modern and secure, though not fully supported in all environments.
  • Fallback Method: If the Clipboard API is not supported, the function falls back to using execCommand to maintain compatibility with older browsers.

Changes:

  • Added a check for Clipboard API availability.
  • Implemented the Clipboard API for copying text when supported.
  • Retained execCommand as a fallback to ensure broad compatibility.

This approach ensures that the CopyToClipboard functionality works reliably across a range of browsers and versions, improving user experience and adhering to current web standards.

### Fix CopyToClipboard Functionality

#### Issue:
The `CopyToClipboard` functionality does not work in Chrome Version 126.0.6478.127 (Official Build) (arm64), with no error messages provided.

#### Root Cause:
The `execCommand` function, which was previously used for copying text to the clipboard, is deprecated and no longer recommended as per [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand). This deprecation affects its reliability and compatibility across browsers.

#### Solution:
To address this issue, the fix implements a conditional approach:
- **Primary Method:** Use the new [Clipboard API](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard#browser_compatibility) if it is available. This API is more modern and secure, though not fully supported in all environments.
- **Fallback Method:** If the Clipboard API is not supported, the function falls back to using `execCommand` to maintain compatibility with older browsers.

#### Changes:
- Added a check for Clipboard API availability.
- Implemented the Clipboard API for copying text when supported.
- Retained `execCommand` as a fallback to ensure broad compatibility.

This approach ensures that the `CopyToClipboard` functionality works reliably across a range of browsers and versions, improving user experience and adhering to current web standards.
Copy link
Contributor

@mbtools mbtools left a comment

Choose a reason for hiding this comment

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

This is the way 👍

@mbtools
Copy link
Contributor

mbtools commented Aug 9, 2024

The execCommand("copy") works again in Chrome Version 127.0.6533.100 (Official Build) (64-bit).

Nevertheless, it's deprecated and not working on Firefox already So it would be great to get this merged 🙏

@mbtools
Copy link
Contributor

mbtools commented Sep 11, 2024

can be closed in favor of #37

@Kikobeats Kikobeats closed this Sep 11, 2024
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.

3 participants