在Firefox WebExtension中使用sdk/tabs时出现ReferenceError

编程入门 行业动态 更新时间:2024-10-17 21:26:14
本文介绍了在Firefox WebExtension中使用sdk/tabs时出现ReferenceError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

This is my first time learning to build a firefox addon. I want store all the open tabs in a window and for that I require 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")行时,出现参考错误.

When I reach the var tabs = require("sdk/tabs") line I get a Reference error.

Github: github/sagar-shah/Session-manifest

请让我知道如何解决此错误.这是我第一次使用附加组件,我完全迷失了.

Kindly let me know how do I resolve this error. This being my first time with add-ons I am completely lost.

谢谢.

更新: 试图在js文件中全局声明.现在,我在标签页上出现了未定义的错误.

Update: Tried to declare it globally in the js file. Now I am getting undefined error for tabs.

Update2: 我正在混合使用@matagus指出的sdk和webextensions进行开发.我决定使用webextensions进行开发.到新存储库的链接已更新.

Update2: I was mixing up development using sdk and webextensions as pointed out by @matagus. I have decided to go with development using the webextensions. Link to the new repository has been updated.

推荐答案

错误出现在package.json第6行:您正在告诉插件sdk,插件的主文件是manage.json.根据[docs],主要的值应为:

The error is on package.json line 6: you're telling to the addon sdk that the main file of your addon is manage.json. According to [the docs] the value of main should be:

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.

So you need to change its value to index.js.

除此之外,我认为您缺少了使用以下工具构建的Firefox插件之间的区别addon-sdk (没有"manifest.json",而是使用jpm工具构建的)和新的 WebExtensions ,它确实需要您像已经写过的那样编写一个"manifest.json".

Besides that, I think you're missing a difference between Firefox addon built using the addon-sdk (which do not have a ´manifest.json´ and that you build using jpm tool) and the new WebExtensions which do require you to write a ´manifest.json´ like the one already have.

更新:

同样:您缺少WebExtensions和基于SDK的插件之间的区别.现在,您进行了WebExtension,但是您尝试使用SDK.这是不可能的.只需直接使用chrome.tabs,而不是尝试从sdk(var tabs = require("sdk/tabs");)导入它.

Again: you're missing the difference between WebExtensions and SDK-based addons. Now you made a WebExtension but you're trying to use the SDK. It isn't possible. Just use chrome.tabs directly instead of trying to import it from the sdk (var tabs = require("sdk/tabs");).

更多推荐

在Firefox WebExtension中使用sdk/tabs时出现ReferenceError

本文发布于:2023-11-17 08:43:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WebExtension   Firefox   sdk   ReferenceError   tabs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!