1- // Chrome automatically creates a background.html page for this to execute.
2- // This can access the inspected page via executeScript
1+ // Chrome automatically creates a background.html page as the context for this
32//
43// Can use:
54// chrome.tabs.*
@@ -14,11 +13,8 @@ chrome.runtime.onConnect.addListener(function (port) {
1413 if ( i !== - 1 ) ports . splice ( i , 1 ) ;
1514 } ) ;
1615 port . onMessage . addListener ( function ( msg ) {
17- if ( msg . tabId ) {
18- getJsonResource ( msg . tabId ) ;
19- } else {
20- console . log ( msg ) ;
21- }
16+ console . log ( "Background.js Recieved Message" , msg ) ;
17+ processBackgroundIncomingMessage ( msg ) ;
2218 } ) ;
2319} ) ;
2420
@@ -28,8 +24,18 @@ chrome.tabs.onUpdated.addListener(function (tabId, changes, tabObject) {
2824 }
2925} ) ;
3026
27+ function processBackgroundIncomingMessage ( msg ) {
28+ console . log ( "Processing Message in Background" , msg )
29+ if ( msg . tabId ) {
30+ getJsonResource ( msg . tabId ) ;
31+ } else {
32+ console . log ( msg ) ;
33+ }
34+ }
35+
3136// Function to send a message to main.js
3237function notifyDevtools ( msg ) {
38+ console . log ( "Background.js Sending Message" , msg ) ;
3339 ports . forEach ( function ( port ) {
3440 port . postMessage ( msg ) ;
3541 } ) ;
@@ -42,9 +48,12 @@ function getJsonResource(tabID) {
4248 var xhr = new XMLHttpRequest ( ) ;
4349 xhr . open ( "GET" , jsonResourceURL , true ) ;
4450 xhr . onreadystatechange = function ( ) {
45- if ( xhr . readyState == 4 ) {
46- var resp = JSON . parse ( xhr . responseText ) ;
47- notifyDevtools ( xhr . responseText ) ;
51+ if ( xhr . readyState === 4 ) {
52+ if ( xhr . status === 200 ) {
53+ notifyDevtools ( xhr . responseText ) ;
54+ } else { //This isn't quite right, because it fires when it shouldnt
55+ notifyDevtools ( "Bad Response" ) ;
56+ }
4857 }
4958 }
5059 xhr . send ( ) ;
0 commit comments