Skip to content

Commit 5b2761f

Browse files
committed
Use JSON Resource to Determine if Page is Solidus
If not a Solidus page, InspectorJSON will not be initialized and any existing InspectorJSON instances are destroyed.
1 parent 8e46142 commit 5b2761f

3 files changed

Lines changed: 30 additions & 28 deletions

File tree

background.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,22 @@ function notifyDevtools(msg) {
4242
}
4343

4444
function getJsonResource(tabID) {
45+
var pageContext;
4546
chrome.tabs.get(tabID, function(tab) {
46-
//Before doing this, we should check if it is a Solidus tab
4747
var jsonResourceURL = tab.url+".json";
4848
var xhr = new XMLHttpRequest();
4949
xhr.open("GET", jsonResourceURL, true);
5050
xhr.onreadystatechange = function() {
5151
if (xhr.readyState === 4){
5252
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");
53+
try {
54+
pageContext = JSON.parse(xhr.responseText);
55+
notifyDevtools(pageContext);
56+
} catch (e) {
57+
notifyDevtools(JSON.parse('{"error":"' + e + '"}'));
58+
}
59+
} else {
60+
notifyDevtools(JSON.parse('{"error":"No Solidus Page Context Found"}'));
5661
}
5762
}
5863
}

views/devpanel.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
<link href="../inspector-json/inspector-json.css" media="all" rel="stylesheet" type="text/css">
77
</head>
88
<body>
9-
<h2>Page Context</h2>
10-
<div id="pagecontext"></div>
119
<div id="messageholder"></div>
10+
<div id="pagecontext"></div>
1211
</body>
1312
</html>

views/devpanel.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,30 @@
44
// chrome.devtools.*
55
// chrome.extension.*
66

7+
var inspector;
8+
79
function processMainIncomingMessage(msg) {
810
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) {
26-
if(!inspector) {
27-
var inspector = new InspectorJSON({
28-
element: 'pagecontext',
29-
json: '{"hello":"world"}'
11+
if (msg.hasOwnProperty('page')) {
12+
displayMessage("Looks like a Solidus page!");
13+
//Check if there is an initialized InspectorJSON that hasn't been destroyed
14+
if ((inspector instanceof InspectorJSON) && (inspector.page)) {
15+
inspector.view(msg);
16+
}
17+
else {
18+
inspector = new InspectorJSON({
19+
element: 'pagecontext',
20+
json: msg
3021
});
3122
}
32-
inspector.view(msg);
23+
} else if (msg.hasOwnProperty('error')) {
24+
if (inspector instanceof InspectorJSON) {
25+
inspector.destroy();
26+
}
27+
displayMessage(msg.error);
28+
} else {
29+
console.log("Message Not Processed", msg);
30+
}
3331
}
3432

3533
function displayMessage(msg) {

0 commit comments

Comments
 (0)