简体   繁体   English

Firefox扩展。 如何从控制台访问“浏览器”名称空间?

[英]Firefox extension. How to access “browser” namespace from console?

I'm trying of the console to access the "browser" environment ex browser.cookies.getAll but this not defined anywhere except extension environment. 我正在尝试通过控制台访问browser.cookies.getAll之类的“浏览器”环境,但是除了扩展环境之外,其他地方都没有定义。

If make simple firefox addon (extension) with one .js file where browser API request: browser.cookies.getAll({}).then(console.log); 如果使用浏览器API请求的一个.js文件制作简单的firefox插件(扩展名): browser.cookies.getAll({}).then(console.log);

get an array with an interactive preview. 获得具有交互式预览的数组。

Execute from extension 从扩展执行

从扩展执行

If execute this command in console 如果在控制台中执行此命令

从控制台执行

How to access "browser" namespace from console? 如何从控制台访问“浏览器”名称空间?

This is not possible, browser.* or chrome.* are not available on developer console because they need an extension's context to run and developer console runs commands on the context of current page. 这是不可能的,因为browser.*chrome.*在开发者控制台上不可用,因为它们需要扩展程序的上下文才能运行,并且开发者控制台在当前页面的上下文中运行命令。

The following approach requires learning/knowledge of unit testing and integration testing in JavaScript and node.js, the example provided is over-simplified, this is by no means production ready code. 以下方法要求学习/了解JavaScript和node.js中的单元测试和集成测试,所提供的示例过于简化,这绝不是生产就绪的代码。


A better approach for testing your extensions and debugging it is to write tests for it. 测试扩展和调试的更好方法是为其编写测试。

  1. Choose a testing framework (Jest, Mocha + chai, etc) and set it up according to your needs 选择一个测试框架(Jest,Mocha + chai等)并根据需要进行设置

  2. Install sinon-chrome package which provides you with stubs for browser.* methods/apis by running npm install --save-dev sinon-chrome 通过运行npm install --save-dev sinon-chrome安装sinon-chrome软件包,该软件包为您提供browser.* 存根 browser.*方法/ api。

  3. (Optional) Install webextensions-api-fake which provides you with mocks for browser.* methods/apis by running npm install --save-dev webextensions-api-fake (可选)安装webextensions-API假它将为您提供的嘲弄 browser.*运行方法/ API的npm install --save-dev webextensions-api-fake

  4. (Optional) Install webextensions-jsdom which helps you to write tests for your browser_action default_popup, sidebar_action default_panel or background page/scripts (可选)安装webextensions-jsdom ,可帮助您为browser_action default_popup,sidebar_action default_panel或背景页面/脚本编写测试

  5. Start writing tests by following the example below 按照以下示例开始编写测试

  6. In order to debug your extension, set a breakpoint in your IDE/Editor of choice and run the tests, the execution will stop at the breakpoint and you will have access the states of Objects and Variables at that time in execution. 为了调试您的扩展,请在您选择的IDE /编辑器中设置一个断点并运行测试,该执行将在该断点处停止,并且您将在执行时访问对象和变量的状态。 This will help you know what and how exactly things are executing and what's happening to the data you pass around in functions. 这将帮助您了解函数执行的内容,执行的方式以及执行过程中传递的数据的执行情况。 There is no need for writing console.log statements everywhere for checking your output or variables, debuggers can help with that. 无需到处编写console.log语句来检查您的输出或变量,调试器可以帮助您。

  7. (Optional) webextensions-toolbox is another great tool for writing cross-browser extensions (Your extension will work on chrome, firefox, opera, edge) with the same code base. (可选) webextensions-toolbox是另一个使用相同代码库编写跨浏览器扩展(您的扩展将适用于chrome,firefox,opera,edge)的出色工具。 This also comes with hot-reloading of your extension page , so you don't have to hit refresh every time you make any changes. 这也随扩展page热重装一起提供,因此您不必每次进行任何更改都刷新。

By following this approach, it will improve your development workflow and will reduce the number of times you have to hit refresh on your browser. 通过遵循这种方法,它将改善您的开发工作流程,并减少您必须在浏览器上单击刷新的次数。

Example usage of sinon-chrome stubs using jest testing framework. 通过玩笑测试框架使用sinon-chrome存根的示例用法。
Lets say you have written your code in yourModule.js then to test/verify that it works in yourModule.test.js you write: 假设您已经在yourModule.js编写了代码,然后通过编写以下代码来测试/验证其是否在yourModule.test.jsyourModule.test.js

import browser from 'sinon-chrome';
import yourModule from './lib/yourModule';

describe('moduleName', () => {
  beforeAll(() => {
    // To make sure yourModule uses the stubbed version
    global.browser = browser;
  });
  it('does something', async () => {
    await yourModule();
    // Lets assume your module creates two tabs
    expect(browser.tabs.create.calledTwice).toBe(true);
    // If you want to test how those browser methods where called
    expect(browser.tabs.create.firstCall.calledWithExactly({
      url: 'https://google.com',
    })).toBe(true);
    // Notice the usage of `.firstCall` here, this makes sure only the first time 
    // `browser.tabs.create` was called with the given args.
  });
});

When you run this test using jest, yourModule will expect there to exist a global variable browser with the apis it uses which is only possible in a real browser, but we faked it using the sinon-chrome package, your module will execute in node.js environment as expected. 当您使用jest运行此测试时,yourModule会期望存在一个使用它的api的全局变量browser ,只有在实际的浏览器中才有可能,但是我们使用sinon-chrome包伪造了它,您的模块将在node中执行。 js环境符合预期。

You don't need to run it in the browser to see changes. 您无需在浏览器中运行它即可查看更改。 You just write tests, write code to pass those tests and when all tests pass. 您只需编写测试,编写代码以通过这些测试以及所有测试均通过。 Check your extension by running it in the browser, at this point in time your extension will run as you'd expect it to. 通过在浏览器中运行扩展程序来检查扩展程序,此时扩展程序将按预期运行。 If you add another feature to yourModule and your tests fail you know exactly what went wrong. 如果您向您的模块添加了另一个功能,而您的测试失败了,您将确切知道出了什么问题。

However the above example only makes sure how browser.* methods/apis were called, for you to test the behavior of yourModule you'd need to mock those methods/apis, this is were the webextensions-api-fake package comes in. You can find example in its repo on github. 但是,上面的示例仅确保了browser.*方法/ api的调用方式,您需要模拟那些方法/ api来测试yourModule的行为,这是webextensions-api-fake软件包的附带内容。可以在github的仓库中找到示例

Examples for testing your browser_action default_popup, sidebar_action default_panel or background page/scripts are also provided in the webextensions-jsdom repo on github. github上的webextensions-jsdom存储库中还提供了用于测试browser_action default_popup,sidebar_action default_panel或背景页面/脚本的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM