diff --git a/src/lib/components/channel/MessageInput.svelte b/src/lib/components/channel/MessageInput.svelte index e441069e10..d50f98c661 100644 --- a/src/lib/components/channel/MessageInput.svelte +++ b/src/lib/components/channel/MessageInput.svelte @@ -134,12 +134,20 @@ const textVariableHandler = async (text: string) => { if (text.includes('{{CLIPBOARD}}')) { - const clipboardText = await navigator.clipboard.readText().catch((err) => { + let clipboardText: string; + try { + clipboardText = await navigator.clipboard.readText(); + } catch (error) { toast.error($i18n.t('Failed to read clipboard contents')); - return '{{CLIPBOARD}}'; - }); + clipboardText = '{{CLIPBOARD}}'; + } - const clipboardItems = await navigator.clipboard.read(); + let clipboardItems: ClipboardItem[] = []; + try { + clipboardItems = await navigator.clipboard.read(); + } catch (error) { + console.error('Failed to read clipboard items:', error); + } for (const item of clipboardItems) { // Check for known image types diff --git a/src/lib/components/channel/WebhookItem.svelte b/src/lib/components/channel/WebhookItem.svelte index 840281df51..a27ca1fff6 100644 --- a/src/lib/components/channel/WebhookItem.svelte +++ b/src/lib/components/channel/WebhookItem.svelte @@ -3,6 +3,7 @@ const i18n = getContext('i18n'); import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants'; + import { copyToClipboard } from '$lib/utils'; import ChevronDown from '$lib/components/icons/ChevronDown.svelte'; import Clipboard from '$lib/components/icons/Clipboard.svelte'; @@ -70,11 +71,11 @@ reader.readAsDataURL(inputFiles[0]); }; - const copyUrl = () => { - navigator.clipboard.writeText( + const copyUrl = async () => { + const copied = await copyToClipboard( `${WEBUI_API_BASE_URL}/channels/webhooks/${webhook.id}/${webhook.token}` ); - toast.success($i18n.t('Copied')); + if (copied) toast.success($i18n.t('Copied')); }; diff --git a/src/lib/components/chat/ChatTabs/GitBar.svelte b/src/lib/components/chat/ChatTabs/GitBar.svelte index 6a5515df8b..e091ad311b 100644 --- a/src/lib/components/chat/ChatTabs/GitBar.svelte +++ b/src/lib/components/chat/ChatTabs/GitBar.svelte @@ -11,6 +11,7 @@ type GitCommit, type GitBranch } from '$lib/apis/terminal/git'; + import { copyToClipboard } from '$lib/utils'; import Icon from '../FileNav/Icon.svelte'; import GitDiff from './GitDiff.svelte'; import { parseDiffRows } from '../Messages/diff'; @@ -538,9 +539,7 @@ >{/if} {$i18n.t('Undo last commit')}{/if} { if (text.includes('{{CLIPBOARD}}')) { - const clipboardText = await navigator.clipboard.readText().catch((err) => { + let clipboardText: string; + try { + clipboardText = await navigator.clipboard.readText(); + } catch (error) { toast.error($i18n.t('Failed to read clipboard contents')); - return '{{CLIPBOARD}}'; - }); + clipboardText = '{{CLIPBOARD}}'; + } - const clipboardItems = await navigator.clipboard.read().catch((err) => { - console.error('Failed to read clipboard items:', err); - return []; - }); + let clipboardItems: ClipboardItem[] = []; + try { + clipboardItems = await navigator.clipboard.read(); + } catch (error) { + console.error('Failed to read clipboard items:', error); + } for (const item of clipboardItems) { for (const type of item.types) { @@ -609,7 +615,7 @@ const copyStatusChatId = async () => { if (!chatId) return; - await navigator.clipboard.writeText(chatId); + if (!(await copyToClipboard(chatId))) return; copiedStatusChatId = true; setTimeout(() => { copiedStatusChatId = false; diff --git a/src/lib/components/comments/CommentItem.svelte b/src/lib/components/comments/CommentItem.svelte index 070ae67993..d0ad8606a8 100644 --- a/src/lib/components/comments/CommentItem.svelte +++ b/src/lib/components/comments/CommentItem.svelte @@ -20,7 +20,7 @@ import CommentComposer from './CommentComposer.svelte'; import { commentsRequest, type ResourceComment } from '$lib/apis/comments'; import { highlightAnchor } from './anchors'; - import { commentLink } from '$lib/utils'; + import { commentLink, copyToClipboard } from '$lib/utils'; export let comment: ResourceComment; export let onReply: (comment: ResourceComment) => void; export let onChange: () => Promise; @@ -140,10 +140,11 @@ {/if} {#if comment.can_delete} diff --git a/src/lib/components/common/ExcelPreview.svelte b/src/lib/components/common/ExcelPreview.svelte index 7b02dc1527..31e2a8c402 100644 --- a/src/lib/components/common/ExcelPreview.svelte +++ b/src/lib/components/common/ExcelPreview.svelte @@ -12,6 +12,7 @@ selectionTSV, type GridCell } from '$lib/utils/excelToTable'; + import { copyToClipboard } from '$lib/utils'; export let html: string; export let sheetNames: string[] = []; @@ -490,10 +491,9 @@ status = t('Copied to clipboard'); } async function copySelection() { - try { - await navigator.clipboard.writeText(copiedText()); + if (await copyToClipboard(copiedText())) { status = t('Copied to clipboard'); - } catch { + } else { status = t('Could not copy. Select a cell and press Ctrl+C or ⌘C.'); } } diff --git a/src/lib/components/common/InterfaceSettings.svelte b/src/lib/components/common/InterfaceSettings.svelte index 0be947cfea..f9214be3bb 100644 --- a/src/lib/components/common/InterfaceSettings.svelte +++ b/src/lib/components/common/InterfaceSettings.svelte @@ -214,13 +214,13 @@ }; const toggleResponseAutoCopy = async () => { - if (externalSettings) { + if (externalSettings || !responseAutoCopy) { saveSettings({ responseAutoCopy: responseAutoCopy }); return; } const permission = await navigator.clipboard - .readText() + ?.readText() .then(() => { return 'granted'; }) diff --git a/src/lib/components/layout/ChatsModal.svelte b/src/lib/components/layout/ChatsModal.svelte index 9d67fee05e..8e0e5b5ae6 100644 --- a/src/lib/components/layout/ChatsModal.svelte +++ b/src/lib/components/layout/ChatsModal.svelte @@ -11,7 +11,7 @@ import { deleteChatById } from '$lib/apis/chats'; import { WEBUI_API_BASE_URL } from '$lib/constants'; - import { formatNumber } from '$lib/utils'; + import { copyToClipboard, formatNumber } from '$lib/utils'; import Modal from '$lib/components/common/Modal.svelte'; import Tooltip from '$lib/components/common/Tooltip.svelte'; @@ -356,8 +356,8 @@ e.stopImmediatePropagation(); e.stopPropagation(); const shareUrl = `${window.location.origin}/s/${chat.share_id}`; - await navigator.clipboard.writeText(shareUrl); - toast.success($i18n.t('Share link copied to clipboard.')); + if (await copyToClipboard(shareUrl)) + toast.success($i18n.t('Share link copied to clipboard.')); }} > diff --git a/src/lib/components/layout/SearchModal.svelte b/src/lib/components/layout/SearchModal.svelte index 8e75d15dc3..65d49fee2e 100644 --- a/src/lib/components/layout/SearchModal.svelte +++ b/src/lib/components/layout/SearchModal.svelte @@ -3,6 +3,7 @@ import { goto } from '$app/navigation'; import { toast } from 'svelte-sonner'; import { folders, mobile, settings, tags } from '$lib/stores'; + import { copyToClipboard } from '$lib/utils'; import DropdownMenu from '$lib/components/common/DropdownMenu.svelte'; import DropdownSub from '$lib/components/common/DropdownSub.svelte'; import Check from '$lib/components/icons/Check.svelte'; @@ -285,11 +286,10 @@ async function copyLink() { if (!selected) return; - try { - await navigator.clipboard.writeText(new URL(selected.url, location.origin).href); + if (await copyToClipboard(new URL(selected.url, location.origin).href)) { toast.success($i18n.t('Link copied to clipboard')); panel = null; - } catch { + } else { toast.error($i18n.t('Unable to copy link')); } } diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 521e017741..7dc71c307a 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -534,6 +534,19 @@ export const formatMessageTimestampFull = (inputDate, locale: string) => minute: '2-digit' }) ?? ''; +const copyWithExecCommand = (text: string, html: string | null = null) => { + const setClipboardData = (event: ClipboardEvent) => { + event.preventDefault(); + event.clipboardData?.clearData(); + event.clipboardData?.setData('text/plain', text); + if (html) event.clipboardData?.setData('text/html', html); + }; + document.addEventListener('copy', setClipboardData); + const copied = document.execCommand('copy'); + document.removeEventListener('copy', setClipboardData); + return copied; +}; + export const copyToClipboard = async (text, html = null, formatted = false) => { if (formatted) { let styledHtml = ''; @@ -605,6 +618,8 @@ export const copyToClipboard = async (text, html = null, formatted = false) => { styledHtml = html; } + if (!navigator.clipboard) return copyWithExecCommand(text, styledHtml); + // Create a blob with HTML content const blob = new Blob([styledHtml], { type: 'text/html' }); @@ -624,36 +639,8 @@ export const copyToClipboard = async (text, html = null, formatted = false) => { return await copyToClipboard(text); } } else { + if (!navigator.clipboard) return copyWithExecCommand(text); let result = false; - if (!navigator.clipboard) { - const span = document.createElement('span'); - span.textContent = text; - span.style.whiteSpace = 'pre'; - span.style.position = 'fixed'; - span.style.top = '0'; - span.style.left = '0'; - span.style.opacity = '0'; - document.body.appendChild(span); - - const range = document.createRange(); - range.selectNodeContents(span); - const selection = window.getSelection(); - selection?.removeAllRanges(); - selection?.addRange(range); - - try { - const successful = document.execCommand('copy'); - const msg = successful ? 'successful' : 'unsuccessful'; - console.log('Fallback: Copying text command was ' + msg); - result = successful; - } catch (err) { - console.error('Fallback: Oops, unable to copy', err); - } - - selection?.removeAllRanges(); - document.body.removeChild(span); - return result; - } result = await navigator.clipboard .writeText(text)