【发布时间】: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 run或jpm xpi对吗?意思是你没有做cfx对吗?由于cfx已被弃用。 -
@Noitidart 我确实尝试在全局范围内声明它,但现在我收到变量选项卡的未定义错误。是的,我正在使用 jpm run。不是 jpm xpi 或 cfx。
-
你有这个的 github 仓库吗?可以分享一下吗?
标签: javascript firefox-addon firefox-addon-sdk