inspector-commands.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. const l10n = require("gcli/l10n");
  6. const {gDevTools} = require("devtools/client/framework/devtools");
  7. /* eslint-disable mozilla/reject-some-requires */
  8. const {EyeDropper, HighlighterEnvironment} = require("devtools/server/actors/highlighters");
  9. /* eslint-enable mozilla/reject-some-requires */
  10. const Telemetry = require("devtools/client/shared/telemetry");
  11. const windowEyeDroppers = new WeakMap();
  12. exports.items = [{
  13. item: "command",
  14. runAt: "client",
  15. name: "inspect",
  16. description: l10n.lookup("inspectDesc"),
  17. manual: l10n.lookup("inspectManual"),
  18. params: [
  19. {
  20. name: "selector",
  21. type: "string",
  22. description: l10n.lookup("inspectNodeDesc"),
  23. manual: l10n.lookup("inspectNodeManual")
  24. }
  25. ],
  26. exec: function* (args, context) {
  27. let target = context.environment.target;
  28. let toolbox = yield gDevTools.showToolbox(target, "inspector");
  29. let walker = toolbox.getCurrentPanel().walker;
  30. let rootNode = yield walker.getRootNode();
  31. let nodeFront = yield walker.querySelector(rootNode, args.selector);
  32. toolbox.getCurrentPanel().selection.setNodeFront(nodeFront, "gcli");
  33. },
  34. }, {
  35. item: "command",
  36. runAt: "client",
  37. name: "eyedropper",
  38. description: l10n.lookup("eyedropperDesc"),
  39. manual: l10n.lookup("eyedropperManual"),
  40. params: [{
  41. // This hidden parameter is only set to true when the eyedropper browser menu item is
  42. // used. It is useful to log a different telemetry event whether the tool was used
  43. // from the menu, or from the gcli command line.
  44. group: "hiddengroup",
  45. params: [{
  46. name: "frommenu",
  47. type: "boolean",
  48. hidden: true
  49. }, {
  50. name: "hide",
  51. type: "boolean",
  52. hidden: true
  53. }]
  54. }],
  55. exec: function* (args, context) {
  56. if (args.hide) {
  57. context.updateExec("eyedropper_server_hide").catch(e => console.error(e));
  58. return;
  59. }
  60. // If the inspector is already picking a color from the page, cancel it.
  61. let target = context.environment.target;
  62. let toolbox = gDevTools.getToolbox(target);
  63. if (toolbox) {
  64. let inspector = toolbox.getPanel("inspector");
  65. if (inspector) {
  66. yield inspector.hideEyeDropper();
  67. }
  68. }
  69. let telemetry = new Telemetry();
  70. telemetry.toolOpened(args.frommenu ? "menueyedropper" : "eyedropper");
  71. context.updateExec("eyedropper_server").catch(e => console.error(e));
  72. }
  73. }, {
  74. item: "command",
  75. runAt: "server",
  76. name: "eyedropper_server",
  77. hidden: true,
  78. exec: function (args, {environment}) {
  79. let eyeDropper = windowEyeDroppers.get(environment.window);
  80. if (!eyeDropper) {
  81. let env = new HighlighterEnvironment();
  82. env.initFromWindow(environment.window);
  83. eyeDropper = new EyeDropper(env);
  84. eyeDropper.once("hidden", () => {
  85. eyeDropper.destroy();
  86. env.destroy();
  87. windowEyeDroppers.delete(environment.window);
  88. });
  89. windowEyeDroppers.set(environment.window, eyeDropper);
  90. }
  91. eyeDropper.show(environment.document.documentElement, {copyOnSelect: true});
  92. }
  93. }, {
  94. item: "command",
  95. runAt: "server",
  96. name: "eyedropper_server_hide",
  97. hidden: true,
  98. exec: function (args, {environment}) {
  99. let eyeDropper = windowEyeDroppers.get(environment.window);
  100. if (eyeDropper) {
  101. eyeDropper.hide();
  102. }
  103. }
  104. }];