From dd0dc21db5aaf5e4e298d145e675614559083e2a Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Wed, 3 Jan 2024 13:54:13 +0100 Subject: [PATCH] Fix replacement of sent URLs with actual links (fixes #231) --- public/scripts/ui.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/public/scripts/ui.js b/public/scripts/ui.js index ae63a0e..9cf0b3b 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -2014,10 +2014,19 @@ class ReceiveTextDialog extends Dialog { // Beautify text if text is short if (text.length < 2000) { - // replace urls with actual links - this.$text.innerHTML = this.$text.innerHTML.replace(/((https?:\/\/|www)[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\-._~:\/?#\[\]@!$&'()*+,;=]+)/g, url => { - return `${url}`; - }); + // replace URLs with actual links + this.$text.innerHTML = this.$text.innerHTML + .replace(/(^|(?<=(
|\s)))(https?:\/\/|www.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%]){2,}\.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%.]){2,})/g, + (url) => { + let link = url; + + // prefix www.example.com with http protocol to prevent it from being a relative link + if (link.startsWith('www')) { + link = "http://" + link + } + + return `${url}`; + }); } this._evaluateOverflowing(this.$text); @@ -2048,6 +2057,7 @@ class ReceiveTextDialog extends Dialog { } hide() { + // Todo: clear text field super.hide(); setTimeout(() => this._dequeueRequests(), 500); }