You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2026/10/18 00:51:19 UTC
(skywalking-horizon-ui) branch main updated: fix(ui): copy over plain HTTP from every copy button (#200)
This is an automated email from the ASF dual-hosted git repository.
wu-sheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-horizon-ui.git
The following commit(s) were added to refs/heads/main by this push:
new d92d59c8 fix(ui): copy over plain HTTP from every copy button (#200)
d92d59c8 is described below
commit d92d59c87374f47d8afb21d98eca1debd926af6d
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Sun Oct 18 08:51:14 2026 +0800
fix(ui): copy over plain HTTP from every copy button (#200)
A browser offers the Clipboard API only to a secure context, HTTPS or
localhost. On a Horizon opened by an intranet address over plain HTTP,
16 copy buttons in 11 files called it and did nothing, with no message:
the trace id and link copies of every trace tab, the log and evaluation
record popouts, the slow-statement cards, a long value's dialog, the MQE
result and the Inspect page. Only the layer's share button fell back.
copyText in utils/clipboard.ts copies as the conversation renderer does
since #190: the Clipboard API when the page has it, and otherwise, or
when it refuses, a hidden text area and execCommand('copy'), giving the
focus back. It says whether the text was copied. Every copy button now
uses it and shows "copied" only when it was, so the share button and the
slow-statement cards no longer say it after a failed copy.
---
apps/ui/src/components/primitives/LongValue.vue | 6 +-
.../src/features/admin/_shared/MqeResultView.vue | 11 +--
.../src/features/operate/inspect/InspectView.vue | 3 +-
apps/ui/src/layer/LayerShell.vue | 17 +---
apps/ui/src/layer/traceql/TraceQLDetailCard.vue | 5 +-
apps/ui/src/layer/traces/TracePopout.vue | 5 +-
apps/ui/src/layer/traces/ZipkinTracePopout.vue | 5 +-
.../widgets/EvaluationRecordDetailPopout.vue | 19 ++---
apps/ui/src/render/widgets/LogDetailPopout.vue | 19 ++---
apps/ui/src/render/widgets/RecordList.vue | 7 +-
apps/ui/src/render/widgets/TraceDetailCard.vue | 5 +-
.../src/render/widgets/ZipkinTraceDetailCard.vue | 5 +-
apps/ui/src/utils/clipboard.test.ts | 93 ++++++++++++++++++++++
apps/ui/src/utils/clipboard.ts | 58 ++++++++++++++
docs/changelog/1.1.0.md | 2 +
15 files changed, 198 insertions(+), 62 deletions(-)
diff --git a/apps/ui/src/components/primitives/LongValue.vue b/apps/ui/src/components/primitives/LongValue.vue
index a35b0ecc..e6a0e438 100644
--- a/apps/ui/src/components/primitives/LongValue.vue
+++ b/apps/ui/src/components/primitives/LongValue.vue
@@ -27,6 +27,7 @@
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import Modal from './Modal.vue';
+import { copyText } from '@/utils/clipboard';
const props = withDefaults(
defineProps<{
@@ -61,11 +62,12 @@ const open = ref(false);
const copied = ref(false);
let copiedTimer: ReturnType<typeof setTimeout> | null = null;
function copyAll(): void {
- navigator.clipboard?.writeText(props.value).then(() => {
+ void copyText(props.value).then((done) => {
+ if (!done) return;
copied.value = true;
if (copiedTimer) clearTimeout(copiedTimer);
copiedTimer = setTimeout(() => { copied.value = false; }, 1400);
- }, () => {});
+ });
}
</script>
diff --git a/apps/ui/src/features/admin/_shared/MqeResultView.vue b/apps/ui/src/features/admin/_shared/MqeResultView.vue
index 6fa116b6..31c6d242 100644
--- a/apps/ui/src/features/admin/_shared/MqeResultView.vue
+++ b/apps/ui/src/features/admin/_shared/MqeResultView.vue
@@ -37,6 +37,7 @@ import TimeChart from '@/components/charts/TimeChart.vue';
const MonacoView = defineAsyncComponent(() => import('@/components/primitives/MonacoView.vue'));
import { alignSeries, seriesLabel } from './mqeSeries';
import { timestampLabel } from '@/utils/formatters';
+import { copyText } from '@/utils/clipboard';
const props = defineProps<{
result: ExpressionResult;
@@ -72,13 +73,9 @@ const pretty = computed(() => JSON.stringify(props.result, null, 2));
const copied = ref(false);
async function copyJson(): Promise<void> {
- try {
- await navigator.clipboard.writeText(pretty.value);
- copied.value = true;
- setTimeout(() => (copied.value = false), 1200);
- } catch {
- /* clipboard blocked (insecure context / no permission) — no-op */
- }
+ if (!(await copyText(pretty.value))) return;
+ copied.value = true;
+ setTimeout(() => (copied.value = false), 1200);
}
/** A type can be legitimate while `results` is empty: when storage returns a
diff --git a/apps/ui/src/features/operate/inspect/InspectView.vue b/apps/ui/src/features/operate/inspect/InspectView.vue
index c4e5b907..3e37ffdb 100644
--- a/apps/ui/src/features/operate/inspect/InspectView.vue
+++ b/apps/ui/src/features/operate/inspect/InspectView.vue
@@ -61,6 +61,7 @@ import {
} from './inspectTime';
import { buildForeignMqeEntity, makeForeignMetric } from './inspectForeign';
import { scopeShort, type ForeignForm, type Source } from './inspectTypes';
+import { copyText } from '@/utils/clipboard';
const { t } = useI18n({ useScope: 'global' });
@@ -810,7 +811,7 @@ function copyMqeEntity(w: Widget) {
.filter((m): m is MqeEntity => m !== undefined);
if (sel.length === 0) return;
const payload = sel.length === 1 ? sel[0] : sel;
- navigator.clipboard?.writeText(JSON.stringify(payload, null, 2));
+ void copyText(JSON.stringify(payload, null, 2));
}
function addCustomEntity(w: Widget) {
diff --git a/apps/ui/src/layer/LayerShell.vue b/apps/ui/src/layer/LayerShell.vue
index 4d747c7d..07442d93 100644
--- a/apps/ui/src/layer/LayerShell.vue
+++ b/apps/ui/src/layer/LayerShell.vue
@@ -55,6 +55,7 @@ import { entryRedirect, rosterReadKey } from './entryRoute';
import { fmtMetric } from '@/utils/formatters';
import { parseServiceName, isBlankServiceName, BLANK_SERVICE_NAME } from '@/utils/serviceName';
import { resolveEntry, routeRow } from '@/utils/layerRoute';
+import { copyText } from '@/utils/clipboard';
const { t } = useI18n({ useScope: 'global' });
@@ -500,21 +501,7 @@ const shareCopied = ref(false);
let shareCopiedTimer: ReturnType<typeof setTimeout> | null = null;
async function copyShareLink(): Promise<void> {
const url = window.location.origin + window.location.pathname;
- try {
- await navigator.clipboard.writeText(url);
- } catch {
- // Fallback for non-secure contexts (clipboard API requires HTTPS
- // or localhost). A hidden textarea + execCommand is the only
- // path that works in plain HTTP intranets — common deployment
- // shape for self-hosted OAP installs.
- const ta = document.createElement('textarea');
- ta.value = url;
- ta.style.position = 'fixed';
- ta.style.opacity = '0';
- document.body.appendChild(ta);
- ta.select();
- try { document.execCommand('copy'); } finally { document.body.removeChild(ta); }
- }
+ if (!(await copyText(url))) return;
shareCopied.value = true;
if (shareCopiedTimer) clearTimeout(shareCopiedTimer);
shareCopiedTimer = setTimeout(() => { shareCopied.value = false; }, 1400);
diff --git a/apps/ui/src/layer/traceql/TraceQLDetailCard.vue b/apps/ui/src/layer/traceql/TraceQLDetailCard.vue
index 348cef90..1afd2587 100644
--- a/apps/ui/src/layer/traceql/TraceQLDetailCard.vue
+++ b/apps/ui/src/layer/traceql/TraceQLDetailCard.vue
@@ -33,6 +33,7 @@ import TraceQLTreeView from './TraceQLTreeView.vue';
import TraceQLStatsView from './TraceQLStatsView.vue';
import TraceQLSpanModal from './TraceQLSpanModal.vue';
import { buildServiceColors, fmtDateTime, fmtMs, serviceColorFrom, TRACEQL_POPOUT_QUERY } from './traceqlDetailShared';
+import { copyText } from '@/utils/clipboard';
const props = defineProps<{
ds: TraceQLDatasource;
@@ -77,14 +78,14 @@ function flashCopy(kind: 'id' | 'url'): void {
copyFlashTimer = setTimeout(() => { copyFlash.value = null; }, 1400);
}
function copyTraceId(): void {
- navigator.clipboard?.writeText(props.traceId).then(() => flashCopy('id'), () => {});
+ void copyText(props.traceId).then((done) => { if (done) flashCopy('id'); });
}
function copyShareableUrl(): void {
const url = new URL(globalThis.location.href);
// The REAL id, the one every other surface shows — never the hex wire form.
url.searchParams.set(TRACEQL_POPOUT_QUERY, props.traceId);
url.searchParams.set('ds', props.ds);
- navigator.clipboard?.writeText(url.toString()).then(() => flashCopy('url'), () => {});
+ void copyText(url.toString()).then((done) => { if (done) flashCopy('url'); });
}
onBeforeUnmount(() => { if (copyFlashTimer) clearTimeout(copyFlashTimer); });
</script>
diff --git a/apps/ui/src/layer/traces/TracePopout.vue b/apps/ui/src/layer/traces/TracePopout.vue
index 6ea71dd0..8dc9faa0 100644
--- a/apps/ui/src/layer/traces/TracePopout.vue
+++ b/apps/ui/src/layer/traces/TracePopout.vue
@@ -40,6 +40,7 @@ import { useTracePopout } from '@/layer/traces/useTracePopout';
import { TRACE_POPOUT_SEGMENT, TRACE_POPOUT_SPAN, TRACE_POPOUT_SPAN_INDEX } from './tracePopoutQuery';
import { componentIconOrNull } from '@/layer/service-map/useTopologyIcons';
import { fmtMetric } from '@/utils/formatters';
+import { copyText } from '@/utils/clipboard';
const { t } = useI18n({ useScope: 'global' });
const route = useRoute();
@@ -197,11 +198,11 @@ function flashCopy(kind: 'id' | 'url'): void {
}
function copyId(): void {
if (!openTraceId.value) return;
- navigator.clipboard?.writeText(openTraceId.value).then(() => flashCopy('id'), () => {});
+ void copyText(openTraceId.value).then((done) => { if (done) flashCopy('id'); });
}
function copyShareableUrl(): void {
if (!openTraceId.value) return;
- navigator.clipboard?.writeText(window.location.href).then(() => flashCopy('url'), () => {});
+ void copyText(window.location.href).then((done) => { if (done) flashCopy('url'); });
}
function selectSpan(s: NativeSpan): void { selectedSpan.value = s; }
diff --git a/apps/ui/src/layer/traces/ZipkinTracePopout.vue b/apps/ui/src/layer/traces/ZipkinTracePopout.vue
index c0ed071a..992203cc 100644
--- a/apps/ui/src/layer/traces/ZipkinTracePopout.vue
+++ b/apps/ui/src/layer/traces/ZipkinTracePopout.vue
@@ -43,6 +43,7 @@ import { useZipkinTrace } from '@/layer/traces/useZipkinTraces';
import { useRoute } from 'vue-router';
import { TRACE_POPOUT_SPAN, TRACE_POPOUT_SPAN_INDEX } from './tracePopoutQuery';
import { useEmptyTraceRetry } from './useEmptyTraceRetry';
+import { copyText } from '@/utils/clipboard';
const { openTraceId, openTraceWindow, closeTrace } = useZipkinTracePopout();
const route = useRoute();
@@ -204,7 +205,7 @@ function annotationHint(value: string): string | null {
}
function copyShareableUrl(): void {
if (typeof window === 'undefined') return;
- navigator.clipboard?.writeText(window.location.href).catch(() => {});
+ void copyText(window.location.href);
}
function fmtMs(us: number): string {
@@ -233,7 +234,7 @@ useEscapeToClose(() => openTraceId.value !== null && selectedSpan.value !== null
function copyTraceId(): void {
if (!traceIdRef.value) return;
- navigator.clipboard?.writeText(traceIdRef.value).catch(() => {});
+ void copyText(traceIdRef.value);
}
</script>
diff --git a/apps/ui/src/render/widgets/EvaluationRecordDetailPopout.vue b/apps/ui/src/render/widgets/EvaluationRecordDetailPopout.vue
index f51d0e3d..9bea96b3 100644
--- a/apps/ui/src/render/widgets/EvaluationRecordDetailPopout.vue
+++ b/apps/ui/src/render/widgets/EvaluationRecordDetailPopout.vue
@@ -20,6 +20,7 @@ import { useI18n } from 'vue-i18n';
import type { LogRow } from '@/api/client';
import Modal from '@/components/primitives/Modal.vue';
import { useAuthStore } from '@/state/auth';
+import { copyText } from '@/utils/clipboard';
const { t } = useI18n();
const auth = useAuthStore();
@@ -94,17 +95,13 @@ const copied = ref(false);
let copyTimer: ReturnType<typeof setTimeout> | null = null;
async function copyContent(): Promise<void> {
if (!props.row) return;
- try {
- await navigator.clipboard.writeText(props.row.content);
- copied.value = true;
- if (copyTimer) clearTimeout(copyTimer);
- copyTimer = setTimeout(() => {
- copied.value = false;
- copyTimer = null;
- }, 1500);
- } catch {
- /* clipboard may be blocked; silently no-op */
- }
+ if (!(await copyText(props.row.content))) return;
+ copied.value = true;
+ if (copyTimer) clearTimeout(copyTimer);
+ copyTimer = setTimeout(() => {
+ copied.value = false;
+ copyTimer = null;
+ }, 1500);
}
watch(() => props.row, () => { copied.value = false; });
onBeforeUnmount(() => { if (copyTimer) clearTimeout(copyTimer); });
diff --git a/apps/ui/src/render/widgets/LogDetailPopout.vue b/apps/ui/src/render/widgets/LogDetailPopout.vue
index fb8bfe5d..4647d2e4 100644
--- a/apps/ui/src/render/widgets/LogDetailPopout.vue
+++ b/apps/ui/src/render/widgets/LogDetailPopout.vue
@@ -37,6 +37,7 @@ import { useI18n } from 'vue-i18n';
import type { LogRow } from '@/api/client';
import Modal from '@/components/primitives/Modal.vue';
import { detectFormat, prettyContent, type LogFormat } from './logContent';
+import { copyText } from '@/utils/clipboard';
const { t } = useI18n();
@@ -63,17 +64,13 @@ const copied = ref(false);
let copyTimer: ReturnType<typeof setTimeout> | null = null;
async function copyContent(): Promise<void> {
if (!props.row) return;
- try {
- await navigator.clipboard.writeText(props.row.content);
- copied.value = true;
- if (copyTimer) clearTimeout(copyTimer);
- copyTimer = setTimeout(() => {
- copied.value = false;
- copyTimer = null;
- }, 1500);
- } catch {
- /* clipboard may be blocked; silently no-op */
- }
+ if (!(await copyText(props.row.content))) return;
+ copied.value = true;
+ if (copyTimer) clearTimeout(copyTimer);
+ copyTimer = setTimeout(() => {
+ copied.value = false;
+ copyTimer = null;
+ }, 1500);
}
watch(() => props.row, () => { copied.value = false; });
onBeforeUnmount(() => { if (copyTimer) clearTimeout(copyTimer); });
diff --git a/apps/ui/src/render/widgets/RecordList.vue b/apps/ui/src/render/widgets/RecordList.vue
index a2977d97..4f1b7f52 100644
--- a/apps/ui/src/render/widgets/RecordList.vue
+++ b/apps/ui/src/render/widgets/RecordList.vue
@@ -37,6 +37,7 @@ import { useTracePopout } from '@/layer/traces/useTracePopout';
import { useZipkinTracePopout } from '@/layer/traces/useZipkinTracePopout';
import { useTraceQLTracePopout } from '@/layer/traceql/useTraceQLTracePopout';
import { fmtMetric } from '@/utils/formatters';
+import { copyText } from '@/utils/clipboard';
interface RecordRow {
name: string;
@@ -70,11 +71,7 @@ function openTrace(id: string): void {
const copiedIdx = ref<number | null>(null);
let copiedTimer: ReturnType<typeof setTimeout> | null = null;
async function copyStatement(text: string, idx: number): Promise<void> {
- try {
- await navigator.clipboard.writeText(text);
- } catch {
- /* clipboard blocked (insecure context / no permission) — no-op */
- }
+ if (!(await copyText(text))) return;
copiedIdx.value = idx;
if (copiedTimer) clearTimeout(copiedTimer);
copiedTimer = setTimeout(() => {
diff --git a/apps/ui/src/render/widgets/TraceDetailCard.vue b/apps/ui/src/render/widgets/TraceDetailCard.vue
index b21295fc..eb64aff0 100644
--- a/apps/ui/src/render/widgets/TraceDetailCard.vue
+++ b/apps/ui/src/render/widgets/TraceDetailCard.vue
@@ -53,6 +53,7 @@ import TraceTreeView from './TraceTreeView.vue';
import TraceStatsView from './TraceStatsView.vue';
import SpanDetailModal from './SpanDetailModal.vue';
import { buildServiceColors, fmtMs, fmtDateTime } from './traceDetailShared';
+import { copyText } from '@/utils/clipboard';
const { t } = useI18n({ useScope: 'global' });
@@ -81,7 +82,7 @@ function flashCopy(kind: 'id' | 'url'): void {
}
function copyTraceId(): void {
if (!props.traceId) return;
- navigator.clipboard?.writeText(props.traceId).then(() => flashCopy('id'), () => {});
+ void copyText(props.traceId).then((done) => { if (done) flashCopy('id'); });
}
function copyShareableUrl(): void {
if (!props.traceId) return;
@@ -90,7 +91,7 @@ function copyShareableUrl(): void {
// Tag the source so Trace inspect (one route, two sources) reopens it as
// native rather than Zipkin.
url.searchParams.set('source', 'native');
- navigator.clipboard?.writeText(url.toString()).then(() => flashCopy('url'), () => {});
+ void copyText(url.toString()).then((done) => { if (done) flashCopy('url'); });
}
onBeforeUnmount(() => { if (copyFlashTimer) clearTimeout(copyFlashTimer); });
diff --git a/apps/ui/src/render/widgets/ZipkinTraceDetailCard.vue b/apps/ui/src/render/widgets/ZipkinTraceDetailCard.vue
index e1a3553e..e6e4ac99 100644
--- a/apps/ui/src/render/widgets/ZipkinTraceDetailCard.vue
+++ b/apps/ui/src/render/widgets/ZipkinTraceDetailCard.vue
@@ -36,6 +36,7 @@ import { tagsReportError } from '@skywalking-horizon-ui/api-client';
import { useEscapeToClose } from '@/components/primitives/useEscapeToClose';
import LongValue from '@/components/primitives/LongValue.vue';
import type { ZipkinSpan } from '@/api/client';
+import { copyText } from '@/utils/clipboard';
const { t } = useI18n({ useScope: 'global' });
@@ -56,7 +57,7 @@ const emit = defineEmits<{
function copyTraceId(): void {
if (!props.traceId) return;
- navigator.clipboard?.writeText(props.traceId).catch(() => {});
+ void copyText(props.traceId);
}
function copyShareableUrl(): void {
if (!props.traceId || typeof window === 'undefined') return;
@@ -70,7 +71,7 @@ function copyShareableUrl(): void {
// trace is older than that.
const { t0 } = detailBounds.value;
if (Number.isFinite(t0) && t0 > 0) url.searchParams.set('traceAt', String(Math.floor(t0 / 1000)));
- navigator.clipboard?.writeText(url.toString()).catch(() => {});
+ void copyText(url.toString());
}
interface DetailRow { span: ZipkinSpan; depth: number; offsetUs: number; durUs: number; }
diff --git a/apps/ui/src/utils/clipboard.test.ts b/apps/ui/src/utils/clipboard.test.ts
new file mode 100644
index 00000000..b902bb68
--- /dev/null
+++ b/apps/ui/src/utils/clipboard.test.ts
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { copyText } from './clipboard';
+
+/** What `execCommand('copy')` would put on the clipboard: the selected text
+ * of the text area at the moment it runs. jsdom does not move the focus on
+ * `select()`, so the text area is looked up rather than read from the focus. */
+function stubExecCommand(result: boolean | 'throws') {
+ const copied: string[] = [];
+ const exec = vi.fn((command: string) => {
+ const area = document.querySelector('textarea');
+ if (command === 'copy' && area instanceof HTMLTextAreaElement) {
+ copied.push(area.value.slice(area.selectionStart, area.selectionEnd));
+ }
+ if (result === 'throws') throw new Error('not allowed');
+ return result;
+ });
+ Object.defineProperty(document, 'execCommand', { value: exec, configurable: true });
+ return { exec, copied };
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ document.body.innerHTML = '';
+});
+
+describe('copyText', () => {
+ it('uses the Clipboard API where the page has it', async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ vi.stubGlobal('navigator', { clipboard: { writeText } });
+ const { exec } = stubExecCommand(true);
+ await expect(copyText('trace-1')).resolves.toBe(true);
+ expect(writeText).toHaveBeenCalledWith('trace-1');
+ expect(exec).not.toHaveBeenCalled();
+ });
+
+ it('copies through a selection on a plain HTTP page, which has no Clipboard API', async () => {
+ vi.stubGlobal('navigator', {});
+ const { copied } = stubExecCommand(true);
+ await expect(copyText('trace-1')).resolves.toBe(true);
+ expect(copied).toEqual(['trace-1']);
+ // The text area goes as soon as the copy is done.
+ expect(document.querySelector('textarea')).toBeNull();
+ });
+
+ it('copies through a selection when the Clipboard API refuses', async () => {
+ vi.stubGlobal('navigator', { clipboard: { writeText: vi.fn().mockRejectedValue(new Error('denied')) } });
+ const { copied } = stubExecCommand(true);
+ await expect(copyText('trace-1')).resolves.toBe(true);
+ expect(copied).toEqual(['trace-1']);
+ });
+
+ it('says it did not copy when the selection cannot be copied either', async () => {
+ vi.stubGlobal('navigator', {});
+ stubExecCommand(false);
+ await expect(copyText('trace-1')).resolves.toBe(false);
+ stubExecCommand('throws');
+ await expect(copyText('trace-1')).resolves.toBe(false);
+ expect(document.querySelector('textarea')).toBeNull();
+ });
+
+ it('gives the focus back to what had it', async () => {
+ vi.stubGlobal('navigator', {});
+ stubExecCommand(true);
+ // A browser's select() moves the focus to the text area; jsdom's does not.
+ const select = vi.spyOn(HTMLTextAreaElement.prototype, 'select').mockImplementation(function (this: HTMLTextAreaElement) {
+ this.focus();
+ this.setSelectionRange(0, this.value.length);
+ });
+ const button = document.createElement('button');
+ document.body.appendChild(button);
+ button.focus();
+ await copyText('trace-1');
+ expect(select).toHaveBeenCalled();
+ expect(document.activeElement).toBe(button);
+ select.mockRestore();
+ });
+});
diff --git a/apps/ui/src/utils/clipboard.ts b/apps/ui/src/utils/clipboard.ts
new file mode 100644
index 00000000..118e7b31
--- /dev/null
+++ b/apps/ui/src/utils/clipboard.ts
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Copy text from a click, and say whether it was copied, so a caller shows
+ * "copied" only when it is true.
+ *
+ * A browser has the Clipboard API only in a secure context, HTTPS or
+ * localhost, and a self-hosted Horizon is often plain HTTP on an intranet
+ * address. Without the API, the copy goes through a hidden text area and
+ * `execCommand('copy')` at once, inside the click, which is the user
+ * activation that command needs. When the API is there and refuses, the same
+ * fallback runs once it has answered, which the browser allows only while the
+ * click's activation lasts. The conversation renderer copies the same way.
+ */
+export function copyText(value: string): Promise<boolean> {
+ if (typeof navigator === 'undefined' || !navigator.clipboard) return Promise.resolve(copySelection(value));
+ return navigator.clipboard.writeText(value).then(
+ () => true,
+ () => copySelection(value),
+ );
+}
+
+function copySelection(value: string): boolean {
+ if (typeof document === 'undefined') return false;
+ const focused = document.activeElement;
+ const area = document.createElement('textarea');
+ area.value = value;
+ area.setAttribute('readonly', '');
+ area.style.position = 'fixed';
+ area.style.opacity = '0';
+ document.body.appendChild(area);
+ area.select();
+ try {
+ return document.execCommand('copy');
+ } catch {
+ return false;
+ } finally {
+ area.remove();
+ // Selecting the text area took the focus; give it back, so a copy from a
+ // dialog leaves the keyboard where it was.
+ if (focused instanceof HTMLElement && document.activeElement !== focused) focused.focus({ preventScroll: true });
+ }
+}
diff --git a/docs/changelog/1.1.0.md b/docs/changelog/1.1.0.md
index 0162be77..eff909f5 100644
--- a/docs/changelog/1.1.0.md
+++ b/docs/changelog/1.1.0.md
@@ -266,6 +266,8 @@ The **3D Infra Map** opens framed on the tiers that hold services. A deployment
**On the live debugger's Capture history page, an active session's start time ran into the rule name beside it.** The left column of an *Active* row carries the DSL badge, the LIVE marker and the start time, and had no room for all three, so the time overlapped the `catalog · file · rule` text of the same row. The time now sits under the badges.
+**Copy buttons work when Horizon is served over plain HTTP.** A browser offers its clipboard to a page only over HTTPS or on `localhost`, so on a Horizon opened by an intranet address over plain HTTP most copy buttons did nothing and said nothing: the trace id and link copies on the trace detail and popout of the **Traces**, **Zipkin Traces**, **TraceQL - Native**, **TraceQL - Zipkin** and **OTLP Traces** tabs, the **Copy** of a log and of an evaluation record in their popouts, the click [...]
+
## Traces, logs and events
### Features