@@ -53,6 +53,36 @@ export const setPopupOptions = ({
5353 popupParams = `width=${ width } ,height=${ height } ,top=${ y } ,left=${ x } ,toolbar=no,menubar=no,scrollbars=no,location=no,status=no,popup=1`
5454}
5555
56+ const PopupManager = {
57+ currentPopup : null as Window | null ,
58+
59+ createPopup ( url : string , name : string , params : string ) : Window {
60+ // Close any existing popup
61+ if ( this . currentPopup && ! this . currentPopup . closed ) {
62+ this . currentPopup . close ( )
63+ }
64+
65+ // Create popup immediately
66+ const popup = window . open ( url , name , params )
67+
68+ if ( ! popup ) {
69+ throw new Error ( "Popup blocked by browser" )
70+ }
71+
72+ this . currentPopup = popup
73+
74+ // Monitor popup state
75+ const checkClosed = setInterval ( ( ) => {
76+ if ( popup . closed ) {
77+ clearInterval ( checkClosed )
78+ this . currentPopup = null
79+ }
80+ } , 500 )
81+
82+ return popup
83+ } ,
84+ }
85+
5686// TODO: abstract AppRouter in order to have one single source of truth
5787// At the moment, this is needed
5888const appRouter = t . router ( {
@@ -195,29 +225,35 @@ export const trpcProxyClient = ({
195225 false : popupLink ( {
196226 listenWindow : window ,
197227 createPopup : ( ) => {
198- let popup : Window | null = null
199- const webwalletBtn = document . createElement ( "button" )
200- webwalletBtn . style . display = "none"
201- webwalletBtn . addEventListener ( "click" , ( ) => {
202- popup = window . open (
203- `${ popupOrigin } ${ popupLocation } ` ,
204- "popup" ,
205- popupParams ,
206- )
207- } )
208- webwalletBtn . click ( )
209-
210- // make sure popup is defined
211- ; ( async ( ) => {
212- while ( ! popup ) {
213- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
214- }
215- } ) ( )
228+ try {
229+ let popup : Window | null = null
230+ const button = document . createElement ( "button" )
231+ button . style . position = "fixed"
232+ button . style . top = "-999px"
233+ button . style . left = "-999px"
234+ button . style . opacity = "0"
235+ button . style . pointerEvents = "none" // prevent click event
216236
217- if ( ! popup ) {
218- throw new Error ( "Could not open popup" )
237+ button . addEventListener ( "click" , ( ) => {
238+ popup = PopupManager . createPopup (
239+ `${ popupOrigin } ${ popupLocation } ` ,
240+ "popup" ,
241+ popupParams ,
242+ )
243+ } )
244+
245+ document . body . appendChild ( button )
246+ button . click ( )
247+ button . remove ( )
248+
249+ if ( ! popup ) {
250+ throw new Error ( "Could not open popup" )
251+ }
252+ return popup
253+ } catch ( error ) {
254+ console . error ( "Failed to create popup:" , error )
255+ throw error
219256 }
220- return popup
221257 } ,
222258 postOrigin : "*" ,
223259 } ) ,
0 commit comments