mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-27 07:39:20 -05:00
fix: copy buttons, clipboard prompts and Auto-Copy Response break over plain HTTP on a LAN address (#33939)
Opened by an address such as http://192.168.1.20:8080 (no HTTPS, not localhost), the browser gives the page no direct clipboard access, and several things broke. Copy link on a comment and in search, Copy Share Link in Shared Chats, the webhook Copy URL, the Chat ID in the chat status bar, Copy branch name and Copy commit hash in the Git panel and the copy button of spreadsheet previews copied nothing, Copy Formatted Text pasted the ** and # symbols instead of the formatting, copied text lost its last line break, prompts using {{CLIPBOARD}} could not be inserted, and switching on Auto-Copy Response did nothing and was not saved. These copies now work on such addresses with the exact text and formatting, and {{CLIPBOARD}} prompts insert with the "Failed to read clipboard contents" message shown whenever the browser refuses clipboard access. Auto-Copy Response cannot work there, since the browser only lets a page copy right after a click, so the switch now stays off and shows its clipboard permission message. Fixes #33927 Fixes #33934 Fixes #33935 Fixes #33937 Fixes #33938
This commit is contained in:
1 parent
241c8c0e47
commit
b13d02e092
10 files changed
+66
-66
No files matched your search
@@ -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
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('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'));
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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}
|
||||
<button
|
||||
on:click={async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(branch.name);
|
||||
} catch {
|
||||
if (!(await copyToClipboard(branch.name))) {
|
||||
error = $i18n.t('Could not copy branch name');
|
||||
}
|
||||
}}><Icon name="copy" size={13} />{$i18n.t('Copy branch name')}</button
|
||||
@@ -1040,9 +1039,7 @@
|
||||
><Icon name="undo" size={13} />{$i18n.t('Undo last commit')}</button
|
||||
>{/if}<button
|
||||
on:click={async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(commit.hash);
|
||||
} catch {
|
||||
if (!(await copyToClipboard(commit.hash))) {
|
||||
error = $i18n.t('Could not copy commit hash');
|
||||
}
|
||||
}}><Icon name="copy" size={13} />{$i18n.t('Copy commit hash')}</button
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
isHeicImage,
|
||||
normalizeImageFile,
|
||||
compressImage,
|
||||
copyToClipboard,
|
||||
createMessagesList,
|
||||
extractContentFromFile,
|
||||
extractCurlyBraceWords,
|
||||
@@ -374,15 +375,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().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;
|
||||
|
||||
@@ -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<void>;
|
||||
@@ -140,10 +140,11 @@
|
||||
{/if}
|
||||
<button
|
||||
type="button"
|
||||
on:click={() => {
|
||||
navigator.clipboard
|
||||
.writeText(new URL(commentLink(comment), location.origin).href)
|
||||
.catch((e) => toast.error(String(e)));
|
||||
on:click={async () => {
|
||||
const copied = await copyToClipboard(
|
||||
new URL(commentLink(comment), location.origin).href
|
||||
);
|
||||
if (!copied) toast.error($i18n.t('Unable to copy link'));
|
||||
}}><Link /><span>{$i18n.t('Copy link')}</span></button
|
||||
>
|
||||
{#if comment.can_delete}
|
||||
|
||||
@@ -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.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
})
|
||||
|
||||
@@ -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.'));
|
||||
}}
|
||||
>
|
||||
<Clipboard class="size-4" strokeWidth="1.5" />
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
||||
+16
-29
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user