From 9b57bd9e214a6f9309c86e21e477f9f66d243cbf Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Tue, 21 Jan 2025 18:20:51 +0100 Subject: [PATCH] Deeplink: Fix `tg://` links on Chrome <130 (#5444) --- src/util/deepLinkParser.ts | 5 +++-- src/util/deeplink.ts | 5 ++++- src/util/windowEnvironment.ts | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/util/deepLinkParser.ts b/src/util/deepLinkParser.ts index 3fe62b714..af8c0f669 100644 --- a/src/util/deepLinkParser.ts +++ b/src/util/deepLinkParser.ts @@ -4,6 +4,7 @@ import { RE_TG_LINK, RE_TME_LINK } from '../config'; import { toChannelId } from '../global/helpers'; import { ensureProtocol } from './ensureProtocol'; import { isUsernameValid } from './username'; +import { IS_BAD_URL_PARSER } from './windowEnvironment'; export type DeepLinkMethod = 'resolve' | 'login' | 'passport' | 'settings' | 'join' | 'addstickers' | 'addemoji' | 'setlanguage' | 'addtheme' | 'confirmphone' | 'socks' | 'proxy' | 'privatepost' | 'bg' | 'share' | 'msg' | 'msg_url' | @@ -148,8 +149,8 @@ function parseDeepLink(url: string) { return parseHttpLink(urlParsed); } if (correctUrl.startsWith('tg:')) { - // Chrome parse url with tg: protocol incorrectly - const urlParsed = new URL(correctUrl.replace(/^tg:/, 'http:')); + const urlToParse = IS_BAD_URL_PARSER ? correctUrl.replace(/^tg:\/\//, 'https://') : correctUrl; + const urlParsed = new URL(urlToParse); return parseTgLink(urlParsed); } return undefined; diff --git a/src/util/deeplink.ts b/src/util/deeplink.ts index f40ec7a08..1f6597895 100644 --- a/src/util/deeplink.ts +++ b/src/util/deeplink.ts @@ -6,6 +6,7 @@ import type { DeepLinkMethod, PrivateMessageLink } from './deepLinkParser'; import { API_CHAT_TYPES, RE_TG_LINK } from '../config'; import { toChannelId } from '../global/helpers'; import { tryParseDeepLink } from './deepLinkParser'; +import { IS_BAD_URL_PARSER } from './windowEnvironment'; export const processDeepLink = (url: string): boolean => { const actions = getActions(); @@ -74,9 +75,11 @@ export const processDeepLink = (url: string): boolean => { return false; } + const urlToParse = IS_BAD_URL_PARSER ? url.replace(/^tg:\/\//, 'https://') : url; + const { protocol, searchParams, hostname, - } = new URL(url); + } = new URL(urlToParse); if (protocol !== 'tg:') return false; diff --git a/src/util/windowEnvironment.ts b/src/util/windowEnvironment.ts index 4b0bb463e..5e1e9229d 100644 --- a/src/util/windowEnvironment.ts +++ b/src/util/windowEnvironment.ts @@ -108,6 +108,8 @@ export const IS_OPEN_IN_NEW_TAB_SUPPORTED = IS_MULTITAB_SUPPORTED && !(IS_PWA && export const IS_TRANSLATION_SUPPORTED = !IS_TEST; export const IS_INTL_LIST_FORMAT_SUPPORTED = 'ListFormat' in Intl; +export const IS_BAD_URL_PARSER = new URL('tg://host').host !== 'host'; + export const MESSAGE_LIST_SENSITIVE_AREA = 750; export const SCROLLBAR_WIDTH = (() => {