Firefox扩展插件标签使用后台脚本 [英] Firefox extension addon tab using background script

查看:161
本文介绍了Firefox扩展插件标签使用后台脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firefox扩展,打开了一个包含位于我的数据文件夹中的html文件的选项卡。 main.js执行以下操作:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ tabbsopen(login.html );



$ b在main.js中,我需要()一堆脚本作为背景运行脚本。这个html文件作为扩展名的登录名。这个html页面如何能够访问后台脚本?

解决方案

它不能。您需要将内容脚本附加到您的登录页面,并以标准方式向其发送变量 using port



另外,你的代码是否工作?你不需要 require(sdk / self).data 可以访问 login.html



以下是您可以做的一个例子。

main.js

  const {data} = require('sdk / self'); 

函数handleClick(state){
tabs.open({
url:data.url('login.html'),
onOpen:function(tab) {
var worker = tab.attach({
contentScriptFile:data.url('login.js')
});
worker.port.emit('foo', foo);
worker.port.on('bar',handleBar);
}
});


函数handleBar(bar){
//用bar做点什么;

login.js

  self.port.on('foo',function(foo){
//用foo
做某事)

self.port.emit('bar',bar);


I have a firefox extension that opens up a tab with a html file that is located in my data folder. The main.js does this:

 function handleClick(state) {
   tabs.open("login.html");
  }

In main.js I require() a bunch of scripts to run as background scripts. This html file acts as the "login" for the extension. How can this html page have access to the background scripts?

解决方案

It can't. You'll need to attach a content script to your login page and to send variables to it the standard way using port.

Also, does your code work? Don't you need to require(sdk/self).data to get access to login.html?

Here's an example of what you can do.

main.js

const { data } = require('sdk/self');

function handleClick(state) {
  tabs.open({
    url: data.url('login.html'),
    onOpen: function(tab) {
      var worker = tab.attach({
        contentScriptFile: data.url('login.js')
      });
      worker.port.emit('foo', foo);
      worker.port.on('bar', handleBar);
    }
  });
}

function handleBar(bar) {
  // do something with bar;
}

login.js

self.port.on('foo', function(foo) {
  // do something with foo
});

self.port.emit('bar', bar);

这篇关于Firefox扩展插件标签使用后台脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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