You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Manifest V2扩展的background.js中访问manifest.json的值?

在Manifest V2扩展的background.js中访问manifest.json键值对

可行性说明

完全可行。Manifest V2的WebExtension API提供了原生方法,允许你在background脚本中获取完整的manifest.json内容,Thunderbird作为支持WebExtensions标准的应用,同样兼容这个API。

具体实现方法

你可以通过browser.runtime.getManifest()(Thunderbird推荐使用,与Firefox生态保持一致)或chrome.runtime.getManifest()(兼容Chrome类浏览器)获取manifest的完整对象,之后即可自由访问任意键值对。

代码示例

假设你的manifest.json包含以下内容:

{
  "manifest_version": 2,
  "name": "我的Thunderbird扩展",
  "version": "1.2.3",
  "background": {
    "scripts": ["background.js"]
  },
  "customSettings": {
    "syncInterval": 300,
    "defaultTheme": "dark"
  }
}

在background.js中读取这些字段:

// 获取manifest对象
const extensionManifest = browser.runtime.getManifest();

// 读取内置字段
console.log(`扩展名称:${extensionManifest.name}`);
console.log(`当前版本:${extensionManifest.version}`);

// 读取自定义字段
console.log(`同步间隔:${extensionManifest.customSettings.syncInterval}秒`);
console.log(`默认主题:${extensionManifest.customSettings.defaultTheme}`);

注意点

  • Thunderbird优先支持browser命名空间,虽然部分场景下chrome也能工作,但建议使用browser确保兼容性。
  • 获取到的manifest对象是只读的,无法在脚本中修改其值。

内容的提问来源于stack exchange,提问作者einpoklum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 07:15:56