Investigation into Web Clipboard Copying Failures

In an IP-based URL environment, the Copy component fails when an element is in full-screen mode.

Sep 25, 2025 · 2 min read · 238 Words · -Views -Comments · Programming
JavaScript Frontend 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

Recently, I worked on a project using Tencent’s TEA component library. It includes a Copy component that relies on copyToClipboard, which in turn depends on document.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

Clipboard Copy Issue Dependencies

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

  1. 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.
  2. Full-screen Conflict: In Chrome, document.execCommand('copy') fails silently (no error, but no copy) when a DOM element is in full-screen mode.
  3. Secure Context Requirement: The newer Clipboard API requires a secure context (HTTPS). On internal sites or IP-based URLs that lack HTTPS, the Clipboard API will not work.

Solution

  1. Prefer the Clipboard API: Always attempt to use the modern navigator.clipboard API first. Use document.execCommand('copy') only as a fallback if the Clipboard API throws an error.
  2. Full-screen Check: Use document.fullscreenElement to 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).
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover