@@ -11,9 +11,10 @@ This script is injected into every page and is responsible for:
1111
1212import {
1313 Place_Name , ConnectionName ,
14- port_on_message , port_post_message_obj , port_post_message ,
14+ port_on_message , port_post_message_obj , message ,
1515 window_post_message_obj , window_on_message , window_post_message ,
1616 place_error , place_log ,
17+ type Message ,
1718} from './shared.ts'
1819
1920// @ts -expect-error ?script&module query ensures output in ES module format and only import the script path
@@ -66,23 +67,55 @@ function on_loaded() {
6667
6768const extension_version = chrome . runtime . getManifest ( ) . version
6869
69- const port = chrome . runtime . connect ( { name : ConnectionName . Content } )
70-
70+ let bg_port : chrome . runtime . Port | null = null
7171let devtools_opened = false
72+ let message_queue : Message [ ] = [ ]
7273
73- /* From Background */
74- port_on_message ( port , e => {
75- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
76- switch ( e . kind ) {
77- case 'DevtoolsOpened' :
78- devtools_opened = e . data
79- window_post_message_obj ( e )
80- break
81- default :
82- /* Background -> Client */
83- window_post_message_obj ( e )
74+ let connecting = false
75+ function connect_port ( ) {
76+ if ( connecting ) return
77+
78+ connecting = true
79+ DEV: { place_log ( Place_Name . Content , 'Attempting to connect port...' ) }
80+
81+ try {
82+ let new_port = chrome . runtime . connect ( { name : ConnectionName . Content } )
83+ bg_port = new_port
84+ DEV: { place_log ( Place_Name . Content , 'Port connected successfully' ) }
85+
86+ // Flush queued messages
87+ for ( let m of message_queue . splice ( 0 , message_queue . length ) ) {
88+ port_post_message_obj ( new_port , m )
89+ }
90+
91+ /* From Background */
92+ port_on_message ( new_port , e => {
93+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
94+ switch ( e . kind ) {
95+ case 'DevtoolsOpened' :
96+ devtools_opened = e . data
97+ window_post_message_obj ( e )
98+ break
99+ default :
100+ /* Background -> Client */
101+ window_post_message_obj ( e )
102+ }
103+ } )
104+
105+ new_port . onDisconnect . addListener ( ( ) => {
106+ if ( bg_port === new_port ) {
107+ bg_port = null
108+ setTimeout ( connect_port , 100 )
109+ }
110+ } )
111+ } catch ( err ) {
112+ place_error ( Place_Name . Content , 'Failed to connect port:' , err )
84113 }
85- } )
114+
115+ connecting = false
116+ }
117+
118+ connect_port ( )
86119
87120/* From Client / Detector_Real_World */
88121window_on_message ( e => {
@@ -97,13 +130,20 @@ window_on_message(e => {
97130 'color: #e38b1b' ,
98131 )
99132
100- port_post_message ( port , 'Versions' , {
133+ let versions_message = message ( 'Versions' , {
101134 client : e . data . client ,
102135 solid : e . data . solid ,
103136 extension : extension_version ,
104137 client_expected : import . meta. env . EXPECTED_CLIENT ,
105138 } )
106139
140+ if ( bg_port ) {
141+ port_post_message_obj ( bg_port , versions_message )
142+ } else {
143+ message_queue . push ( versions_message )
144+ connect_port ( )
145+ }
146+
107147 if ( devtools_opened ) {
108148 window_post_message ( 'DevtoolsOpened' , devtools_opened )
109149 }
@@ -112,7 +152,11 @@ window_on_message(e => {
112152 }
113153 default :
114154 /* Client -> Background */
115- port_post_message_obj ( port , e )
155+ if ( bg_port ) {
156+ port_post_message_obj ( bg_port , e )
157+ } else {
158+ message_queue . push ( e )
159+ connect_port ( )
160+ }
116161 }
117162} )
118-
0 commit comments