Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: iframe deeplink #3403

Merged
merged 5 commits into from
Dec 6, 2024
Merged
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
20 changes: 20 additions & 0 deletions .changeset/small-games-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-core': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

iframe deeplink fix
13 changes: 11 additions & 2 deletions packages/core/src/utils/CoreHelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { CaipAddress, CaipNetwork } from '@reown/appkit-common'
import type { AccountTypeMap, ChainAdapter, LinkingRecord, NamespaceTypeMap } from './TypeUtil.js'

type SDKFramework = 'html' | 'react' | 'vue'
type OpenTarget = '_blank' | '_self' | 'popupWindow' | '_top'

export const CoreHelperUtil = {
isMobile() {
Expand Down Expand Up @@ -50,6 +51,14 @@ export const CoreHelperUtil = {
navigator.clipboard.writeText(text)
},

isIframe() {
try {
return window.self !== window.top
} catch (e) {
return false
}
},

getPairingExpiry() {
return Date.now() + ConstantsUtil.FOUR_MINUTES_MS
},
Expand Down Expand Up @@ -136,11 +145,11 @@ export const CoreHelperUtil = {

return target
},
openHref(href: string, target: '_blank' | '_self' | 'popupWindow', features?: string) {
openHref(href: string, target: OpenTarget, features?: string) {
window.open(href, this.getOpenTargetForPlatform(target), features || 'noreferrer noopener')
},

returnOpenHref(href: string, target: '_blank' | '_self' | 'popupWindow', features?: string) {
returnOpenHref(href: string, target: OpenTarget, features?: string) {
return window.open(
href,
this.getOpenTargetForPlatform(target),
Expand Down
29 changes: 29 additions & 0 deletions packages/core/tests/utils/CoreHelperUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment jsdom
import { describe, expect, it } from 'vitest'
import { CoreHelperUtil } from '../../src/utils/CoreHelperUtil.js'

Expand Down Expand Up @@ -28,4 +29,32 @@ describe('CoreHelperUtil', () => {
expect(CoreHelperUtil.isAddress(address, chain)).toBe(expected)
}
)

it('should return true when inside an iframe', () => {
lukaisailovic marked this conversation as resolved.
Show resolved Hide resolved
const originalTop = global.window.top
const originalSelf = global.window.self
try {
;(global.window as any).top = { name: 'top' }
;(global.window as any).self = { name: 'self' }

expect(CoreHelperUtil.isIframe()).toBe(true)
} finally {
global.window.top = originalTop
global.window.self = originalSelf
}
})

it('should return false when not inside an iframe', () => {
const originalTop = global.window.top
const originalSelf = global.window.self

try {
global.window.top = global.window.self

expect(CoreHelperUtil.isIframe()).toBe(false)
} finally {
global.window.top = originalTop
global.window.self = originalSelf
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export class W3mConnectingWcMobile extends W3mConnectingWidget {
const { redirect, href } = CoreHelperUtil.formatNativeUrl(mobile_link, this.uri)
ConnectionController.setWcLinking({ name, href })
ConnectionController.setRecentWallet(this.wallet)
CoreHelperUtil.openHref(redirect, '_self')
const target = CoreHelperUtil.isIframe() ? '_top' : '_self'
CoreHelperUtil.openHref(redirect, target)
clearTimeout(this.labelTimeout)
this.secondaryLabel = ConstantsUtil.CONNECT_LABELS.MOBILE
} catch (e) {
Expand Down
Loading