inject.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Services = require("Services");
  6. const { listenOnce } = require("devtools/shared/async-utils");
  7. const l10n = require("gcli/l10n");
  8. exports.items = [
  9. {
  10. item: "command",
  11. runAt: "server",
  12. name: "inject",
  13. description: l10n.lookup("injectDesc"),
  14. manual: l10n.lookup("injectManual2"),
  15. params: [{
  16. name: "library",
  17. type: {
  18. name: "union",
  19. alternatives: [
  20. {
  21. name: "selection",
  22. lookup: [
  23. {
  24. name: "jQuery",
  25. value: {
  26. name: "jQuery",
  27. src: Services.prefs.getCharPref("devtools.gcli.jquerySrc")
  28. }
  29. },
  30. {
  31. name: "lodash",
  32. value: {
  33. name: "lodash",
  34. src: Services.prefs.getCharPref("devtools.gcli.lodashSrc")
  35. }
  36. },
  37. {
  38. name: "underscore",
  39. value: {
  40. name: "underscore",
  41. src: Services.prefs.getCharPref("devtools.gcli.underscoreSrc")
  42. }
  43. }
  44. ]
  45. },
  46. {
  47. name: "url"
  48. }
  49. ]
  50. },
  51. description: l10n.lookup("injectLibraryDesc")
  52. }],
  53. exec: function*(args, context) {
  54. let document = context.environment.document;
  55. let library = args.library;
  56. let name = (library.type === "selection") ?
  57. library.selection.name : library.url;
  58. let src = (library.type === "selection") ?
  59. library.selection.src : library.url;
  60. if (context.environment.window.location.protocol == "https:") {
  61. src = src.replace(/^http:/, "https:");
  62. }
  63. try {
  64. // Check if URI is valid
  65. Services.io.newURI(src, null, null);
  66. } catch(e) {
  67. return l10n.lookupFormat("injectFailed", [name]);
  68. }
  69. let newSource = document.createElement("script");
  70. newSource.setAttribute("src", src);
  71. let loadPromise = listenOnce(newSource, "load");
  72. document.head.appendChild(newSource);
  73. yield loadPromise;
  74. return l10n.lookupFormat("injectLoaded", [name]);
  75. }
  76. }
  77. ];