Skip to content

Commit

Permalink
Use mount point instead of remote path on Chrome Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sh19910711 committed Aug 18, 2015
1 parent 24ffa5e commit dd6f2e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
19 changes: 10 additions & 9 deletions extensions/chrome/js/background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var tabInfo = {};
var sessions = {};
var ports = {};

initPanelMessage();
Expand All @@ -14,8 +14,8 @@ function panelMessage(tabId, type, msg) {
}
}

function sendSessionId(tabId) {
panelMessage(tabId, 'session-id', { sessionId: tabInfo[tabId].sessionId });
function sendSession(tabId) {
panelMessage(tabId, 'update-session', sessions[tabId]);
}

function removeConsole(tabId) {
Expand All @@ -26,8 +26,8 @@ function initPanelMessage() {
chrome.runtime.onConnect.addListener(onConnect);

function handleMessage(msg) {
if (msg.type === 'session-id') {
sendSessionId(msg.tabId);
if (msg.type === 'session') {
sendSession(msg.tabId);
}
}

Expand All @@ -52,7 +52,7 @@ function initReqRes() {

function handleMessage(req, sender, sendResponse) {
if (req.type === 'request') {
var url = tabInfo[req.tabId].remoteHost + '/' + req.url;
var url = sessions[req.tabId].remoteHost + '/' + req.url;
REPLConsole.request(req.method, url, req.params, function(xhr) {
sendResponse(extractProps(xhr));
});
Expand Down Expand Up @@ -85,8 +85,9 @@ function initHttpListener() {
var headers = getHeaders(details);
var sessionId;
if (sessionId = headers['X-Web-Console-Session-Id']) {
tabInfo[details.tabId] = {
sessions[details.tabId] = {
sessionId: sessionId,
mountPoint: headers['X-Web-Console-Mount-Point'],
remoteHost: details.url.match(/([^:]+:\/\/[^\/]+)\/?/)[1]
};
}
Expand All @@ -97,12 +98,12 @@ function initNavListener() {
// Fired when a document is completely loaded and initialized.
chrome.webNavigation.onCompleted.addListener(function(details) {
if (filter(details)) {
sendSessionId(details.tabId);
sendSession(details.tabId);
removeConsole(details.tabId);
}
});

function filter(details) {
return details.frameId === 0 && tabInfo[details.tabId];
return details.frameId === 0 && sessions[details.tabId];
}
}
15 changes: 8 additions & 7 deletions extensions/chrome/js/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ REPLConsole.request = function(method, url, params, callback) {

// Handle messages from the background script.
port.onMessage.addListener(function(msg) {
if (msg.type === 'session-id') {
updateRemotePath(msg.sessionId);
if (msg.type === 'update-session') {
updateSession(msg);
} else if (msg.type === 'remove-console') {
removeConsole();
}
});

function updateRemotePath(sessionId) {
var remotePath = '__web_console/repl_sessions/' + sessionId;
function updateSession(info) {
if (repl) {
repl.remotePath = remotePath;
repl.sessionId = info.sessionId;
repl.mountPoint = info.mountPoint;
} else {
repl = REPLConsole.installInto('console', { remotePath: remotePath });
var options = { sessionId: info.sessionId, mountPoint: info.mountPoint };
repl = REPLConsole.installInto('console', options);
}
}

Expand All @@ -36,5 +37,5 @@ function removeConsole() {
chrome.devtools.inspectedWindow.eval(script);
}

port.postMessage({ type: 'session-id', tabId: tabId });
port.postMessage({ type: 'session', tabId: tabId });
removeConsole();

0 comments on commit dd6f2e6

Please sign in to comment.