Recently, I worked on a project using Tencent’s TEA component library. It includes a
Copycomponent that relies oncopyToClipboard, which in turn depends ondocument.execCommand('copy'). This API has several pitfalls; here is a summary of what I encountered.
The Problem
When visiting a site via an IP-based URL, the Copy component fails to copy text if a specific DOM element is currently in full-screen mode.
Component Dependency Graph

How the Copy Component Works
Under the hood, it creates an invisible element, populates it with text, selects that text, executes document.execCommand('copy'), and then removes the element from the DOM.
Analysis
- Deprecated API:
document.execCommand('copy')is deprecated. However, it is still widely used because it can function even when the browser hasn’t explicitly granted Clipboard API permissions. - Full-screen Conflict: In Chrome,
document.execCommand('copy')fails silently (no error, but no copy) when a DOM element is in full-screen mode. - Secure Context Requirement: The newer
Clipboard APIrequires a secure context (HTTPS). On internal sites or IP-based URLs that lack HTTPS, the Clipboard API will not work.
Solution
- Prefer the Clipboard API: Always attempt to use the modern
navigator.clipboardAPI first. Usedocument.execCommand('copy')only as a fallback if the Clipboard API throws an error. - Full-screen Check: Use
document.fullscreenElementto detect if the user is in full-screen mode. Since the fallback won’t work in this state, you should handle this explicitly (e.g., by notifying the user to exit full-screen if the modern API fails).

