web: every Copy button works over plain http; saved keys and passwords can be removed (#8508, #8511)
copyText (family idea "copyText"): - web/src/lib/utils/clipboard.ts tries the Clipboard API first. - Where the API is missing (plain http on a home network) or refuses, it copies through the copy command on a hidden textarea instead. - If that fails too, it selects the shown value. - All six Copy buttons go through it: an invite token, the needs-a-file list, diagnostics, connection details, a playlist link, and report details. Before, five of them only showed an error over http. SecretField (family idea "SecretField"): - One component for a write-only credential: a password box for a new value, which the card's own Save sends, and a Remove button while one is saved. - Used for the provider API keys, the SMTP password, the backup secret key and the AcoustID key. Only AcoustID had Remove before. - The server gains clear_password (SMTP) and clear_secret_key (backup). An empty value still keeps the stored one. Provider keys already clear when sent empty. - Remove sends the saved settings back with only the credential cleared, so unsaved edits elsewhere on the card stay unsaved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -658,7 +658,8 @@ export async function getSMTPConfig(): Promise<SMTPConfig> {
|
||||
return api.get<SMTPConfig>('/api/admin/smtp-config');
|
||||
}
|
||||
|
||||
export async function updateSMTPConfig(input: SMTPConfig): Promise<void> {
|
||||
/** A save. An empty password keeps the stored one; clear_password removes it. */
|
||||
export async function updateSMTPConfig(input: SMTPConfig & { clear_password?: boolean }): Promise<void> {
|
||||
await api.put('/api/admin/smtp-config', input);
|
||||
}
|
||||
|
||||
@@ -1036,8 +1037,8 @@ export type BackupStatus = {
|
||||
music: BackupMusicProgress | null;
|
||||
};
|
||||
|
||||
/** A save or test. An empty secret_key keeps the stored one. */
|
||||
export type BackupUpdate = Omit<BackupConfig, 'has_secret_key'> & { secret_key: string };
|
||||
/** A save or test. An empty secret_key keeps the stored one; clear_secret_key removes it. */
|
||||
export type BackupUpdate = Omit<BackupConfig, 'has_secret_key'> & { secret_key: string; clear_secret_key?: boolean };
|
||||
|
||||
export type BackupTestResult = { backups: number; latest: string | null };
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import SecretField from '#lib/components/settings/SecretField.svelte';
|
||||
import { onMount } from 'svelte';
|
||||
import {
|
||||
getAcoustID,
|
||||
@@ -157,17 +158,7 @@
|
||||
>Register an application</a
|
||||
>
|
||||
{/snippet}
|
||||
<input
|
||||
id="acoustid-key"
|
||||
type="password"
|
||||
autocomplete="off"
|
||||
bind:value={keyInput}
|
||||
placeholder={overview.settings.api_key_set ? 'Key saved' : 'Paste key'}
|
||||
class="{field} w-40 font-mono"
|
||||
/>
|
||||
{#if overview.settings.api_key_set}
|
||||
<button type="button" class={button} disabled={saving} onclick={forgetKey}>Remove</button>
|
||||
{/if}
|
||||
<SecretField id="acoustid-key" has={overview.settings.api_key_set} bind:value={keyInput} remove={forgetKey} disabled={saving} class="w-40" />
|
||||
</SettingRow>
|
||||
<SettingRow label="Minimum score" for="acoustid-score" hint="How sure AcoustID must be, 0.5 to 1.">
|
||||
<input id="acoustid-score" type="number" min="0.5" max="1" step="0.01" bind:value={minScore} class="{field} w-24" />
|
||||
|
||||
@@ -63,7 +63,7 @@ describe("AcoustIDCard", () => {
|
||||
await renderCard();
|
||||
const key = screen.getByLabelText(/api key/i) as HTMLInputElement;
|
||||
expect(key.value).toBe("");
|
||||
expect(key.placeholder).toBe("Key saved");
|
||||
expect(key.placeholder).toBe("Key saved · leave empty to keep it");
|
||||
expect(saveButton()).toHaveProperty("disabled", true);
|
||||
|
||||
await fireEvent.input(
|
||||
|
||||
@@ -143,7 +143,7 @@ describe('BackupPanel', () => {
|
||||
await renderPanel();
|
||||
const key = screen.getByLabelText(/secret key/i) as HTMLInputElement;
|
||||
expect(key.value).toBe('');
|
||||
expect(key.placeholder).toBe('Key saved');
|
||||
expect(key.placeholder).toBe('Key saved · leave empty to keep it');
|
||||
const save = screen.getByRole('button', { name: /^save$/i });
|
||||
expect(save).toHaveProperty('disabled', true);
|
||||
|
||||
@@ -165,6 +165,18 @@ describe('BackupPanel', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('Remove takes the saved secret key away, and leaves the rest as saved', async () => {
|
||||
vi.mocked(saveBackups).mockResolvedValue(base);
|
||||
await renderPanel();
|
||||
await fireEvent.input(screen.getByLabelText(/^bucket$/i), { target: { value: 'not-saved-yet' } });
|
||||
await fireEvent.click(screen.getByRole('button', { name: /^remove$/i }));
|
||||
await waitFor(() =>
|
||||
expect(saveBackups).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ bucket: base.config.bucket, secret_key: '', clear_secret_key: true })
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
test('Test destination checks the form as typed and says what it found', async () => {
|
||||
vi.mocked(testBackups).mockResolvedValue({ backups: 3, latest: '2026-11-02T03:00:05Z' });
|
||||
await renderPanel();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import SecretField from '#lib/components/settings/SecretField.svelte';
|
||||
import {
|
||||
saveBackups,
|
||||
testBackups,
|
||||
@@ -67,6 +68,17 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Removes only the secret key: the saved settings go back as they are, so
|
||||
// unsaved edits elsewhere on the card stay unsaved.
|
||||
async function removeKey() {
|
||||
try {
|
||||
onsaved(await saveBackups({ ...fromConfig(config), secret_key: '', clear_secret_key: true }));
|
||||
pushToast('Secret key removed.');
|
||||
} catch (e) {
|
||||
pushToast(errMessage(e), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function test() {
|
||||
testing = true;
|
||||
check = null;
|
||||
@@ -138,14 +150,7 @@
|
||||
<input id="backup-access-key" bind:value={draft.access_key} autocomplete="off" spellcheck="false" class="{field} w-56 font-mono" />
|
||||
</SettingRow>
|
||||
<SettingRow label="Secret key" for="backup-secret-key" hint="Kept encrypted on the server.">
|
||||
<input
|
||||
id="backup-secret-key"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
bind:value={secretKey}
|
||||
placeholder={config.has_secret_key ? 'Key saved' : 'Paste key'}
|
||||
class="{field} w-56 font-mono"
|
||||
/>
|
||||
<SecretField id="backup-secret-key" has={config.has_secret_key} bind:value={secretKey} remove={removeKey} disabled={saving} />
|
||||
</SettingRow>
|
||||
<SettingRow label="Upload limit" for="backup-limit" hint="KiB a second, so an upload leaves room on the connection. 0 is no limit.">
|
||||
<input id="backup-limit" type="number" min="0" step="256" bind:value={draft.upload_limit_kib} class="{field} w-28" />
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import type { Snippet } from 'svelte';
|
||||
import SettingsGroup from '#lib/components/settings/SettingsGroup.svelte';
|
||||
import SettingRow from '#lib/components/settings/SettingRow.svelte';
|
||||
import SecretField from '#lib/components/settings/SecretField.svelte';
|
||||
import { errMessage } from '#lib/api/errors.js';
|
||||
import { pushToast } from '#lib/stores/toast.svelte.js';
|
||||
|
||||
@@ -98,6 +99,17 @@
|
||||
}
|
||||
}
|
||||
|
||||
// An empty key removes the saved one on the server.
|
||||
async function onRemoveKey(p: P) {
|
||||
try {
|
||||
await save(p.id, { api_key: '' });
|
||||
// A test result for the old key no longer says anything.
|
||||
delete results[p.id];
|
||||
} catch (e) {
|
||||
pushToast(`Couldn't remove the key for ${p.display_name}: ${errMessage(e)}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function onTest(p: P) {
|
||||
testing[p.id] = true;
|
||||
try {
|
||||
@@ -135,12 +147,14 @@
|
||||
{#if l}
|
||||
<div class="flex flex-wrap items-center gap-2 px-4 pb-3">
|
||||
{#if p.requires_api_key}
|
||||
<input
|
||||
type="password"
|
||||
aria-label="{p.display_name} API key"
|
||||
placeholder={p.api_key_set ? 'Key saved · leave empty to keep it' : keyPlaceholder}
|
||||
<SecretField
|
||||
label="{p.display_name} API key"
|
||||
has={p.api_key_set}
|
||||
placeholder={p.api_key_set ? undefined : keyPlaceholder}
|
||||
bind:value={l.apiKey}
|
||||
class="w-64 max-w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm"
|
||||
remove={() => onRemoveKey(p)}
|
||||
disabled={saving[p.id]}
|
||||
class="w-64"
|
||||
/>
|
||||
{/if}
|
||||
<button
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import SecretField from '#lib/components/settings/SecretField.svelte';
|
||||
import { useQueryClient } from '@tanstack/svelte-query';
|
||||
import { createSMTPConfigQuery, updateSMTPConfig, testSMTPConfig, type SMTPConfig } from '#lib/api/admin.js';
|
||||
import { qk } from '#lib/api/queries.js';
|
||||
@@ -45,6 +46,19 @@
|
||||
}
|
||||
});
|
||||
|
||||
// Removes only the password: the saved settings go back as they are, so
|
||||
// unsaved edits elsewhere on the card stay unsaved.
|
||||
async function onRemovePassword() {
|
||||
if (!smtpQ.data) return;
|
||||
try {
|
||||
await updateSMTPConfig({ ...smtpQ.data, password: '', clear_password: true });
|
||||
await client.invalidateQueries({ queryKey: qk.smtpConfig() });
|
||||
pushToast('Email password removed.');
|
||||
} catch (e: unknown) {
|
||||
pushToast(`Not removed: ${errMessage(e)}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function onSave() {
|
||||
saving = true;
|
||||
try {
|
||||
@@ -116,12 +130,14 @@
|
||||
<input id="smtp-username" type="text" bind:value={form.username} class={field} />
|
||||
</SettingRow>
|
||||
<SettingRow label="Password" for="smtp-password">
|
||||
<input
|
||||
<SecretField
|
||||
id="smtp-password"
|
||||
type="password"
|
||||
placeholder={form.password === '***' ? 'Saved · leave empty to keep it' : ''}
|
||||
noun="password"
|
||||
has={smtpQ.data?.password === '***'}
|
||||
bind:value={passwordInput}
|
||||
class={field}
|
||||
remove={onRemovePassword}
|
||||
disabled={!loaded || saving}
|
||||
class="w-64"
|
||||
/>
|
||||
</SettingRow>
|
||||
<SettingRow label="From address" for="smtp-from">
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<script lang="ts">
|
||||
// A credential the server keeps and never sends back: an API key or a
|
||||
// password. The box holds a new value for the card's own Save to send, and
|
||||
// is empty while a saved one is kept. Remove takes the saved one away at
|
||||
// once, so a key that was pasted by mistake, or belongs to an account
|
||||
// that's gone, doesn't have to stay.
|
||||
//
|
||||
// `remove` does the request and reports its own failure; the button is busy
|
||||
// until it settles.
|
||||
|
||||
let {
|
||||
id,
|
||||
label,
|
||||
has,
|
||||
value = $bindable(''),
|
||||
remove,
|
||||
noun = 'key',
|
||||
placeholder,
|
||||
disabled = false,
|
||||
class: cls = 'w-56'
|
||||
}: {
|
||||
id?: string;
|
||||
// The box's accessible name, when no visible label names it.
|
||||
label?: string;
|
||||
// Whether a value is saved now.
|
||||
has: boolean;
|
||||
value?: string;
|
||||
remove: () => Promise<unknown>;
|
||||
noun?: 'key' | 'password';
|
||||
placeholder?: string;
|
||||
disabled?: boolean;
|
||||
class?: string;
|
||||
} = $props();
|
||||
|
||||
let removing = $state(false);
|
||||
|
||||
async function onRemove() {
|
||||
removing = true;
|
||||
try {
|
||||
await remove();
|
||||
} finally {
|
||||
removing = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<input
|
||||
{id}
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
spellcheck="false"
|
||||
aria-label={label}
|
||||
bind:value
|
||||
{disabled}
|
||||
placeholder={placeholder ?? (has ? `${noun === 'key' ? 'Key' : 'Password'} saved · leave empty to keep it` : `Paste the ${noun}`)}
|
||||
class="{cls} max-w-full rounded-md border border-border bg-background px-3 py-1.5 font-mono text-sm"
|
||||
/>
|
||||
{#if has}
|
||||
<button
|
||||
type="button"
|
||||
disabled={disabled || removing}
|
||||
onclick={onRemove}
|
||||
class="rounded-md border border-border px-3 py-1.5 text-sm text-text-secondary hover:bg-surface-hover hover:text-text-primary disabled:opacity-50"
|
||||
>{removing ? 'Removing…' : 'Remove'}</button
|
||||
>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -0,0 +1,83 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { copyText } from './clipboard';
|
||||
|
||||
// The Clipboard API is there on a secure origin and missing over plain http.
|
||||
function setClipboard(writeText: ((t: string) => Promise<void>) | undefined) {
|
||||
Object.defineProperty(navigator, 'clipboard', {
|
||||
configurable: true,
|
||||
value: writeText ? { writeText } : undefined
|
||||
});
|
||||
}
|
||||
|
||||
function setCopyCommand(result: boolean | undefined) {
|
||||
Object.defineProperty(document, 'execCommand', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: result === undefined ? undefined : vi.fn(() => result)
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
window.getSelection()?.removeAllRanges();
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
describe('copyText', () => {
|
||||
it('uses the Clipboard API on a secure origin', async () => {
|
||||
const writeText = vi.fn(async () => {});
|
||||
setClipboard(writeText);
|
||||
setCopyCommand(false);
|
||||
|
||||
expect(await copyText('token-1')).toBe(true);
|
||||
expect(writeText).toHaveBeenCalledWith('token-1');
|
||||
expect(document.execCommand).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('copies over plain http, where there is no Clipboard API', async () => {
|
||||
setClipboard(undefined);
|
||||
let copied = '';
|
||||
setCopyCommand(true);
|
||||
vi.mocked(document.execCommand).mockImplementation(() => {
|
||||
copied = document.querySelector('textarea')?.value ?? '';
|
||||
return true;
|
||||
});
|
||||
|
||||
expect(await copyText('http://music.example.lan/playlists/1')).toBe(true);
|
||||
expect(copied).toBe('http://music.example.lan/playlists/1');
|
||||
expect(document.querySelector('textarea')).toBeNull();
|
||||
});
|
||||
|
||||
it('falls back to the copy command when the Clipboard API refuses', async () => {
|
||||
setClipboard(async () => Promise.reject(new Error('denied')));
|
||||
setCopyCommand(true);
|
||||
|
||||
expect(await copyText('x')).toBe(true);
|
||||
expect(document.execCommand).toHaveBeenCalledWith('copy');
|
||||
});
|
||||
|
||||
it('selects the shown value when nothing can copy', async () => {
|
||||
setClipboard(undefined);
|
||||
setCopyCommand(false);
|
||||
const input = document.createElement('input');
|
||||
input.readOnly = true;
|
||||
input.value = 'invite-code';
|
||||
document.body.appendChild(input);
|
||||
|
||||
expect(await copyText('invite-code', input)).toBe(false);
|
||||
expect(document.activeElement).toBe(input);
|
||||
expect(input.selectionStart).toBe(0);
|
||||
expect(input.selectionEnd).toBe('invite-code'.length);
|
||||
});
|
||||
|
||||
it('selects the contents of a shown element that is not an input', async () => {
|
||||
setClipboard(undefined);
|
||||
setCopyCommand(undefined);
|
||||
const code = document.createElement('code');
|
||||
code.textContent = '/music/library';
|
||||
document.body.appendChild(code);
|
||||
|
||||
expect(await copyText('/music/library', code)).toBe(false);
|
||||
expect(window.getSelection()?.getRangeAt(0).toString()).toBe('/music/library');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
// copyText puts text on the clipboard and says whether it got there.
|
||||
//
|
||||
// navigator.clipboard exists only on a secure origin, and a self-hosted
|
||||
// server is often reached over plain http on a home network, where it is
|
||||
// undefined. So a failed or missing Clipboard API falls back to the copy
|
||||
// command on a hidden textarea, which browsers still honour there. If that
|
||||
// fails too and the value is on the page, `shown` is selected so a press of
|
||||
// Ctrl+C or a long-press copies it.
|
||||
//
|
||||
// Every Copy button goes through here. Don't call navigator.clipboard directly.
|
||||
export async function copyText(text: string, shown?: Element | null): Promise<boolean> {
|
||||
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return true;
|
||||
} catch {
|
||||
// Denied or unfocused: try the copy command below.
|
||||
}
|
||||
}
|
||||
if (copyByCommand(text)) return true;
|
||||
if (shown) select(shown);
|
||||
return false;
|
||||
}
|
||||
|
||||
function copyByCommand(text: string): boolean {
|
||||
if (typeof document === 'undefined' || typeof document.execCommand !== 'function') return false;
|
||||
const focused = document.activeElement as HTMLElement | null;
|
||||
const box = document.createElement('textarea');
|
||||
box.value = text;
|
||||
box.readOnly = true;
|
||||
box.setAttribute('aria-hidden', 'true');
|
||||
box.style.position = 'fixed';
|
||||
box.style.top = '0';
|
||||
box.style.left = '-9999px';
|
||||
document.body.appendChild(box);
|
||||
box.select();
|
||||
let ok = false;
|
||||
try {
|
||||
ok = document.execCommand('copy');
|
||||
} catch {
|
||||
ok = false;
|
||||
}
|
||||
box.remove();
|
||||
focused?.focus?.();
|
||||
return ok;
|
||||
}
|
||||
|
||||
// select highlights a shown value: an input or textarea by its own selection,
|
||||
// any other element by a range over its contents. A disabled input can't be
|
||||
// selected, so a shown value sits in a readonly one.
|
||||
function select(el: Element) {
|
||||
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
|
||||
el.focus();
|
||||
el.select();
|
||||
return;
|
||||
}
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(el);
|
||||
const sel = window.getSelection();
|
||||
sel?.removeAllRanges();
|
||||
sel?.addRange(range);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { copyText } from '#lib/utils/clipboard.js';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
import { page } from '$app/state';
|
||||
import { relativeTime } from '#lib/utils/relativeTime.js';
|
||||
@@ -162,11 +163,10 @@
|
||||
}
|
||||
|
||||
async function onCopyJson() {
|
||||
try {
|
||||
await navigator.clipboard.writeText(JSON.stringify(exportObject(), null, 2));
|
||||
if (await copyText(JSON.stringify(exportObject(), null, 2))) {
|
||||
pushToast(`Copied ${rows.length} events to clipboard`);
|
||||
} catch (e: unknown) {
|
||||
pushToast(`Copy failed: ${errMessage(e)}`, 'error');
|
||||
} else {
|
||||
pushToast('Copy failed. Download the events instead.', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { copyText } from '#lib/utils/clipboard.js';
|
||||
import { PASSWORD_MIN_LENGTH, PASSWORDS_DIFFER } from '#lib/auth/password.js';
|
||||
import { knownCopy } from '#lib/api/error-copy.js';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
@@ -185,12 +186,8 @@
|
||||
}
|
||||
|
||||
async function copyToken(token: string) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(token);
|
||||
pushToast('Token copied to clipboard.');
|
||||
} catch {
|
||||
pushToast('Copy failed — select the token and copy manually.', 'error');
|
||||
}
|
||||
if (await copyText(token)) pushToast('Token copied to clipboard.');
|
||||
else pushToast('Copy failed — select the token and copy manually.', 'error');
|
||||
}
|
||||
|
||||
function formatDate(iso: string): string {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { copyText } from '#lib/utils/clipboard.js';
|
||||
import Cover from '#lib/components/Cover.svelte';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
@@ -73,7 +74,7 @@
|
||||
try {
|
||||
const res = await fetch(MISSING_FILES_CSV_URL, { credentials: 'same-origin', signal: AbortSignal.timeout(15_000) });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
await navigator.clipboard.writeText(await res.text());
|
||||
if (!(await copyText(await res.text()))) throw new Error('this browser refused to copy');
|
||||
pushToast(`Copied ${plural(total, 'track', 'tracks')}`);
|
||||
} catch (e) {
|
||||
pushToast(`Couldn't copy the list: ${errMessage(e)}`, 'error');
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { copyText } from '#lib/utils/clipboard.js';
|
||||
import Cover from '#lib/components/Cover.svelte';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
@@ -97,12 +98,8 @@
|
||||
at: x.occurred_at
|
||||
}))
|
||||
};
|
||||
try {
|
||||
await navigator.clipboard.writeText(JSON.stringify(payload, null, 2));
|
||||
pushToast('Copied the details');
|
||||
} catch (e) {
|
||||
pushToast(`Copy failed: ${errMessage(e)}`, 'error');
|
||||
}
|
||||
if (await copyText(JSON.stringify(payload, null, 2))) pushToast('Copied the details');
|
||||
else pushToast('Copy failed: this browser refused to copy', 'error');
|
||||
}
|
||||
|
||||
async function onPlay(r: ReportRow) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { copyText } from '#lib/utils/clipboard.js';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
import PageColumn from '#lib/components/PageColumn.svelte';
|
||||
import { page } from '$app/state';
|
||||
@@ -178,12 +179,11 @@
|
||||
|
||||
async function onCopyLink() {
|
||||
const href = `${window.location.origin}/playlists/${id}`;
|
||||
try {
|
||||
await navigator.clipboard.writeText(href);
|
||||
if (await copyText(href)) {
|
||||
pushToast('Link copied to clipboard.');
|
||||
} catch {
|
||||
// navigator.clipboard fails in insecure contexts and older browsers.
|
||||
// Fall back to selecting the link in a prompt so the user can copy.
|
||||
} else {
|
||||
// Nothing could copy, and the link isn't on the page: show it in a
|
||||
// prompt, where it is selected ready to copy.
|
||||
window.prompt('Copy the playlist link:', href);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { copyText } from '#lib/utils/clipboard.js';
|
||||
import SettingsLoadStatus from '#lib/components/settings/SettingsLoadStatus.svelte';
|
||||
import SettingsColumns from '#lib/components/settings/SettingsColumns.svelte';
|
||||
import { onMount } from 'svelte';
|
||||
@@ -144,12 +145,8 @@
|
||||
|
||||
async function copy(text: string | null) {
|
||||
if (!text) return;
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
pushToast('Copied to clipboard.');
|
||||
} catch {
|
||||
pushToast('Copy failed.', 'error');
|
||||
}
|
||||
if (await copyText(text)) pushToast('Copied to clipboard.');
|
||||
else pushToast('Copy failed. Select it and copy it yourself.', 'error');
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user