Skip to content

Commit

Permalink
Merge pull request #243 from schlagmichdoch/fix-regex-error
Browse files Browse the repository at this point in the history
Fix Error Invalid Group Specifier Name on MacOS Safari and iOS Firefox on older MacOS/iOS
  • Loading branch information
schlagmichdoch authored Jan 21, 2024
2 parents 59360fb + 2e15a01 commit 3fbca72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions public/scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2019,16 +2019,22 @@ class ReceiveTextDialog extends Dialog {
if (text.length < 2000) {
// replace URLs with actual links
this.$text.innerHTML = this.$text.innerHTML
.replace(/(^|(?<=(<br>|\s)))(https?:\/\/|www.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%]){2,}\.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%.]){2,})/g,
(url) => {
.replace(/(^|<br>|\s|")((https?:\/\/|www.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%]){2,}\.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%.]){2,}))/g,
(match, whitespace, 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 `<a href="${link}" target="_blank">${url}</a>`;
// Check if link is valid
if (isUrlValid(link)) {
return `${whitespace}<a href="${link}" target="_blank">${url}</a>`;
}
else {
return match;
}
});
}

Expand Down
10 changes: 10 additions & 0 deletions public/scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,14 @@ async function decodeBase64Text(base64) {
if (!base64) throw new Error('Base64 is empty');

return decodeURIComponent(escape(window.atob(base64)))
}

function isUrlValid(url) {
try {
let urlObj = new URL(url);
return true;
}
catch (e) {
return false;
}
}

0 comments on commit 3fbca72

Please sign in to comment.