Skip to content

Commit 4d66c7c

Browse files
committed
Merge pull request #6 from solidusjs/run-only-on-solidus
Run only on solidus
2 parents 5662f02 + e08db2f commit 4d66c7c

12 files changed

Lines changed: 194 additions & 72 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
[submodule "views/inspector-json"]
2-
path = inspector-json
3-
url = https://github.com/SparkartGroupInc/Inspector-JSON.git

.jshintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"browser": true,
3+
"camelcase": true,
4+
"devel": true,
5+
"eqeqeq": true,
6+
"immed": true,
7+
"maxlen": 80,
8+
"noarg": true,
9+
"predef": [ "chrome", "InspectorJSON" ],
10+
"quotmark": true,
11+
"trailing": true,
12+
"undef": true,
13+
"unused": true,
14+
"node": true
15+
}

Gruntfile.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = function(grunt) {
2+
3+
// Project configuration.
4+
grunt.initConfig({
5+
pkg: grunt.file.readJSON('package.json'),
6+
jshint: {
7+
files: ['*.js', 'views/*.js'],
8+
options: {
9+
jshintrc: true
10+
}
11+
}
12+
});
13+
14+
grunt.loadNpmTasks('grunt-contrib-uglify');
15+
grunt.loadNpmTasks('grunt-contrib-jshint');
16+
grunt.loadNpmTasks('grunt-contrib-nodeunit');
17+
18+
grunt.registerTask('default', ['uglify']);
19+
grunt.registerTask('test', ['jshint']);
20+
21+
};

background.js

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,73 @@
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.*
65
// chrome.extension.*
76

7+
var tabInspected;
88
var ports = [];
99
chrome.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-
if (msg.tabId) {
18-
getJsonResource(msg.tabId);
19-
} else {
20-
console.log(msg);
21-
}
22-
});
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+
});
2320
});
2421

25-
chrome.tabs.onUpdated.addListener(function (tabId, changes, tabObject) {
26-
if (changes.status == "complete") {
22+
chrome.tabs.onUpdated.addListener(function (tabId, changes) {
23+
if (tabId === tabInspected && changes.status === 'complete') {
2724
getJsonResource(tabId);
2825
}
2926
});
3027

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+
3138
// Function to send a message to main.js
3239
function notifyDevtools(msg) {
33-
ports.forEach(function (port) {
34-
port.postMessage(msg);
35-
});
40+
console.log('Background.js Sending Message', msg);
41+
ports.forEach(function (port) {
42+
port.postMessage(msg);
43+
});
3644
}
3745

3846
function getJsonResource(tabID) {
3947
chrome.tabs.get(tabID, function(tab) {
40-
//Before doing this, we should check if it is a Solidus tab
41-
var jsonResourceURL = tab.url+".json";
48+
var jsonResourceURL = tab.url+'.json';
4249
var xhr = new XMLHttpRequest();
43-
xhr.open("GET", jsonResourceURL, true);
50+
xhr.open('GET', jsonResourceURL, true);
4451
xhr.onreadystatechange = function() {
45-
if (xhr.readyState == 4) {
46-
var resp = JSON.parse(xhr.responseText);
47-
notifyDevtools(xhr.responseText);
52+
var isSolidus, errMsg;
53+
isSolidus = (xhr.getResponseHeader('X-Powered-By').match(/Express/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+
}
65+
}
66+
} else {
67+
errMsg = 'Looks like you\'re not inspecting a Solidus Page.';
68+
notifyDevtools(JSON.parse('{"error":"' + errMsg + '"}'));
4869
}
49-
}
70+
};
5071
xhr.send();
5172
});
5273
}

circle.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
machine:
2+
node:
3+
version: 0.10.29
4+
dependencies:
5+
post:
6+
- npm install grunt-cli -g

inspector-json

Lines changed: 0 additions & 1 deletion
This file was deleted.

main.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!doctype html>
22
<html>
3-
<head>
4-
<script src="main.js"></script>
5-
</head>
6-
<body>
7-
</body>
3+
<head>
4+
<script src="main.js"></script>
5+
</head>
6+
<body>
7+
</body>
88
</html>

main.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
1+
// Runs in the context of DevTools including devpanel.js
12
// Can use
23
// chrome.devtools.*
34
// chrome.extension.*
45

5-
chrome.devtools.panels.create("Solidus", "solidus.png", "views/devpanel.html",
6+
chrome.devtools.panels.create('Solidus', 'solidus.png', 'views/devpanel.html',
67
function(panel){
78

8-
var _window;
9-
var data = [];
10-
var port = chrome.runtime.connect({name: 'devtools'});
9+
var _window;
10+
var data = [];
11+
var port = chrome.runtime.connect({name: 'devtools'});
1112

12-
port.onMessage.addListener(function(msg) {
13-
// Send message to devpanel, if it exists.
14-
// If there is no panel yet, queue messages for later.
15-
if (_window) {
16-
_window.sendJsonToInspector(msg);
17-
} else {
18-
data.push(msg);
19-
}
20-
});
13+
port.onMessage.addListener(function(msg) {
2114

22-
panel.onShown.addListener(function tmp(panelWindow) {
23-
panel.onShown.removeListener(tmp); // Only run first time
24-
_window = panelWindow;
15+
console.log('Main.js Recieved Message', msg);
16+
// Send message to devpanel, if it exists.
17+
// If there is no panel yet, queue messages for later.
18+
if (_window) {
19+
_window.processMainIncomingMessage(msg);
20+
} else {
21+
data.push(msg);
22+
}
23+
});
2524

26-
var msg;
27-
while (msg = data.shift())
28-
_window.sendJsonToInspector(msg);
29-
_window.respond = function(msg) {
30-
port.postMessage(msg);
31-
};
25+
panel.onShown.addListener(function tmp(panelWindow) {
26+
panel.onShown.removeListener(tmp); // Only run first time
27+
_window = panelWindow;
3228

33-
panelWindow.respond(chrome.devtools.inspectedWindow);
34-
});
29+
var msg;
30+
while (msg === data) {
31+
msg = data.shift();
32+
_window.processMainIncomingMessage(msg);
33+
}
34+
_window.respond = function(msg) {
35+
console.log('Main.js Sending Message', msg);
36+
port.postMessage(msg);
37+
};
38+
39+
//Tell background.js which tab is being inspected
40+
panelWindow.respond(chrome.devtools.inspectedWindow);
41+
});
3542
});

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "devtools-solidus",
3+
"version": "0.1.0",
4+
"description": "Adds Solidus debugging tools to the Chrome Developer Tools.",
5+
"keywords": [
6+
"Solidus",
7+
"devtools",
8+
"Chrome"
9+
],
10+
"license": "MIT",
11+
"contributors": [
12+
"Josiah Sprague <info@josiahsprague.com> (http://josiahsprague.github.io/)",
13+
"Eric Lanehart <eric@sparkart.com>"
14+
],
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/solidusjs/devtools-solidus.git"
18+
},
19+
"bugs": {
20+
"url": "https://github.com/solidusjs/devtools-solidus/issues"
21+
},
22+
"scripts": {
23+
"test": "grunt test"
24+
},
25+
"devDependencies": {
26+
"grunt": "~0.4.5",
27+
"grunt-contrib-jshint": "~0.10.0",
28+
"grunt-contrib-nodeunit": "~0.3.3",
29+
"grunt-contrib-uglify": "~0.4.0"
30+
},
31+
"dependencies": {
32+
"Inspector-JSON": "SparkartGroupInc/Inspector-JSON"
33+
}
34+
}

0 commit comments

Comments
 (0)