Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/mini-app/src/Links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import type { Bridge } from './Bridge.ts'
import type { OutgoingEventMap } from './Events.ts'

export interface Links {
openLink: (
url: string | URL,
options?: {
tryInstantView?: boolean
tryBrowser?:
| 'chrome'
| 'firefox'
| 'edge'
| 'opera'
| 'opera-mini'
| 'brave'
| 'duckduckgo'
| 'samsung'
| 'vivaldi'
| 'kiwi'
| 'uc'
| 'tor'
},
) => void
openTelegramLink: (
url: string | URL,
options?: {
force_request?: boolean
},
) => void
}

export interface InitOptions {
bridge: Bridge
isIframe: boolean
}

export const init = ({
bridge,
isIframe,
}: InitOptions): Links => {
const links: Links = {
openLink: (url_, options = {}) => {
const url = new URL(url_)
const params: OutgoingEventMap['open_link'] = {
url: url.toString(),
}
if (options.tryInstantView) {
params.try_instant_view = true
}
if (options.tryBrowser) {
params.try_browser = options.tryBrowser
}
bridge.emit('open_link', params)
},
openTelegramLink: (url_, options = {}) => {
const url = new URL(url_)
const params: OutgoingEventMap['open_tg_link'] = {
path_full: url.pathname + url.search,
}
if (options.force_request) {
params.force_request = true
}
bridge.emit('open_tg_link', params)
},
}
if (isIframe) {
document.addEventListener('click', (event) => {
if (event.metaKey || event.ctrlKey) {
return
}
let el = event.target as any
if (el == null) {
return
}
while (el.tagName !== 'A' && el.parentNode) {
el = el.parentNode
}
const anchor = el as HTMLAnchorElement
if (
anchor.tagName === 'A' &&
anchor.target !== '_blank' &&
(anchor.protocol === 'http:' || anchor.protocol === 'https:') &&
anchor.hostname === 't.me'
) {
links.openTelegramLink(anchor.href)
event.preventDefault()
}
})
}
return links
}
1 change: 1 addition & 0 deletions packages/mini-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * as Fullscreen from './Fullscreen.ts'
export * as Haptic from './Haptic.ts'
export * as LaunchParams from './LaunchParams.ts'
export * as Lifecycle from './Lifecycle.ts'
export * as Links from './Links.ts'
export * as MainButton from './MainButton.ts'
export * as SecondaryButton from './SecondaryButton.ts'
export * as SessionStorage from './SessionStorage.ts'
Expand Down