关于配置首选项和JS [英] about config preferences and js

查看:221
本文介绍了关于配置首选项和JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有可能得到某些偏好设置在about:配置使用javascript?



激励是为了获得设置在firefox插件中的偏好值当用户登陆插件前端时创建的。基本上,我试图确定在FE上登陆的用户,而不要求他们明确登录。

解决方案

是的,您可以。知道Mozilla使用XPCOM接口作为首选系统。



三个使用的接口是 nsIPrefService nsIPrefBranch 和<您可以使用与您实例化相同的方式实例化首选项服务。 nsIPrefBranch2

xpcomref / creatingcomps.htmlrel =noreferrer> XPCOM服务



两个例子清楚说明:

  //获取根分支
var prefs = Components.classes [@ mozilla.org/preferences-service;1]
。的getService(Components.interfaces.nsIPrefBranch);



  //获取extensions.myext。分支
var prefs = Components.classes [@ mozilla.org/preferences-service;1]
.getService(Components.interfaces.nsIPrefService);
prefs = prefs.getBranch(extensions.myext。);

有三种偏好设置,分别是 string 整数布尔。在 nsIPrefBranch 中,有六种读取和写入首选项的方法: getBoolPref() setBoolPref() getCharPref() setCharPref() getIntPref() setIntPref()

更多的例子:

  //获取可访问性。分支
var prefs = Components.classes [@ mozilla.org/preferences-service;1]
.getService(Components.interfaces.nsIPrefService).getBranch(accessibility。);

// prefs是一个nsIPrefBranch。
//查看上面的部分获取一个例子。
var value = prefs.getBoolPref(typeaheadfind); //获得一个pref(accessibility.typeaheadfind)
prefs.setBoolPref(typeaheadfind,!value); //设置一个pref(accessibility.typeaheadfind)

您也可以使用复杂的类型。通过使用nsISupportsString,用于在首选项中处理字符串,因此,当首选项值可能包含非ASCII字符时,请使用它。

例子:

  // prefs是一个nsIPrefBranch 

//示例1:获取Unicode值
var value = prefs.getComplexValue(preference.with.non.ascii.value,
Components.interfaces.nsISupportsString).data;

//示例2:设置Unicode值
var str = Components.classes [@ mozilla.org/supports-string;1]
.createInstance(Components.interfaces .nsISupportsString);
str.data =一些非ASCII文本;
prefs.setComplexValue(preference.with.non.ascii.value,
Components.interfaces.nsISupportsString,str);

我希望你能解决这个问题。

更多此页面


I wonder if it is possible to get values of certain preferences set in about:config using javascript?

Incentive is to get value of preferences set in firefox addon i've created when user lands on addon front end. Basically, I'm trying to identify users landing on FE without asking them to login explicitly.

解决方案

Yes, you can.

First, you need to know that Mozilla uses the XPCOM interfaces for the preferences system.

Three used interfaces are nsIPrefService, nsIPrefBranch and nsIPrefBranch2.

The preferences service is instantiated in the same way you instantiate any XPCOM service.

Two examples to make it clear:

// Get the root branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefBranch);

.

// Get the "extensions.myext." branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService);
prefs = prefs.getBranch("extensions.myext.");

And there are 3 types of preferences, they're string, integer and boolean. There are six methods in nsIPrefBranch that read and write preferences: getBoolPref(), setBoolPref(), getCharPref(), setCharPref(), getIntPref() and setIntPref().

More examples on that:

// Get the "accessibility." branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService).getBranch("accessibility.");

// prefs is an nsIPrefBranch.
// Look in the above section for examples of getting one.
var value = prefs.getBoolPref("typeaheadfind"); // get a pref (accessibility.typeaheadfind)
prefs.setBoolPref("typeaheadfind", !value); // set a pref (accessibility.typeaheadfind)

You can also use complex types. By using nsISupportsString, that is used to handle strings in preferences, so, use this when the preference value may contain non-ASCII characters.

Example:

// prefs is an nsIPrefBranch

// Example 1: getting Unicode value
var value = prefs.getComplexValue("preference.with.non.ascii.value",
      Components.interfaces.nsISupportsString).data;

// Example 2: setting Unicode value
var str = Components.classes["@mozilla.org/supports-string;1"]
      .createInstance(Components.interfaces.nsISupportsString);
str.data = "some non-ascii text";
prefs.setComplexValue("preference.with.non.ascii.value", 
      Components.interfaces.nsISupportsString, str);

I hope you solve your doubt with this.

More on this page.

这篇关于关于配置首选项和JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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