11const parseAndGenerate = require ( './scripts/parser' ) ;
2-
3- let portToDevtools ;
4- const msgsToPanel = [ ] ;
2+ const ports = [ ] ;
53
64chrome . tabs . onUpdated . addListener ( ( id , info , tab ) => {
75 if ( tab . status !== 'complete' || tab . url . startsWith ( 'chrome' ) ) return ;
@@ -13,16 +11,18 @@ chrome.tabs.onUpdated.addListener((id, info, tab) => {
1311 runAt : 'document_end' ,
1412 } ) ;
1513
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' });
14+ chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
15+ notifyPorts (
16+ { action : 'refresh_devtool' , tabId : tabs [ 0 ] . id } ,
17+ 'devtools' ,
18+ ) ;
19+ } ) ;
2020} ) ;
2121
2222
2323let interceptedUrl = '' ;
2424function handleRequest ( request ) {
25- // TODO: filter the request from the webRequest call.
25+ // TODO: filter the request from the webRequest call.
2626 if ( ! interceptedUrl . startsWith ( request . initiator ) ) return { cancel : false } ;
2727
2828 console . log ( 'intercepting... ' , request ) ;
@@ -48,27 +48,20 @@ function handleRequest(request) {
4848// The App on the devtools panel start a connection so that it can
4949// tell us when to start intercepting the script requests.
5050chrome . 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- } ) ;
51+ if ( ports ) ports . push ( port ) ;
6252
6353 port . onMessage . addListener ( ( msg ) => {
64- if ( ! msg . turnOnDevtool ) return ;
65- interceptedUrl = msg . url ;
66- addScriptInterception ( ) ;
54+ if ( msg . turnOnDevtool ) {
55+ interceptedUrl = msg . url ;
56+ addScriptInterception ( ) ;
6757
68- // after activating our interception script, we refresh the active tab
69- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
70- chrome . tabs . update ( tabs [ 0 ] . id , { url : tabs [ 0 ] . url } ) ;
71- } ) ;
58+ // after activating our interception script, we refresh the active tab
59+ chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
60+ chrome . tabs . update ( tabs [ 0 ] . id , { url : tabs [ 0 ] . url } ) ;
61+ } ) ;
62+ } else {
63+ console . log ( 'Got a msg not turnOnDevtool: ' , msg ) ;
64+ }
7265 } ) ;
7366} ) ;
7467
@@ -88,3 +81,14 @@ function sendMessageToContent(codeString) {
8881 chrome . tabs . sendMessage ( tabs [ 0 ] . id , { codeString, index } ) ;
8982 } ) ;
9083}
84+
85+ function notifyPorts ( msg , portName ) {
86+ ports . forEach ( ( port ) => {
87+ if ( portName && ( port . name !== portName ) ) return ;
88+ try {
89+ port . postMessage ( msg ) ;
90+ } catch {
91+ console . log ( 'notifyPorts has found some closed conections.' ) ;
92+ }
93+ } ) ;
94+ }
0 commit comments