Safe Link: Fix opening links with no protocol (#1556)
This commit is contained in:
parent
50865c78f5
commit
eed0934c0a
@ -6,6 +6,7 @@ import {
|
|||||||
DEBUG, RE_TG_LINK, RE_TME_LINK,
|
DEBUG, RE_TG_LINK, RE_TME_LINK,
|
||||||
} from '../../config';
|
} from '../../config';
|
||||||
import buildClassName from '../../util/buildClassName';
|
import buildClassName from '../../util/buildClassName';
|
||||||
|
import { ensureProtocol } from '../../util/ensureProtocol';
|
||||||
|
|
||||||
type OwnProps = {
|
type OwnProps = {
|
||||||
url?: string;
|
url?: string;
|
||||||
@ -72,14 +73,6 @@ const SafeLink: FC<OwnProps> = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function ensureProtocol(url?: string) {
|
|
||||||
if (!url) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return url.includes('://') ? url : `https://${url}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDomain(url?: string) {
|
function getDomain(url?: string) {
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import { withGlobal } from '../../lib/teact/teactn';
|
|||||||
import { GlobalActions } from '../../global/types';
|
import { GlobalActions } from '../../global/types';
|
||||||
|
|
||||||
import { pick } from '../../util/iteratees';
|
import { pick } from '../../util/iteratees';
|
||||||
|
import { ensureProtocol } from '../../util/ensureProtocol';
|
||||||
import renderText from '../common/helpers/renderText';
|
import renderText from '../common/helpers/renderText';
|
||||||
import useLang from '../../hooks/useLang';
|
import useLang from '../../hooks/useLang';
|
||||||
import useCurrentOrPrev from '../../hooks/useCurrentOrPrev';
|
import useCurrentOrPrev from '../../hooks/useCurrentOrPrev';
|
||||||
@ -20,7 +21,7 @@ const SafeLinkModal: FC<OwnProps & DispatchProps> = ({ url, toggleSafeLinkModal
|
|||||||
const lang = useLang();
|
const lang = useLang();
|
||||||
|
|
||||||
const handleOpen = useCallback(() => {
|
const handleOpen = useCallback(() => {
|
||||||
window.open(url);
|
window.open(ensureProtocol(url));
|
||||||
toggleSafeLinkModal({ url: undefined });
|
toggleSafeLinkModal({ url: undefined });
|
||||||
}, [toggleSafeLinkModal, url]);
|
}, [toggleSafeLinkModal, url]);
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import { IAnchorPosition } from '../../../types';
|
|||||||
|
|
||||||
import { EDITABLE_INPUT_ID } from '../../../config';
|
import { EDITABLE_INPUT_ID } from '../../../config';
|
||||||
import buildClassName from '../../../util/buildClassName';
|
import buildClassName from '../../../util/buildClassName';
|
||||||
|
import { ensureProtocol } from '../../../util/ensureProtocol';
|
||||||
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
||||||
import useShowTransition from '../../../hooks/useShowTransition';
|
import useShowTransition from '../../../hooks/useShowTransition';
|
||||||
import useVirtualBackdrop from '../../../hooks/useVirtualBackdrop';
|
import useVirtualBackdrop from '../../../hooks/useVirtualBackdrop';
|
||||||
@ -280,7 +281,7 @@ const TextFormatter: FC<OwnProps> = ({
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
function handleLinkUrlConfirm() {
|
function handleLinkUrlConfirm() {
|
||||||
const formattedLinkUrl = encodeURI(linkUrl.includes('://') ? linkUrl : `http://${linkUrl}`);
|
const formattedLinkUrl = encodeURI(ensureProtocol(linkUrl) || '');
|
||||||
|
|
||||||
if (isEditingLink) {
|
if (isEditingLink) {
|
||||||
const element = getSelectedElement();
|
const element = getSelectedElement();
|
||||||
|
|||||||
9
src/util/ensureProtocol.ts
Normal file
9
src/util/ensureProtocol.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export function ensureProtocol(url?: string) {
|
||||||
|
if (!url) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTP was chosen by default as a fix for https://bugs.telegram.org/c/10712.
|
||||||
|
// It is also the default protocol in the official TDesktop client.
|
||||||
|
return url.includes('://') ? url : `http://${url}`;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user