11const parseAndGenerate = require ( './scripts/parser' ) ;
22
3+ let portToDevtools ;
4+ const msgsToPanel = [ ] ;
5+
36chrome . tabs . onUpdated . addListener ( ( id , info , tab ) => {
47 if ( tab . status !== 'complete' || tab . url . startsWith ( 'chrome' ) ) return ;
58
9+ // active page action button and inject extension.js
610 chrome . pageAction . show ( tab . id ) ;
711 chrome . tabs . executeScript ( null , {
812 file : 'extension.js' ,
913 runAt : 'document_end' ,
1014 } ) ;
15+
16+ // refresh devtool panel everytime we refresh webpage
17+ // console.log('port: ', portToDevtools);
18+ // if (portToDevtools) portToDevtools.postMessage({ action: 'refresh_devtool' });
19+ // else msgsToPanel.push({ action: 'refresh_devtool' });
1120} ) ;
1221
1322
@@ -29,7 +38,6 @@ function handleRequest(request) {
2938 const syncRequest = new XMLHttpRequest ( ) ;
3039 syncRequest . open ( 'GET' , request . url , false ) ;
3140 syncRequest . send ( null ) ;
32- console . log ( `Status: ${ syncRequest . status } - Size of response: ${ syncRequest . responseText . length } ` ) ;
3341
3442 sendMessageToContent ( parseAndGenerate ( syncRequest . responseText ) ) ;
3543
@@ -40,6 +48,18 @@ function handleRequest(request) {
4048// The App on the devtools panel start a connection so that it can
4149// tell us when to start intercepting the script requests.
4250chrome . runtime . onConnect . addListener ( ( port ) => {
51+ portToDevtools = port ;
52+
53+ // if (msgsToPanel.length > 0) {
54+ // for (let msg of msgsToPanel) port.postMessage(msg);
55+ // }
56+ // we change the port to null when we disconnect, so that when we refresh
57+ // the page by start recording, we can check if (!port) and not refresh
58+ // the devtools page.
59+ port . onDisconnect . addListener ( ( ) => {
60+ portToDevtools = null ;
61+ } ) ;
62+
4363 port . onMessage . addListener ( ( msg ) => {
4464 if ( ! msg . turnOnDevtool ) return ;
4565 interceptedUrl = msg . url ;
@@ -64,7 +84,6 @@ function addScriptInterception() {
6484let reqIndex = 0 ;
6585function sendMessageToContent ( codeString ) {
6686 const index = reqIndex ++ ;
67- console . log ( `Sending request ${ index } .` ) ;
6887 chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
6988 chrome . tabs . sendMessage ( tabs [ 0 ] . id , { codeString, index } ) ;
7089 } ) ;
0 commit comments