【问题标题】:ReferenceError while using sdk/tabs in firefox webextension在 firefox webextension 中使用 sdk/tabs 时出现 ReferenceError
【发布时间】:2016-04-10 08:43:33
【问题描述】:

这是我第一次学习构建 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") 行时,我得到一个参考错误。

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

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

提前致谢。

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

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

【问题讨论】:

  • 不确定,但"main": "manifest.json", 正确吗?在我所有的插件中,它都设置为index.js
  • @FastSnail 我不太确定。在本教程中,主文件以 index.js 的形式给出,但是,当在我的插件中单击按钮时,我想打开一个弹出窗口并且在单击按钮时不运行任何 js 文件。所以我想我会将 manifest.json 添加为 main,因为清单包含弹出文件的详细信息。
  • 我昨天用的很好。尝试在全球范围内声明它 - github.com/Noitidart/jpmOAuth/blob/master/index.js - 您使用的是 jpm runjpm xpi 对吗?意思是你没有做cfx 对吗?由于cfx 已被弃用。
  • @Noitidart 我确实尝试在全局范围内声明它,但现在我收到变量选项卡的未定义错误。是的,我正在使用 jpm run。不是 jpm xpi 或 cfx。
  • 你有这个的 github 仓库吗?可以分享一下吗?

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


【解决方案1】:

错误出现在package.json 第 6 行:您告诉插件 sdk 您插件的主文件是 manage.json。根据 [the docs] 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");) 导入。

【讨论】:

  • 感谢您指出两者之间的区别。我现在已经创建了一个新的存储库,但仍然出现错误。请帮忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多