这是我第一次学习构建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进行开发。到新存储库的链接已更新。
最佳答案
错误出现在package.json
第6行上:您正在告诉插件sdk,插件的主文件是manage.json
。根据[文档] 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之间的区别,新的要求您编写“清单”。 json´就像已经有了。
更新:
再次:您错过了WebExtensions和基于SDK的插件之间的区别。现在,您进行了WebExtension,但是您尝试使用SDK。这是不可能的。只需直接使用
chrome.tabs
而不是尝试从sdk(var tabs = require("sdk/tabs");
)导入它。关于javascript - 在Firefox WebExtension中使用sdk/tabs时出现ReferenceError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36488114/