33 // Check if the content script has already been loaded into the current tab
44 // Prevents it from injecting into the same page twice if the developer opens and closes the dev tool
55 if ( window . myContentScriptLoaded ) {
6- console . log ( ' CONTENT.TS: Content script already injected' ) ;
6+ console . log ( " CONTENT.TS: Content script already injected" ) ;
77 return ;
88 } else {
99 // Set the flag on the window to indicate the content script has already been loaded in the tab
1010 window . myContentScriptLoaded = true ;
11- console . log ( ' CONTENT.TS: Loaded' ) ;
11+ console . log ( " CONTENT.TS: Loaded" ) ;
1212 }
1313
1414 let appConnected = false ;
2020
2121 // Function to setup and initialize the background port
2222 function setupPort ( ) {
23- console . log ( ' CONTENT.TS: Background.ts Connected' ) ;
23+ console . log ( " CONTENT.TS: Background.ts Connected" ) ;
2424
2525 // Connect to background script
26- backgroundPort = chrome . runtime . connect ( { name : ' content-background' } ) ;
26+ backgroundPort = chrome . runtime . connect ( { name : " content-background" } ) ;
2727
2828 // Handle background.ts messages - send message if connected to app otherwise add to queue
29- backgroundPort . onMessage . addListener ( message => {
29+ backgroundPort . onMessage . addListener ( ( message ) => {
3030 if ( appConnected ) {
31- console . log ( ' CONTENT.TS: Message to app' , message ) ;
31+ console . log ( " CONTENT.TS: Message to app" , message ) ;
3232 window . postMessage ( message ) ;
3333 } else {
3434 appMessageQueue . push ( message ) ;
3535 }
3636 } ) ;
3737
3838 backgroundPort . onDisconnect . addListener ( ( ) => {
39- console . log ( ' CONTENT.TS: Background.ts Disconnected' ) ;
39+ console . log ( " CONTENT.TS: Background.ts Disconnected" ) ;
4040 // Reset the port to trigger reconnection attempt
4141 backgroundPort = null ;
4242 setupPort ( ) ;
4949 }
5050
5151 // Add listener to the window to handle messages from the app
52- window . addEventListener ( ' message' , handleMessageFromApp , false ) ;
52+ window . addEventListener ( " message" , handleMessageFromApp , false ) ;
5353
5454 function handleMessageFromApp ( message : MessageEvent ) {
5555 // Initial message from the app to confirm connection
56- if ( message . data ?. type === ' app-connected' ) {
57- console . log ( ' CONTENT.TS: App Connected' ) ;
56+ if ( message . data ?. type === " app-connected" ) {
57+ console . log ( " CONTENT.TS: App Connected" ) ;
5858 clearInterval ( appConnectionInterval ) ;
5959 appConnected = true ;
6060 appMessageQueue . forEach ( ( message : any ) => window . postMessage ( message ) ) ;
6161 appMessageQueue = [ ] ;
6262 }
6363
6464 // All other messages are sent to background.ts
65- if ( message . data ?. type === ' event' ) {
65+ if ( message . data ?. type === " event" ) {
6666 // console.log('CONTENT.TS: Message from App:', message);
6767 sendMessageToBackground ( message ) ;
6868 }
7171 // Notify app that content.ts is ready
7272 function establishAppConnection ( ) {
7373 if ( ! appConnected ) {
74- window . postMessage ( { type : ' content-script-ready' } , '*' ) ;
74+ window . postMessage ( { type : " content-script-ready" } , "*" ) ;
7575 }
7676 }
7777
8181
8282 // Function to send a heartbeat message to the background script to keep it active
8383 function sendHeartbeat ( ) {
84- backgroundPort ?. postMessage ( { type : ' heartbeat' } ) ;
84+ backgroundPort ?. postMessage ( { type : " heartbeat" } ) ;
8585 // console.log('heartbeat');
8686 }
8787
8888 // Call sendHeartbeat function every 25 seconds
8989 setInterval ( sendHeartbeat , 25000 ) ;
90+
91+ // Function to send a message to the app when the component tree is ready
92+ window . addEventListener ( "message" , ( event ) => {
93+ // console.log("message from inject.js", event.data.eventListStr);
94+ if ( event . data . type && event . data . type === "tree" ) {
95+ console . log ( "CONTENT.ts: component tree sending event: " , event ) ;
96+ backgroundPort ?. postMessage ( { type : event . data . type , data : event . data . eventListStr } ) ;
97+ // chrome.runtime.sendMessage({
98+ // action: event.data.type,
99+ // data: event.data.eventListStr,
100+ // });
101+ }
102+ } ) ;
90103} ) ( ) ;
91104
92105// *** Component Tree ***
99112*/
100113const inject = ( fileName : string ) => {
101114 // console.log("CONTENTSCRIPT.JS: INJECTING SCRIPT");
102- const treeScript = document . createElement ( ' script' ) ;
103- treeScript . setAttribute ( ' type' , ' text/javascript' ) ;
104- treeScript . setAttribute ( ' src' , chrome . runtime . getURL ( fileName ) ) ;
115+ const treeScript = document . createElement ( " script" ) ;
116+ treeScript . setAttribute ( " type" , " text/javascript" ) ;
117+ treeScript . setAttribute ( " src" , chrome . runtime . getURL ( fileName ) ) ;
105118 document . body . appendChild ( treeScript ) ;
106119} ;
107120
108121//invoke inject function to inject script
109- inject ( 'inject.js' ) ;
110-
111- // Function to send a message to the app when the component tree is ready
112- window . addEventListener ( 'message' , event => {
113- // console.log("message from inject.js", event.data.eventListStr);
114- if ( event . data . type && event . data . type === 'EVENT_LIST' ) {
115- console . log ( "component tree sending event: " , event ) ;
116- chrome . runtime . sendMessage ( {
117- action : event . data . type ,
118- data : event . data . eventListStr ,
119- } ) ;
120- }
121- } ) ;
122-
122+ inject ( "inject.js" ) ;
123123
124124export { } ;
125125
@@ -135,4 +135,3 @@ export {};
135135// appMessageQueue = [];
136136// console.log('CONTENT.TS: Old content.ts cleaned up.');
137137// }
138-
0 commit comments