@@ -2,8 +2,8 @@ const parseAndGenerate = require('./scripts/parser');
22const injectBundleStr = require ( './scripts/inject_bundle' ) ;
33
44let ports = [ ] ;
5- let interceptedUrl = '' ;
65let reqIndex = 0 ;
6+ const interceptedURLs = { } ;
77
88chrome . tabs . onUpdated . addListener ( ( id , info , tab ) => {
99 if ( tab . status !== 'complete' || tab . url . startsWith ( 'chrome' ) ) return ;
@@ -15,19 +15,26 @@ chrome.tabs.onUpdated.addListener((id, info, tab) => {
1515 runAt : 'document_end' ,
1616 } ) ;
1717
18- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
19- interceptedUrl = '' ;
18+ if ( interceptedURLs . hasOwnProperty ( tab . url ) ) {
19+ delete interceptedURLs [ tab . url ] ;
2020 reqIndex = 0 ;
21- notifyPorts (
22- { action : 'refresh_devtool' , tabId : tabs [ 0 ] . id } ,
23- 'devtools' ,
24- ) ;
25- } ) ;
21+ }
22+
23+ notifyPorts (
24+ { action : 'refresh_devtool' , tabId : tab . id } ,
25+ 'devtools' ,
26+ ) ;
27+
2628} ) ;
2729
2830function handleRequest ( request ) {
2931 // TODO: filter the request from the webRequest call.
30- if ( ! interceptedUrl . startsWith ( request . initiator ) ) return { cancel : false } ;
32+ // We check wether or not the URL should have its requests intercepted
33+ let shouldInterceptUrl = false ;
34+ Object . keys ( interceptedURLs ) . forEach ( ( url ) => {
35+ if ( url . startsWith ( request . initiator ) ) shouldInterceptUrl = true ;
36+ } ) ;
37+ if ( ! shouldInterceptUrl ) return { cancel : false } ;
3138
3239 if ( request . type === 'script' && ! request . url . startsWith ( 'chrome' )
3340 && request . frameId === 0 && ( ( request . url . slice ( - 3 ) === '.js' )
@@ -57,18 +64,15 @@ chrome.runtime.onConnect.addListener((port) => {
5764 if ( ports ) ports . push ( port ) ;
5865
5966 port . onMessage . addListener ( ( msg ) => {
60- if ( msg . turnOnDevtool ) {
61- interceptedUrl = msg . url ;
62- addScriptInterception ( ) ;
63-
64- // after activating our interception script, we refresh the active tab
65- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
66- chrome . tabs . update ( tabs [ 0 ] . id , { url : tabs [ 0 ] . url } ) ;
67- } ) ;
68-
69- } else {
70- console . log ( 'Got a msg not turnOnDevtool: ' , msg ) ;
71- }
67+ if ( ! msg . turnOnDevtool ) return ;
68+ console . log ( 'got turn on: ' , msg ) ;
69+ interceptedURLs [ msg . url ] = true ;
70+ addScriptInterception ( ) ;
71+
72+ // after activating our interception script, we refresh the active tab
73+ chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
74+ chrome . tabs . update ( tabs [ 0 ] . id , { url : tabs [ 0 ] . url } ) ;
75+ } ) ;
7276 } ) ;
7377} ) ;
7478
0 commit comments