@@ -14,32 +14,29 @@ chrome.runtime.onConnect.addListener(function (port) {
1414 if ( i !== - 1 ) ports . splice ( i , 1 ) ;
1515 } ) ;
1616 port . onMessage . addListener ( function ( msg ) {
17- console . log ( 'Background.js Recieved Message' , msg ) ;
18- processBackgroundIncomingMessage ( msg ) ;
17+ if ( msg . tabId ) {
18+ tabInspected = msg . tabId ;
19+ getJsonResource ( tabInspected ) ;
20+ } else {
21+ console . log ( msg ) ;
22+ }
1923 } ) ;
2024} ) ;
2125
2226chrome . tabs . onUpdated . addListener ( function ( tabId , changes ) {
2327 if ( tabId === tabInspected && changes . status === 'complete' ) {
2428 getJsonResource ( tabId ) ;
29+ notifyDevtools ( 'action' , 'reload' ) ;
2530 }
2631} ) ;
2732
28- function processBackgroundIncomingMessage ( msg ) {
29- console . log ( 'Processing Message in Background' , msg ) ;
30- if ( msg . tabId ) {
31- tabInspected = msg . tabId ;
32- getJsonResource ( tabInspected ) ;
33- } else {
34- console . log ( msg ) ;
35- }
36- }
37-
3833// Function to send a message to main.js
39- function notifyDevtools ( msg ) {
40- console . log ( 'Background.js Sending Message' , msg ) ;
34+ function notifyDevtools ( msgType , payload ) {
35+ var packagedMessage = { } ;
36+ packagedMessage . msgType = msgType ;
37+ packagedMessage . payload = payload ;
4138 ports . forEach ( function ( port ) {
42- port . postMessage ( msg ) ;
39+ port . postMessage ( packagedMessage ) ;
4340 } ) ;
4441}
4542
@@ -49,23 +46,29 @@ function getJsonResource(tabID) {
4946 var xhr = new XMLHttpRequest ( ) ;
5047 xhr . open ( 'GET' , jsonResourceURL , true ) ;
5148 xhr . onreadystatechange = function ( ) {
52- var isSolidus , errMsg ;
53- isSolidus = ( xhr . getResponseHeader ( 'X-Powered-By' ) . match ( / S o l i d u s / i) ) ;
54- if ( xhr . readyState === 4 && isSolidus ) { // Is complete Solidus response?
55- if ( xhr . status !== 200 ) { // Check that Solidus response didn't fail
56- errMsg = 'Failed to get Solidus context. Status: ' + xhr . status ;
57- notifyDevtools ( JSON . parse ( '{"error":"' + errMsg + '"}' ) ) ;
58- } else {
59- try {
60- // Send Solidus JSON to devpanel
61- notifyDevtools ( JSON . parse ( xhr . responseText ) ) ;
62- } catch ( e ) {
63- notifyDevtools ( JSON . parse ( '{"error":"' + e + '"}' ) ) ;
64- }
49+ if ( xhr . readyState !== 4 ) return ; //quickly return if not complete request
50+ var xpbHeader = xhr . getResponseHeader ( 'X-Powered-By' ) || 'unknown' ;
51+ if ( ! xpbHeader . match ( / S o l i d u s / i) ) { //quickly return if not solidus
52+ notifyDevtools ( 'error' , 'No Solidus header detected.' ) ;
53+ notifyDevtools ( 'status' , '<b>not</b> running Solidus 0.1.7 or greater' ) ;
54+ notifyDevtools ( 'action' , 'shutdown' ) ;
55+ return ;
56+ }
57+ notifyDevtools ( 'status' , 'running ' + xpbHeader ) ;
58+ if ( xhr . status !== 200 ) {
59+ notifyDevtools ( 'error' , 'Solidus JSON status: ' + xhr . status ) ;
60+ notifyDevtools ( 'action' , 'soft-shutdown' ) ;
61+ return ;
62+ }
63+ if ( xhr . status === 200 ) {
64+ try {
65+ notifyDevtools ( 'context' , JSON . parse ( xhr . responseText ) ) ;
66+ notifyDevtools ( 'action' , 'reload' ) ;
67+ } catch ( e ) {
68+ notifyDevtools ( 'error' , e ) ;
6569 }
6670 } else {
67- errMsg = 'Looks like you\'re not inspecting a Solidus Page.' ;
68- notifyDevtools ( JSON . parse ( '{"error":"' + errMsg + '"}' ) ) ;
71+ notifyDevtools ( 'status' , 'what' + xhr . status ) ;
6972 }
7073 } ;
7174 xhr . send ( ) ;
0 commit comments