|
| 1 | +// Chrome automatically creates a background.html page for this to execute. |
| 2 | +// This can access the inspected page via executeScript |
| 3 | +// |
| 4 | +// Can use: |
| 5 | +// chrome.tabs.* |
| 6 | +// chrome.extension.* |
| 7 | + |
| 8 | +var ports = []; |
| 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 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +chrome.tabs.onUpdated.addListener(function (tabId, changes, tabObject) { |
| 26 | + if (changes.status == "complete") { |
| 27 | + getJsonResource(tabId); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +// Function to send a message to main.js |
| 32 | +function notifyDevtools(msg) { |
| 33 | + ports.forEach(function (port) { |
| 34 | + port.postMessage(msg); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function getJsonResource(tabID) { |
| 39 | + 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"; |
| 42 | + var xhr = new XMLHttpRequest(); |
| 43 | + xhr.open("GET", jsonResourceURL, true); |
| 44 | + xhr.onreadystatechange = function() { |
| 45 | + if (xhr.readyState == 4) { |
| 46 | + var resp = JSON.parse(xhr.responseText); |
| 47 | + notifyDevtools(xhr.responseText); |
| 48 | + } |
| 49 | + } |
| 50 | + xhr.send(); |
| 51 | + }); |
| 52 | +} |
0 commit comments