在Firefox附加组件中获取浏览器区域设置代码

时间:2016-08-06 05:54:04

标签: firefox firefox-addon firefox-addon-sdk firefox-webextensions firefox-addon-restartless

我正在开发一个Firefox附加组件。我需要确定在用户的浏览器中设置了哪种语言 是否有某种对象,如window.navigator,其中包含浏览器的当前区域设置?

2 个答案:

答案 0 :(得分:3)

访问区域设置的方法

使用window.navigator
虽然可能还有其他内容,但您可以使用window.navigator

navigator object只能在与内容关联的window对象上提供(即不是浏览器的XUL window)。但是,即使您需要从内容脚本访问它,该对象也可用于所有扩展类型。

  • WebExtensions:您需要从内容脚本中访问它。
  • 所有其他类型:即使在多进程Firefox中,您也可以从主/后台脚本访问window.navigator。与扩展中一样,您必须使用正确的<window>对象。并非所有<window>个对象都具有navigator属性。

获取Firefox偏好设置:
但是,一个似乎是偏好general.useragent.locale,或者intl.accept_languages。您正在编写哪种类型的加载项将决定如何访问它,或者即使它当前可用:

在ChromeWindow上使用getLocale()
正如stkvtflw所提到的,您还可以在主ChromeWindow上使用getLocale()PanelUI(不要与sdk/panel混淆)包含一个函数getLocale()。但是,似乎没有任何关于此功能的官方文档。它的代码是:

function getLocale() {
  try {
    let chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"]
                           .getService(Ci.nsIXULChromeRegistry);
    return chromeRegistry.getSelectedLocale("browser");
  } catch (ex) {
    return "en-US";
  }
}

显然,这表明您可以根据需要直接使用nsIXULChromeRegistry服务。此服务似乎也没有官方文档。

WebExtensions特定方法:
WebExtensions具有Internationalization API,可从后台脚本获得。使用i18n.getUILanguage()可以使用本地。方法i18n.getAcceptLanguages()也可能是有意义的。

WebExtensions附加组件的完整代码

main.js

let locale = chrome.i18n.getUILanguage();
console.log('The locale is: ' + locale);

的manifest.json

{
    "description": "Log the locale to the console",
    "manifest_version": 2,
    "name": "log_locale",
    "version": "0.1",
    "applications": {
        "gecko": {
            "id": "extention@stick.man",
            "strict_min_version": "45.0"
        }
    },
    "background": {
        "scripts": ["main.js"]
    }
}

附加SDK扩展的完整代码

index.js

//Get the window.navigator from current window/tab

//Get the currently active Browser Window
let activeWindow = require('sdk/window/utils').getMostRecentBrowserWindow();
//activeWindow.document.defaultView is the <window> you want to use
let defaultViewNavigator = activeWindow.document.defaultView.navigator;
//log the language from window.navigator
console.log('navigator language='+defaultViewNavigator.language);


//Get the locale from preferences:
let prefs = require("sdk/preferences/service");
let langFromPref = prefs.get(`general.useragent.locale`);
console.log('language from prefs='+langFromPref);


//Get the locale using getLocale():
let localeGet = require("sdk/window/utils").getMostRecentBrowserWindow().getLocale()
console.log('getLocale language='+localeGet);

的package.json

{
  "title": "Find locale",
  "name": "find_local",
  "version": "0.0.1",
  "description": "Find the locale",
  "main": "index.js",
  "author": "Makyen",
  "engines": {
    "firefox": ">=38.0a1",
    "fennec": ">=38.0a1"
  },
  "license": "MIT",
  "keywords": [
    "jetpack"
  ]
}

引导程序/传统附加程序代码

//Import Services
Components.utils.import("resource://gre/modules/Services.jsm");
//Get the currently active Browser Window
let activeWindow=Services.wm.getMostRecentWindow("navigator:browser");

//Get the window.navigator from current window/tab   
//activeWindow.document.defaultView is the <window> you want to use
let defaultViewNavigator = activeWindow.document.defaultView.navigator;
console.log('navigator language='+defaultViewNavigator.language);

//Get the locale from preferences:
let langFromPref = Services.prefs.getCharPref(`general.useragent.locale`);
console.log('language from prefs='+langFromPref);

//Get the locale using getLocale():
let localeGet = activeWindow.getLocale()
console.log('getLocale language='+localeGet);

答案 1 :(得分:2)

@ Mayken的回答非常好。我只想添加,你也可以navigator获得windowWebWorkerChromeWorker他们可以使用一些有用的东西:

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers#workerscope

我不建议只是为了获取导航信息而启动工作人员,使用Services.appshell.hiddenDOMWindow可能会更容易,但如果你已经使用了工作人员,那么它是一个很好的解决方案。