内容脚本中的Firefox SDK访问首选项 [英] Firefox SDK access preferences from content script

查看:147
本文介绍了内容脚本中的Firefox SDK访问首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于



我正在使用Firefox附加组件SDK开发Firefox附加组件。该插件将是特定于网站的,它会根据用户的喜好隐藏某些元素。



几年前我已经制作了这个附加组件,但是新的SDK工作方式有些不同。




$ b

代码



因为这个加载项是特定于站点的,所以我需要修改我使用 PageMod 模块的网站内容

main.js ]

  pageMod.PageMod({
include:* .ipvisie.com,
contentScriptFile:[
data.url('jquery-1.11.1.min.js'),
data.url('script.js')
]
});

这很好,jQuery被实现了,我可以从script.js添加和运行javascript



我已经在' package.json '中声明了这个首选项,这个工作很好。我可以从' main.js '访问它






$ h $问题

我的问题是ContentScript无法访问用户的喜好。



我怎么能访问我的ContentScript' script.js '中的当前首选项?
$ b


尝试过的尝试



尝试1
我试过的第一件事就是从ContentScript请求首选项

  if(require('sdk / simple-prefs')。prefs ['somePreference'] == true){
alert ('Pref checked');

$ / code $ / pre

$ p

尝试2
我在文档中阅读了一些只读参数可以用ContentScript发送。这似乎工作,但是当我改变我的喜好,价值已经设定。

  contentScriptOptions:{
advertLink:require(' ('sdk / simple-prefs')。prefs ['advertTop'],
advertDay:require('sdk / simple-prefs')。prefs ['advertDay'],
advertAdmart:require('sdk / prefs ['advertAdmart'],
advertLink:require('sdk / simple-prefs')。prefs ['advertLink']
}

您可以通过以下消息与内容脚本进行通信:与内容脚本进行通信

创建一个接收已更改的首选项名称和值以及首选项更改监听器将更改发送到脚本:

  pageMod.PageMod({
// ...
onAttach:function(worker ){
worker.port.on(prefChange,函数(prefName,prefValue){
//更新页面...
});

函数onPrefChange(prefName){
self.port.emit(prefChange,prefName,require(sdk / simple-prefs)。prefs [prefName]);

require(sdk / simple-prefs)on(,onPrefChange);
}
});


About

I'm working on a Firefox Add-on using the Firefox Add-on SDK. The add-on will be site specific and it will hide certain elements based on user preferences.

I already made this add-on a few years back, but with the new SDK things work a bit different.


Code

Because the add-on is site specific and I need to modify the content of the site I use the 'PageMod' module

[main.js]

pageMod.PageMod({
  include: "*.ipvisie.com",
  contentScriptFile: [
    data.url('jquery-1.11.1.min.js'),
    data.url('script.js')
  ]
});

This works great, jQuery is implemented and I can add and run javascript from script.js

I have declared the preferences in 'package.json' and this works great. I can access this from 'main.js'


Problem

My problem is that the ContentScript doesn't have access to the user preferences.

How can I gain access to the current preferences in my ContentScript 'script.js'?


Tried attempts

Attempt 1
First thing I tried was just to request the preference from the ContentScript

if (require('sdk/simple-prefs').prefs['somePreference'] == true) {
    alert('Pref checked');
}



Attempt 2
I read in the documentation that some read only parameters can be send with the ContentScript. This seemed to work, but when I changed my preferences the values were already set. Only if I would restart the browser the correct setting would be passed.

contentScriptOptions: {
    advertLink: require('sdk/simple-prefs').prefs['advertTop'],
    advertDay: require('sdk/simple-prefs').prefs['advertDay'],
    advertAdmart: require('sdk/simple-prefs').prefs['advertAdmart'],
    advertLink: require('sdk/simple-prefs').prefs['advertLink']
}

解决方案

You should send the new preferences to the content script every time preferences got changed and not only at the scripts initialisation.

You can communicate with the content script via messages: Communicating With Content Scripts
Create a function that receives changed preference names and values and a preference change listener to send the changes to the script:

pageMod.PageMod({
  // ...
  onAttach: function(worker) {
    worker.port.on("prefChange", function(prefName, prefValue) {
      // update page ...
    });

    function onPrefChange(prefName) {
      self.port.emit("prefChange", prefName, require("sdk/simple-prefs").prefs[prefName]);
    }
    require("sdk/simple-prefs").on("", onPrefChange);
  }
});

这篇关于内容脚本中的Firefox SDK访问首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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