Skip to content

Commit b83ca43

Browse files
committed
fix(safari page auto reload): fixed Safari auto refresh and other issues
1 parent fafbd47 commit b83ca43

File tree

4 files changed

+90
-37
lines changed

4 files changed

+90
-37
lines changed

example/basic-dapp/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,11 @@ async function walletInitialized() {
529529
}, 800)
530530
})
531531
})
532-
533-
provider.request({ method: 'anyweb_version' }).then((version) => {
534-
getElement('version').innerHTML = version
535-
})
536532
try {
533+
provider.request({ method: 'anyweb_version' }).then((version) => {
534+
getElement('version').innerHTML = version
535+
})
536+
537537
const [chainId, networkId, alreadyAuthedAddresses] = await Promise.all([
538538
provider.request({ method: 'cfx_chainId' }),
539539
provider.request({ method: 'cfx_netVersion' }),
@@ -557,7 +557,7 @@ async function walletInitialized() {
557557
}
558558
} catch (e) {
559559
unAuthed()
560-
console.error(e)
560+
console.error('try 到错误了', e)
561561
}
562562

563563
connectButton.onclick = () => {
@@ -604,6 +604,9 @@ async function walletInitialized() {
604604
getElement('get_cfx_result').innerHTML = result
605605
console.log('result', result)
606606
})
607+
.catch((e) => {
608+
console.error('获取CFX失败', e)
609+
})
607610
} catch (err) {
608611
console.log('err', err)
609612
}

src/provider.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,23 @@ export class Provider implements IProvider {
167167
? params[0]
168168
: params
169169
: {}
170-
return await callIframe('pages/dapp/auth', {
171-
appId: this.appId,
172-
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
173-
params: params ? JSON.stringify(paramsObj) : '',
174-
authType:
175-
params && Object.keys(paramsObj).includes('to') && paramsObj['to']
176-
? getAddressType(paramsObj['to']) === AddressType.CONTRACT
177-
? 'callContract'
178-
: 'createTransaction'
179-
: 'createContract',
180-
})
170+
try {
171+
return await callIframe('pages/dapp/auth', {
172+
appId: this.appId,
173+
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
174+
params: params ? JSON.stringify(paramsObj) : '',
175+
authType:
176+
params && Object.keys(paramsObj).includes('to') && paramsObj['to']
177+
? getAddressType(paramsObj['to']) === AddressType.CONTRACT
178+
? 'callContract'
179+
: 'createTransaction'
180+
: 'createContract',
181+
})
182+
} catch (e) {
183+
console.error('Error to sendTransaction', e)
184+
return 'fail'
185+
}
186+
181187
case 'anyweb_version':
182188
return config.version
183189
case 'anyweb_home':

src/utils/address.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@ export enum AddressType {
1212
* @param address
1313
*/
1414
export const getAddressType = (address: string): AddressType => {
15-
return decode(address).type as AddressType
15+
const decodeResult = decode(address)
16+
console.log('decodeResult', decodeResult)
17+
if (Object.keys(decodeResult).includes('type')) {
18+
return decodeResult.type as AddressType
19+
} else {
20+
return AddressType.USER
21+
}
1622
}

src/utils/common.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,26 @@ const setBodyScrollable = () => {
6464
export const getIframe = async (
6565
url: string,
6666
onClose: () => void
67-
): Promise<HTMLDivElement> => {
67+
): Promise<{
68+
root: HTMLDivElement
69+
close: () => void
70+
}> => {
71+
if (
72+
document.getElementById('anyweb-iframe-mask') &&
73+
document.getElementById('anyweb-iframe')
74+
) {
75+
const mask = document.getElementById('anyweb-iframe-mask') as HTMLDivElement
76+
const iframe = document.getElementById('anyweb-iframe') as HTMLIFrameElement
77+
iframe.setAttribute('src', url)
78+
mask.style.display = 'block'
79+
return {
80+
root: document.getElementById('anyweb-iframe-mask') as HTMLDivElement,
81+
close: () => {
82+
onClose()
83+
mask.style.display = 'none'
84+
},
85+
}
86+
}
6887
const mask = document.createElement('div')
6988
const div = document.createElement('div')
7089
const iframe = document.createElement('iframe')
@@ -139,6 +158,7 @@ export const getIframe = async (
139158
button.className = 'iframe-contain-button'
140159
iframe.className = 'iframe'
141160

161+
iframe.id = 'anyweb-iframe'
142162
mask.id = 'anyweb-iframe-mask'
143163

144164
iframe.setAttribute('src', url)
@@ -151,13 +171,21 @@ export const getIframe = async (
151171
button.onclick = () => {
152172
setBodyScrollable()
153173
onClose()
154-
mask.remove()
174+
mask.style.display = 'none'
155175
}
156176
document.body.appendChild(style)
157177
setBodyNonScrollable()
158178
document.body.insertBefore(mask, document.body.firstElementChild)
159-
return mask
179+
return {
180+
root: mask,
181+
close: () => {
182+
setBodyScrollable()
183+
onClose()
184+
mask.style.display = 'none'
185+
},
186+
}
160187
}
188+
161189
export const callIframe = async (
162190
path: string,
163191
{
@@ -182,51 +210,61 @@ export const callIframe = async (
182210
console.error('Get serialNumber error', e)
183211
throw new Error('Get serialNumber error')
184212
}
213+
185214
if (waitResult) {
186215
return new Promise<unknown>(async (resolve, reject) => {
187216
let timer: NodeJS.Timeout | undefined = undefined
188-
const iframeContain = await getIframe(
217+
const { close } = await getIframe(
189218
`${BASE_URL}${path}?appId=${appId}&authType=${authType}&serialNumber=${serialNumber}&hash=${hash}&random=${Math.floor(
190219
Math.random() * 1000
191220
)}&chainId=${chainId}&params=${params}&scope=${JSON.stringify(scope)}`,
192221
() => {
193222
if (timer) {
194223
clearTimeout(timer)
195-
reject(new Error('User cancel'))
196224
}
197225
}
198226
)
199-
const delay = 800
227+
const delay = 1000
200228
const next = (i: number) => {
201229
timer = setTimeout(async () => {
230+
let data
202231
try {
203-
const data = (
232+
data = (
204233
await axios.post(`${API_BASE_URL}/open/serial/read`, {
205234
serialNumber: serialNumber,
206235
hash: hash,
207236
})
208237
).data.data
209-
if (data && data !== 'false' && data !== false) {
210-
timer && clearTimeout(timer)
211-
iframeContain.remove()
212-
setBodyScrollable()
213-
resolve(JSON.parse(data))
214-
} else {
215-
if (i * delay > 10 * 60 * 1000) {
216-
iframeContain.remove()
217-
setBodyScrollable()
218-
reject(new Error('Timeout'))
219-
}
220-
next(i++)
221-
}
222238
} catch (e) {
223239
console.error("Can't get result from iframe", e)
240+
next(i++)
241+
return
242+
// reject(new Error("Can't get result from iframe"))
243+
}
244+
if (data && data !== 'false' && data !== false) {
245+
timer && clearTimeout(timer)
246+
close()
247+
resolve(JSON.parse(data))
248+
} else {
249+
if (i * delay > 10 * 60 * 1000) {
250+
close()
251+
reject(new Error('Timeout'))
252+
}
253+
next(i++)
224254
}
225255
}, delay)
226256
}
227257
next(0)
228258
})
229259
}
260+
await getIframe(
261+
`${BASE_URL}${path}?appId=${appId}&authType=${authType}&serialNumber=${serialNumber}&hash=${hash}&random=${Math.floor(
262+
Math.random() * 1000
263+
)}&chainId=${chainId}&params=${params}&scope=${JSON.stringify(scope)}`,
264+
() => {
265+
return
266+
}
267+
)
230268
return 'ok'
231269
}
232270

0 commit comments

Comments
 (0)