我试图让用户在Firefox WebExtensions插件中运行哪个操作系统。
Mozilla API Documentation上有一个简单的例子。
根据这个文档,函数browser.runtime.getPlatformInfo()应该可以帮助我获得平台操作系统,但它不起作用,似乎未定义。
我在Windows 10 64-Bit上安装的Firefox 57.0.2 64位上测试它。
为什么browser.runtime.getPlatformInfo未定义?
这是我的代码:
的manifest.json
{
"manifest_version": 2,
"name": "Test Addon",
"version": "2.0",
"description": "Test Addon",
"permissions": [
"activeTab",
"contextMenus",
"clipboardWrite"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content-script.js"]
}]
}
内容-的script.js
console.log("browser.runtime:");
console.log(browser.runtime);
console.log("browser.runtime.getPlatformInfo:");
console.log(browser.runtime.getPlatformInfo);
function gotPlatformInfo(info) {
console.log(info.os);
}
// The following line breaks code execution
var gettingInfo = browser.runtime.getPlatformInfo();
gettingInfo.then(gotPlatformInfo);