在Firefox WebExtension中使用sdk/tabs时出现ReferenceError [英] ReferenceError while using sdk/tabs in firefox webextension

查看:85
本文介绍了在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: https://github.com/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工具构建的)和新的

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆