18807b8ac2
Over plain http on a tailnet address the page is not a secure context and
navigator.clipboard is undefined, so the web engine refused every copy.
ClipboardReportingMessenger now answers clipboard calls through the
browser's legacy clipboard when the API is missing (web only, conditional
import): copy uses execCommand('copy') with a one-shot copy-event listener
(no focus or selection change); a paste key's Clipboard.getData takes the
browser's own paste event, the key reported not-handled so the browser
still fires it. A Paste button/menu says to use Ctrl+V or the https address.
Secure pages keep the engine path. Capture kit: --insecure and clipboard=.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
2.9 KiB
Dart
97 lines
2.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:js_interop';
|
|
import 'dart:js_interop_unsafe';
|
|
|
|
import 'package:web/web.dart' as web;
|
|
|
|
import 'insecure_clipboard.dart';
|
|
|
|
InsecureClipboardHost? insecureClipboardHost() => _BrowserClipboard();
|
|
|
|
/// The legacy clipboard of a page without the Clipboard API.
|
|
class _BrowserClipboard implements InsecureClipboardHost {
|
|
/// `navigator.clipboard` exists only in a secure context (and a browser
|
|
/// new enough to have it); neither changes while the page is open.
|
|
@override
|
|
late final bool unavailable =
|
|
!web.window.isSecureContext ||
|
|
(web.window.navigator as JSObject)
|
|
.getProperty<JSAny?>('clipboard'.toJS)
|
|
.isUndefinedOrNull;
|
|
|
|
/// The copy command writes what its `copy` event puts on the event's
|
|
/// clipboard data, so no element is focused or selected for it: the
|
|
/// Flutter text field keeps its focus, caret and any IME composition, and
|
|
/// a selection in it is not copied instead.
|
|
@override
|
|
bool copy(String text) {
|
|
var written = false;
|
|
void onCopy(web.Event event) {
|
|
final data = (event as web.ClipboardEvent).clipboardData;
|
|
if (data == null) return;
|
|
data.setData('text/plain', text);
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
written = true;
|
|
}
|
|
|
|
final listener = onCopy.toJS;
|
|
web.window.addEventListener(
|
|
'copy',
|
|
listener,
|
|
web.AddEventListenerOptions(capture: true),
|
|
);
|
|
try {
|
|
// False (or no copy event) without a user activation.
|
|
return web.document.execCommand('copy') && written;
|
|
} on Object {
|
|
return false;
|
|
} finally {
|
|
web.window.removeEventListener(
|
|
'copy',
|
|
listener,
|
|
web.EventListenerOptions(capture: true),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Listens on the window in the capture phase, ahead of the composer's
|
|
/// document listener and the focused element's own insertion.
|
|
@override
|
|
Future<String?> nextPaste(Duration timeout) {
|
|
final result = Completer<String?>();
|
|
late final JSFunction listener;
|
|
Timer? timer;
|
|
void finish(String? text) {
|
|
if (result.isCompleted) return;
|
|
timer?.cancel();
|
|
web.window.removeEventListener(
|
|
'paste',
|
|
listener,
|
|
web.EventListenerOptions(capture: true),
|
|
);
|
|
result.complete(text);
|
|
}
|
|
|
|
void onPaste(web.Event event) {
|
|
final data = (event as web.ClipboardEvent).clipboardData;
|
|
final text = data?.getData('text/plain') ?? '';
|
|
if (text.isNotEmpty) {
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
}
|
|
// A paste with no text (files) is left to the page's other listeners.
|
|
finish(text);
|
|
}
|
|
|
|
listener = onPaste.toJS;
|
|
web.window.addEventListener(
|
|
'paste',
|
|
listener,
|
|
web.AddEventListenerOptions(capture: true),
|
|
);
|
|
timer = Timer(timeout, () => finish(null));
|
|
return result.future;
|
|
}
|
|
}
|