Skip to content

Commit 8e46142

Browse files
committed
separate message processing into separate function and add lots of console.logs to help debugging
1 parent 5662f02 commit 8e46142

4 files changed

Lines changed: 53 additions & 16 deletions

File tree

background.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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
3237
function 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();

main.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runs in the context of DevTools including devpanel.js
12
// Can use
23
// chrome.devtools.*
34
// chrome.extension.*
@@ -10,10 +11,12 @@ function(panel){
1011
var port = chrome.runtime.connect({name: 'devtools'});
1112

1213
port.onMessage.addListener(function(msg) {
14+
15+
console.log("Main.js Recieved Message", msg);
1316
// Send message to devpanel, if it exists.
1417
// If there is no panel yet, queue messages for later.
1518
if (_window) {
16-
_window.sendJsonToInspector(msg);
19+
_window.processMainIncomingMessage(msg);
1720
} else {
1821
data.push(msg);
1922
}
@@ -24,9 +27,11 @@ function(panel){
2427
_window = panelWindow;
2528

2629
var msg;
27-
while (msg = data.shift())
28-
_window.sendJsonToInspector(msg);
30+
while (msg = data.shift()) {
31+
_window.processMainIncomingMessage(msg);
32+
}
2933
_window.respond = function(msg) {
34+
console.log("Main.js Sending Message", msg);
3035
port.postMessage(msg);
3136
};
3237

views/devpanel.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<body>
99
<h2>Page Context</h2>
1010
<div id="pagecontext"></div>
11+
<div id="messageholder"></div>
1112
</body>
1213
</html>

views/devpanel.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
// This runs in the context of the panel in the Dev Tools
1+
// Runs in the context of the DevTools panel
22
//
33
// Can use
44
// chrome.devtools.*
55
// chrome.extension.*
66

7-
//I think the inspector var needs to be global and just updated here
8-
function sendJsonToInspector(msg) {
7+
function processMainIncomingMessage(msg) {
8+
console.log("Devpanel Processing Message", msg);
9+
var msgJson = 0;
10+
try {
11+
msgJson = JSON.parse(msg); //This doesn't work if it's not a JSON string
12+
} catch (e) {
13+
console.log("Ooops, the message wasnt a JSON string");
14+
}
15+
if (msgJson.hasOwnProperty('page')) {
16+
console.log("Message contains page object!");
17+
updateInspectorJSON(msg);
18+
} else if (msg === "Bad Response") {
19+
displayMessage("This isnt what we need");
20+
} else {
21+
console.log("Ooops!", msg.page);
22+
}
23+
}
24+
25+
function updateInspectorJSON(msg) {
926
if(!inspector) {
1027
var inspector = new InspectorJSON({
1128
element: 'pagecontext',
@@ -14,3 +31,8 @@ function sendJsonToInspector(msg) {
1431
}
1532
inspector.view(msg);
1633
}
34+
35+
function displayMessage(msg) {
36+
document.querySelector('#messageholder').innerHTML = msg;
37+
console.log("Updated Panel With Message", msg);
38+
}

0 commit comments

Comments
 (0)