使用simple-prefs模块并将值导出到存储在数据文件夹中的脚本中 [英] Work with the simple-prefs module and export the values to a script stored in the data folder

查看:70
本文介绍了使用simple-prefs模块并将值导出到存储在数据文件夹中的脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试向Firefox附加组件添加一些首选项.为此,我正在使用新的"simple-prefs"模块. ( Simple-Prefs )

I'm currently trying to add some preferences to a Firefox add-on. To do so, I'm playing with the new "simple-prefs" module. (Simple-Prefs on the Mozilla Blog)

documentation 的内容不是很详细,在理解如何检索选项的附加值并将其导出到数据文件夹中存在的JS脚本中时,我遇到了一些问题.

The documentation is not very detailed and I face some problems understanding how I can retrieve the value attached to an option and export it to a JS script present in my data folder.

假设我的插件中只有一个可选设置,一个布尔设置,那么我的packages.json将如下所示:

Let's say that I have only one optional setting in my addon, a boolean one, then my packages.json will look like this:

{
  "name": "test",
  ...
  "preferences": [{
    "type": "bool",
    "name": "option1",
    "value": true,
    "title": "Desc Option 1" 
  }]
}

这是我的main.js文件[更新]:

Here is my main.js file [UPDATED]:

var pageMod = require("page-mod");
const data = require("self").data;
const prefSet = require("simple-prefs");  //Simple-prefs module

var option1 = prefSet.prefs.option1;     //get the value for option1

function onPrefChange(prefName) {        //Listen for changes

   var prefName = prefSet.prefs[prefName];
                      }

prefSet.on("option1", onPrefChange);


exports.main = function() {
  pageMod.PageMod({ 
    include: ["https://mail.google.com/*","http://mail.google.com/*"],
    contentScriptWhen: 'ready',
    contentScriptFile: [data.url("jquery.js"),data.url("script.js")],
    onAttach: function(worker)
        {
      worker.postMessage( option1 );
        }
    });
}

如何检索"option1"所附的值并将其导出以便在我的"script.js"文件中进行调用?

推荐答案

通常,内容脚本无权访问API-它们只能

As usually, content scripts don't have access to the API - they can only receive messages from your extension's scripts. Here you would do:

pageMod.PageMod({ 
  include: ["https://mail.google.com/*","http://mail.google.com/*"],
  contentScriptWhen: 'ready',
  contentScriptFile: [data.url("jquery.js"),data.url("script.js")],
  onAttach: function(worker)
  {
    worker.postMessage(backtop);
  }
});

在内容脚本中,您将具有以下代码:

And in the content script you would have the following code:

self.on("message", function(data)
{
  alert("Received option value: " + data);
});

此消息异步到达,这意味着您的内容脚本最初将不知道选项值-但这就是内容脚本的工作方式.

This message arrives asynchronously meaning that your content script won't know the option value initially - but that's how content scripts work.

这篇关于使用simple-prefs模块并将值导出到存储在数据文件夹中的脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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