You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
angularjs-batarang/background.js

51 lines
1.1 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// tabId -> devtool port
var inspectedTabs = {};
// TODO: keep track of app state here
// tabId -> list of buffered events
var buffer = {};
function bufferOrForward(message, sender) {
var tabId = sender.tab.id,
devToolsPort = inspectedTabs[tabId];
if (devToolsPort) {
devToolsPort.postMessage(message);
}
if (!buffer[tabId] || message === 'refresh') {
resetState(tabId);
}
buffer[tabId].push(message);
}
// context script > background
chrome.runtime.onMessage.addListener(bufferOrForward);
chrome.runtime.onConnect.addListener(function(devToolsPort) {
devToolsPort.onMessage.addListener(registerInspectedTabId);
function registerInspectedTabId(inspectedTabId) {
inspectedTabs[inspectedTabId] = devToolsPort;
if (!buffer[inspectedTabId]) {
resetState(inspectedTabId);
}
buffer[inspectedTabId].forEach(function(msg) {
devToolsPort.postMessage(msg);
});
devToolsPort.onDisconnect.addListener(function () {
delete inspectedTabs[inspectedTabId];
});
//devToolsPort.onMessage.removeListener(registerInspectedTabId);
}
});
function resetState(tabId) {
buffer[tabId] = [];
}