firefox sdk 附加 html 文件

firefox sdk append html file

我使用以下代码将 html 添加到脚本中的 firefox 选项卡:

main.js:

let worker = tabs.activeTab.attach({
    contentScriptFile: [
      data.url("jquery.min.js"),
      data.url("worker.js")
    ],
    contentScriptOptions: contentOptions
  });
  attach(style, tabs.activeTab);

worker.js:

$('body').append("<div>foo</div>");

我的问题是: 是否可以将 html 代码添加到 html 文件并附加该文件。这将使代码更加美观和可读。

编辑:

如果以前的代码提交给 mozilla 全面审查,它将被拒绝,因为从 HTML 个包含可能未清理数据的字符串创建 DOM 个节点。

一种可能是使用 data.load(name) 并将 HTML 内容发送到您的 contentScript:

main.js

let worker = tabs.activeTab.attach({
    contentScriptFile: [
      data.url("jquery.min.js"),
      data.url("worker.js")
    ],
    contentScriptOptions: contentOptions
  });
worker.port.emit("html", data.load("worker.html"));

worker.js

self.port.on('html', function(contents) {
  $('body').append(contents);
});

worker.html

<div>foo</div>