javascript - 在 Firefox WebExtension 中使用 sdk/tabs 时出现引用错误

标签 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 文件中全局声明它。现在我收到选项卡未定义的错误。

更新2: 正如 @matagus 所指出的,我混淆了使用 sdk 和 webextensions 的开发。我决定使用网络扩展进行开发。新存储库的链接已更新。

最佳答案

错误位于package.json第6行:您告诉插件sdk您的插件的主文件是manage.json。根据[文档],ma​​in 的值应该是:

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");)。

关于javascript - 在 Firefox WebExtension 中使用 sdk/tabs 时出现引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488114/

相关文章:

javascript - selectionStart、selectionEnd 和 focus() 等同于可内容编辑的 span 和 div

javascript - 如何在 YUIDoc 生成的文档中定义自定义主页

javascript - 悬停时抓取链接 - Firefox 插件

javascript - 单击时有反应的 Firefox 插件(左)

javascript - "XMLHttpRequest is not a constructor"错误,与 Mozilla 文档在浏览器 chrome 中的用法相反

javascript - BrowserSync 阻止在 Firefox (OS X) 中导航

URL 中使用的 Javascript

javascript - 如何在本地测试firefox插件

javascript - 使用内容脚本从 get 函数返回值

firefox-addon-sdk - 在 Firefox 扩展中使用 port.emit 和 port.on