77var tabInspected ;
88var ports = [ ] ;
99chrome . runtime . onConnect . addListener ( function ( port ) {
10- if ( port . name !== " devtools" ) return ;
11- ports . push ( port ) ;
12- port . onDisconnect . addListener ( function ( ) {
13- var i = ports . indexOf ( port ) ;
14- if ( i !== - 1 ) ports . splice ( i , 1 ) ;
15- } ) ;
16- port . onMessage . addListener ( function ( msg ) {
17- console . log ( " Background.js Recieved Message" , msg ) ;
18- processBackgroundIncomingMessage ( msg ) ;
19- } ) ;
10+ if ( port . name !== ' devtools' ) return ;
11+ ports . push ( port ) ;
12+ port . onDisconnect . addListener ( function ( ) {
13+ var i = ports . indexOf ( port ) ;
14+ if ( i !== - 1 ) ports . splice ( i , 1 ) ;
15+ } ) ;
16+ port . onMessage . addListener ( function ( msg ) {
17+ console . log ( ' Background.js Recieved Message' , msg ) ;
18+ processBackgroundIncomingMessage ( msg ) ;
19+ } ) ;
2020} ) ;
2121
2222chrome . tabs . onUpdated . addListener ( function ( tabId , changes ) {
23- if ( tabId === tabInspected && changes . status === " complete" ) {
23+ if ( tabId === tabInspected && changes . status === ' complete' ) {
2424 getJsonResource ( tabId ) ;
2525 }
2626} ) ;
2727
2828function processBackgroundIncomingMessage ( msg ) {
29- console . log ( " Processing Message in Background" , msg ) ;
29+ console . log ( ' Processing Message in Background' , msg ) ;
3030 if ( msg . tabId ) {
3131 tabInspected = msg . tabId ;
3232 getJsonResource ( tabInspected ) ;
@@ -37,31 +37,35 @@ function processBackgroundIncomingMessage(msg) {
3737
3838// Function to send a message to main.js
3939function notifyDevtools ( msg ) {
40- console . log ( " Background.js Sending Message" , msg ) ;
41- ports . forEach ( function ( port ) {
42- port . postMessage ( msg ) ;
43- } ) ;
40+ console . log ( ' Background.js Sending Message' , msg ) ;
41+ ports . forEach ( function ( port ) {
42+ port . postMessage ( msg ) ;
43+ } ) ;
4444}
4545
4646function getJsonResource ( tabID ) {
4747 chrome . tabs . get ( tabID , function ( tab ) {
48- var jsonResourceURL = tab . url + " .json" ;
48+ var jsonResourceURL = tab . url + ' .json' ;
4949 var xhr = new XMLHttpRequest ( ) ;
50- xhr . open ( " GET" , jsonResourceURL , true ) ;
50+ xhr . open ( ' GET' , jsonResourceURL , true ) ;
5151 xhr . onreadystatechange = function ( ) {
52- var isSolidus = ( xhr . getResponseHeader ( "X-Powered-By" ) . substring ( 0 , 7 ) === "Express" ) ;
53- if ( xhr . readyState === 4 && isSolidus ) { // Check for completed Solidus response
52+ var isSolidus , errMsg ;
53+ isSolidus = ( xhr . getResponseHeader ( 'X-Powered-By' ) . match ( / E x p r e s s / i) ) ;
54+ if ( xhr . readyState === 4 && isSolidus ) { // Is complete Solidus response?
5455 if ( xhr . status !== 200 ) { // Check that Solidus response didn't fail
55- notifyDevtools ( JSON . parse ( '{"error":"Failed to get Solidus page context."}' ) ) ;
56+ errMsg = 'Failed to get Solidus context. Status: ' + xhr . status ;
57+ notifyDevtools ( JSON . parse ( '{"error":"' + errMsg + '"}' ) ) ;
5658 } else {
5759 try {
60+ // Send Solidus JSON to devpanel
5861 notifyDevtools ( JSON . parse ( xhr . responseText ) ) ;
5962 } catch ( e ) {
6063 notifyDevtools ( JSON . parse ( '{"error":"' + e + '"}' ) ) ;
6164 }
6265 }
6366 } else {
64- notifyDevtools ( JSON . parse ( '{"error":"Looks like you\'re not inspecting a Solidus Page."}' ) ) ;
67+ errMsg = 'Looks like you\'re not inspecting a Solidus Page.' ;
68+ notifyDevtools ( JSON . parse ( '{"error":"' + errMsg + '"}' ) ) ;
6569 }
6670 } ;
6771 xhr . send ( ) ;
0 commit comments