在firefox webextension

时间:2016-04-07 22:10:46

标签: javascript firefox-addon firefox-addon-sdk

这是我第一次学习构建firefox插件。我希望在窗口中存储所有打开的选项卡,为此我需要sdk / tabs。

这是我的js文件:

/*
Given the name of a beast, get the URL to the corresponding image.
*/
debugger;
var tabs = require("sdk/tabs");
function beastNameToURL(beastName) {
  switch (beastName) {
    case "Save Session":
      debugger;
        for (let tab of tabs)
          console.log(tab.url);
        return;
    case "Load Session":
    debugger;
      return chrome.extension.getURL("beasts/snake.jpg");
    case "Turtle":
      return chrome.extension.getURL("beasts/turtle.jpg");
  }
}

/*
Listen for clicks in the popup.

If the click is not on one of the beasts, return early.

Otherwise, the text content of the node is the name of the beast we want.

Inject the "beastify.js" content script in the active tab.

Then get the active tab and send "beastify.js" a message
containing the URL to the chosen beast's image.
*/
document.addEventListener("click", function(e) {

  if (!e.target.classList.contains("btn")) {
    return;
  }


  var chosenBeast = e.target.textContent;
  var chosenBeastURL = beastNameToURL(chosenBeast);

  chrome.tabs.executeScript(null, {
    file: "/content_scripts/beastify.js"
  });

  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {beastURL: chosenBeastURL});
  });

});

当我到达var tabs = require(" sdk / tabs")行时,我收到一条参考错误。 Error snapshot

Github:https://github.com/sagar-shah/Session-manifest

请告诉我如何解决此错误。这是我第一次使用附加组件,我完全迷失了。

提前致谢。

更新: 试图在js文件中全局声明它。现在我收到了标签的未定义错误。

UPDATE2: 正如@matagus指出的那样,我正在使用sdk和webextensions混合开发。我决定使用webextensions进行开发。链接到新存储库已更新。

1 个答案:

答案 0 :(得分:4)

错误发生在package.json第6行:您告诉插件sdk你的插件的主文件是manage.json。根据[docs], main 的值应为:

A string representing the name of a program module that is located in one of the top-level module directories specified by lib. Defaults to "index.js".

因此您需要将其值更改为index.js

除此之外,我认为你错过了Firefox addon built using the addon-sdk(没有'manifest.json'和使用jpm工具构建的)和新的WebExtensions之间的区别。你可以像现有的那样写一个'manifest.json'。

<强>更新

再次说明:你错过了WebExtensions和基于SDK的插件之间的区别。现在你创建了一个WebExtension,但是你正在尝试使用SDK。这是不可能的。只需直接使用chrome.tabs,而不是尝试从sdk(var tabs = require("sdk/tabs");)导入它。

相关问题