Skip to content

Instantly share code, notes, and snippets.

@past
Last active August 29, 2015 14:01
Show Gist options
  • Save past/d92f80f147c06bfacf9c to your computer and use it in GitHub Desktop.
Save past/d92f80f147c06bfacf9c to your computer and use it in GitHub Desktop.
// -sp-context:browser
// Open this file in scratchpad and run it. Output goes to the Browser Console (Cmd-Shift-J).
Components.utils.import("resource://gre/modules/devtools/dbg-server.jsm");
Components.utils.import("resource://gre/modules/devtools/dbg-client.jsm");
let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
let { StyleSheetsFront } = devtools.require("devtools/server/actors/stylesheets");
let client;
function startDebugger()
{
// Start the server.
if (!DebuggerServer.initialized) {
DebuggerServer.init();
DebuggerServer.addBrowserActors();
}
// Listen to an nsIPipe
let transport = DebuggerServer.connectPipe();
// Start the client.
client = new DebuggerClient(transport);
client.connect(onConnected);
}
function shutdownDebugger()
{
client.close();
}
/**
* Start debugging the current tab.
*/
function onConnected()
{
// Get the list of tabs to find the one to attach to.
client.listTabs(function(response) {
// Find the active tab.
let tab = response.tabs[response.selected];
// Attach to the tab.
client.attachTab(tab.actor, function(response, tabClient) {
if (!tabClient) return;
console.log("HTML:", tab.url);
// Attach to the thread (context).
client.attachThread(response.threadActor, function(response, threadClient) {
if (!threadClient) return;
// Fetch the JS sources.
threadClient.getSources(function(response) {
console.log("JS:", response.sources.map(s => s.url));
// Fetch the stylesheets.
let ssf = StyleSheetsFront(client, tab);
ssf.getStyleSheets().then((styleSheets) => {
console.log("CSS:", styleSheets.map(s => s.href));
});
});
// Resume the thread.
threadClient.resume();
// Debugger is now ready and debuggee is running.
});
});
});
}
startDebugger();
// shutdownDebugger();
@janodvarko
Copy link

It's nice example of how a (RDP) connection to the server can be established. Is there also a simple way how to reuse an existing connection (if any) created by native tools and confirmed by the user, from within an Firefox extension?

Honza

@past
Copy link
Author

past commented May 15, 2014

Connections (client endpoints to be precise) are not supposed to be shared among tools. Each tool is expected to create its own connection to the (shared) server. But assuming you just want to augment the native tools, I would say that you need to get a reference to the toolbox first, and get toolbox.target.client from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment