Firefox 94.0自定义扩展提示损坏、临时加载找不到配置目录求助
问题排查与修复方案
核心根因
你将需要运行在扩展特权上下文的后台脚本,错误注册为了网页侧的内容脚本,是所有报错的核心原因。
各错误具体原因
- 打包后提示扩展损坏:两个原因,一是自己本地打包的扩展未经过Mozilla官方签名,Firefox默认禁止安装未签名的非临时扩展;二是如果打包时把所有文件放在外层目录再压缩,Firefox找不到根目录下的manifest.json也会触发该提示。
- 临时加载时报错
Error: Can't find profile directory.:内容脚本运行在网页沙箱环境,没有权限访问browser.tabs这类扩展特权API,越权调用就会触发该类异常。
修复步骤
1. 修正manifest.json配置
删除错误的content_scripts字段,新增后台脚本声明,同时注意原*://example/aaa/*不符合域名规范,实际使用时需要补充完整域名后缀:
{ "manifest_version": 2, "name": "Popup", "version": "1.0", "background": { "scripts": ["background.js"], "persistent": false }, "permissions": ["tabs", "http://*/*", "https://*/*"], "icons": { "16": "icons/l16.png", "32": "icons/l32.png", "48": "icons/l48.png", "128": "icons/l128.png" } }
2. 修正background.js逻辑
扩展后台上下文没有网页侧的sessionStorage,改用Set存储已访问地址避免重复跳转,同时简化冗余的标签查询逻辑,补充URL编码避免跳转参数异常:
// background.js const visitedUrls = new Set(); const apps = [ ['AAA', 'https://example.com/aaa/'], ['BBB', 'http://examp.aaa.bb.cc/'] ] browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { // 仅页面加载完成后触发,避免重复执行 if (changeInfo.status !== 'complete') return; const currentUrl = tab.url; const matchIndex = apps.findIndex(item => currentUrl.includes(item[1])); if (matchIndex > -1 && !visitedUrls.has(currentUrl)) { visitedUrls.add(currentUrl); browser.tabs.update(tabId, { url: `http://popup.aaa.bb.cc?title=${encodeURIComponent(apps[matchIndex][0])}` }); } });
3. 开发与安装注意事项
- 开发阶段直接访问
about:debugging#/runtime/this-firefox,点击「临时加载附加组件」选中项目根目录下的manifest.json即可,无需打包。 - 如需正式安装使用,必须提交扩展到Mozilla扩展商店进行审核签名,签名后的安装包不会提示损坏。
内容的提问来源于stack exchange,提问作者sada
相关产品推荐
相关产品推荐

