在Firefox插件中获取浏览器区域设置代码

Get browser locale code in Firefox add-on

本文关键字:区域 设置 代码 浏览器 获取 Firefox 插件      更新时间:2023-09-26

我正在开发一个Firefox插件。我需要确定在用户的浏览器中设置了哪种语言
有没有像window.navigator这样的对象,它包含浏览器的当前区域设置?

访问区域设置的方法

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

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

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

获取Firefox首选项:
然而,一个似乎是偏好general.useragent.locale,或者可能是intl.accept_languages。你正在编写的插件类型将决定你如何访问它,甚至它目前是否可用:

  • 插件SDK扩展可以使用preferences/service访问它们
  • 引导程序或旧版加载项可以使用Services.prefs,这使您可以访问nsIPrefBranch、nsIPrefBranch2和nsIPrefService
  • 此时,WebExtensions似乎无法访问首选项。考虑到Firefox的整体偏好,它们很可能在未来可用

在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具有国际化API,可从后台脚本中获得。本地可用i18n.getUILanguage()。方法CCD_ 20也可能是感兴趣的。

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);

@Mayken的回答非常好。不过我只想补充一点,你也可以在没有window的情况下获得navigator,你可以使用WebWorkerChromeWorker,他们有很多有用的东西:

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

我不建议仅仅为了获取导航信息而启动worker,使用Services.appshell.hiddenDOMWindow可能会更容易,但如果你已经在使用worker,那么是的,这是一个不错的解决方案。