From ae4cf01b7481ffcd7f7ad813367f2a31b9234553 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Mon, 8 Apr 2024 18:49:03 -0700 Subject: [PATCH] msglist: Catch invalid-URL error on link long-press Fixes: #5854 --- src/webview/handleOutboundEvents.js | 21 ++++++++++++++++++--- static/translations/messages_en.json | 3 +++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/webview/handleOutboundEvents.js b/src/webview/handleOutboundEvents.js index 158d574812..08c0063e6e 100644 --- a/src/webview/handleOutboundEvents.js +++ b/src/webview/handleOutboundEvents.js @@ -6,7 +6,7 @@ import * as api from '../api'; import config from '../config'; import type { UserId } from '../types'; import type { JSONableDict } from '../utils/jsonable'; -import { showErrorAlert, showToast } from '../utils/info'; +import { showConfirmationDialog, showErrorAlert, showToast } from '../utils/info'; import { pmKeyRecipientsFromMessage } from '../utils/recipient'; import { isUrlAnImage, tryParseUrl } from '../utils/url'; import * as logging from '../utils/logging'; @@ -209,8 +209,23 @@ const handleLongPress = (args: {| const { _ } = props; if (href !== null) { - const url = new URL(href, props.backgroundData.auth.realm).toString(); - Clipboard.setString(url); + const url = tryParseUrl(href, props.backgroundData.auth.realm); + if (url == null) { + showConfirmationDialog({ + title: 'Copy invalid link', + message: { + text: 'This link appears to be invalid. Do you want to copy it anyway?\n\n{text}', + values: { text: href }, + }, + onPressConfirm: () => { + Clipboard.setString(href); + showToast(_('Text copied')); + }, + _, + }); + return; + } + Clipboard.setString(url.toString()); showToast(_('Link copied')); return; } diff --git a/static/translations/messages_en.json b/static/translations/messages_en.json index 30a425f180..0ffa5d5dfd 100644 --- a/static/translations/messages_en.json +++ b/static/translations/messages_en.json @@ -184,6 +184,9 @@ "Add a reaction": "Add a reaction", "Copy to clipboard": "Copy to clipboard", "Copied": "Copied", + "Copy invalid link": "Copy invalid link", + "This link appears to be invalid. Do you want to copy it anyway?\n\n{text}": "This link appears to be invalid. Do you want to copy it anyway?\n\n{text}", + "Text copied": "Text copied", "Link copied": "Link copied", "This time is in your timezone. Original text was “{originalText}”.": "This time is in your timezone. Original text was “{originalText}”.", "Mute topic": "Mute topic",