From aff64e539057ac6cb6e8fa3d98eb8f9823e1d082 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Sat, 24 Apr 2021 17:35:00 +0400 Subject: [PATCH] Show alert for masked urls --- src/lang.ts | 3 +++ src/lib/appManagers/appImManager.ts | 25 +++++++++++++++++++++++++ src/lib/richtextprocessor.ts | 14 ++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/lang.ts b/src/lang.ts index 1c38802e..49e195b3 100644 --- a/src/lang.ts +++ b/src/lang.ts @@ -403,6 +403,9 @@ const lang = { "HidAccount": "The account was hidden by the user", "TelegramFeatures": "Telegram Features", "SetColor": "Set a color", + "Open": "Open", + "OpenUrlTitle": "Open Link", + "OpenUrlAlert2": "Do you want to open %1$s?", // * macos "AccountSettings.Filters": "Chat Folders", diff --git a/src/lib/appManagers/appImManager.ts b/src/lib/appManagers/appImManager.ts index 56a42acc..3db26806 100644 --- a/src/lib/appManagers/appImManager.ts +++ b/src/lib/appManagers/appImManager.ts @@ -49,6 +49,7 @@ import { hslaStringToHex } from '../../helpers/color'; import { copy, getObjectKeysAndSort } from '../../helpers/object'; import { getFilesFromEvent } from '../../helpers/files'; import PeerTitle from '../../components/peerTitle'; +import PopupPeer from '../../components/popups/peer'; //console.log('appImManager included33!'); @@ -172,6 +173,30 @@ export class AppImManager { sessionStorage.get('chatPositions').then((c) => { sessionStorage.setToCache('chatPositions', c || {}); }); + + (window as any).showMaskedAlert = (element: HTMLAnchorElement, e: Event) => { + cancelEvent(null); + + const href = element.href; + + const a = element.cloneNode(true) as HTMLAnchorElement; + a.innerText = href; + a.removeAttribute('onclick'); + + new PopupPeer('popup-masked-url', { + titleLangKey: 'OpenUrlTitle', + descriptionLangKey: 'OpenUrlAlert2', + descriptionLangArgs: [a], + buttons: [{ + langKey: 'Open', + callback: () => { + a.click(); + }, + }] + }).show(); + + return false; + }; } private onHashChange = () => { diff --git a/src/lib/richtextprocessor.ts b/src/lib/richtextprocessor.ts index 9efa983b..e65f862e 100644 --- a/src/lib/richtextprocessor.ts +++ b/src/lib/richtextprocessor.ts @@ -384,7 +384,8 @@ namespace RichTextProcessor { } }; - for(const entity of entities) { + for(let i = 0, length = entities.length; i < length; ++i) { + const entity = entities[i]; switch(entity._) { case 'messageEntityBold': { if(!options.noTextFormat) { @@ -506,9 +507,18 @@ namespace RichTextProcessor { let inner: string; let url: string; + let masked = false; if(entity._ === 'messageEntityTextUrl') { url = (entity as MessageEntity.messageEntityTextUrl).url; url = wrapUrl(url, true); + + const nextEntity = entities[i + 1]; + if(nextEntity?._ === 'messageEntityUrl' && + nextEntity.length === entity.length && + nextEntity.offset === entity.offset) { + i++; + masked = true; + } } else { url = wrapUrl(entityText, false); //inner = encodeEntities(replaceUrlEncodings(entityText)); @@ -516,7 +526,7 @@ namespace RichTextProcessor { const currentContext = url[0] === '#'; - insertPart(entity, ``, ''); + insertPart(entity, ``, ''); } break;