toolbox-init.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. // URL constructor doesn't support about: scheme
  6. let href = window.location.href.replace("about:", "http://");
  7. let url = new window.URL(href);
  8. // Only use this method to attach the toolbox if some query parameters are given
  9. if (url.search.length > 1) {
  10. const Cu = Components.utils;
  11. const Ci = Components.interfaces;
  12. const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
  13. const { gDevTools } = require("devtools/client/framework/devtools");
  14. const { targetFromURL } = require("devtools/client/framework/target-from-url");
  15. const { Toolbox } = require("devtools/client/framework/toolbox");
  16. const { TargetFactory } = require("devtools/client/framework/target");
  17. const { DebuggerServer } = require("devtools/server/main");
  18. const { DebuggerClient } = require("devtools/shared/client/main");
  19. const { Task } = require("devtools/shared/task");
  20. // `host` is the frame element loading the toolbox.
  21. let host = window.QueryInterface(Ci.nsIInterfaceRequestor)
  22. .getInterface(Ci.nsIDOMWindowUtils)
  23. .containerElement;
  24. // Specify the default tool to open
  25. let tool = url.searchParams.get("tool");
  26. Task.spawn(function* () {
  27. let target;
  28. if (url.searchParams.has("target")) {
  29. // Attach toolbox to a given browser iframe (<xul:browser> or <html:iframe
  30. // mozbrowser>) whose reference is set on the host iframe.
  31. // `iframe` is the targeted document to debug
  32. let iframe = host.wrappedJSObject ? host.wrappedJSObject.target
  33. : host.target;
  34. // Need to use a xray and query some interfaces to have
  35. // attributes and behavior expected by devtools codebase
  36. iframe = XPCNativeWrapper(iframe);
  37. iframe.QueryInterface(Ci.nsIFrameLoaderOwner);
  38. if (iframe) {
  39. // Fake a xul:tab object as we don't have one.
  40. // linkedBrowser is the only one attribute being queried by client.getTab
  41. let tab = { linkedBrowser: iframe };
  42. if (!DebuggerServer.initialized) {
  43. DebuggerServer.init();
  44. DebuggerServer.addBrowserActors();
  45. }
  46. let client = new DebuggerClient(DebuggerServer.connectPipe());
  47. yield client.connect();
  48. // Creates a target for a given browser iframe.
  49. let response = yield client.getTab({ tab });
  50. let form = response.tab;
  51. target = yield TargetFactory.forRemoteTab({client, form, chrome: false});
  52. } else {
  53. alert("Unable to find the targetted iframe to debug");
  54. }
  55. } else {
  56. target = yield targetFromURL(url);
  57. }
  58. let options = { customIframe: host };
  59. yield gDevTools.showToolbox(target, tool, Toolbox.HostType.CUSTOM, options);
  60. }).catch(error => {
  61. console.error("Exception while loading the toolbox", error);
  62. });
  63. }