|
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 |
3 | 2 | // |
4 | 3 | // Can use: |
5 | 4 | // chrome.tabs.* |
6 | 5 | // chrome.extension.* |
7 | 6 |
|
| 7 | +var tabInspected; |
8 | 8 | var ports = []; |
9 | 9 | 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 | + }); |
23 | 20 | }); |
24 | 21 |
|
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') { |
27 | 24 | getJsonResource(tabId); |
28 | 25 | } |
29 | 26 | }); |
30 | 27 |
|
| 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 | + |
31 | 38 | // Function to send a message to main.js |
32 | 39 | 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 | + }); |
36 | 44 | } |
37 | 45 |
|
38 | 46 | function getJsonResource(tabID) { |
39 | 47 | 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'; |
42 | 49 | var xhr = new XMLHttpRequest(); |
43 | | - xhr.open("GET", jsonResourceURL, true); |
| 50 | + xhr.open('GET', jsonResourceURL, true); |
44 | 51 | 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 + '"}')); |
48 | 69 | } |
49 | | - } |
| 70 | + }; |
50 | 71 | xhr.send(); |
51 | 72 | }); |
52 | 73 | } |
0 commit comments