WXT Extension升级至v0.19.3后失效,求修复或降级方案
问题描述
我使用WXT开发了一款Chrome扩展,功能是操作DOM在LinkedIn评论框中插入按钮。该扩展在WXT v0.18.5版本下运行正常,但升级至v0.19.3后完全失效,且控制台无任何错误提示。
原v0.18.5配置
import path from 'path'; import { defineConfig } from 'wxt'; // See https://wxt.dev/api/config.html export default defineConfig({ modules: ['@wxt-dev/module-react'], runner: { keepProfileChanges: true, chromiumProfile: path.resolve('.chrome-profile'), openConsole: true, openDevtools: true, // startUrls: ['https://www.linkedin.com/feed/'], }, manifest: { name: 'Draftly for LinkedIn', description: 'Make your LinkedIn Comments stand out', page_action: { default_title: 'Draftly for LinkedIn', }, }, alias: { '@shared-frontend': path.resolve( __dirname, '../../libs/shared-frontend/src' ), '@shared': path.resolve(__dirname, '../../libs/shared/src'), '@shadcn/components': path.resolve( __dirname, '../../libs/shadcn/src/components' ), '@shadcn/utils': path.resolve( __dirname, '../../libs/shadcn/src/lib/utils.ts' ), }, });
已尝试的解决方法
- 将manifest中的
page_action改为action - 显式添加
manifest_version: 3并更新模块版本 - 尝试降级回v0.18.15,但问题仍存在
- 清除浏览器缓存和扩展数据
额外报错
在部分配置下,出现以下错误:
Uncaught (in promise) TypeError: Error in invocation of runtime.setUninstallURL(string url, optional function callback): No matching signature.
解决方案
适配WXT v0.19.3的修复方案
1. 修正Manifest V3配置
WXT v0.19+对Manifest V3的校验更严格,按以下调整配置:
- 明确指定
manifest_version: 3 - 彻底移除
page_action,统一用action(Manifest V3已废弃前者) - 新增
host_permissions确保扩展能访问LinkedIn页面,同时显式配置内容脚本注入规则:manifest: { manifest_version: 3, name: 'Draftly for LinkedIn', description: 'Make your LinkedIn Comments stand out', action: { default_title: 'Draftly for LinkedIn', }, host_permissions: ["https://www.linkedin.com/*"], content_scripts: [ { matches: ["https://www.linkedin.com/*"], js: ["src/content-scripts/main.tsx"], // 替换成你的内容脚本入口路径 run_at: "document_idle" // 可根据需求调整为document_start/end } ] }
2. 确认内容脚本是否正常注入
v0.19调整了内容脚本的注入逻辑,做以下检查:
- 在内容脚本开头加
console.log('Content script loaded'),打开LinkedIn页面确认控制台是否输出该日志,判断脚本是否被注入 - 检查内容脚本的入口文件是否正确导出,WXT v0.19对入口的规范要求更严格
3. 修复runtime.setUninstallURL报错
这个错误是因为Chrome API不支持Promise风格调用,按以下处理:
- 不要用
await chrome.runtime.setUninstallURL(url),改用回调写法:chrome.runtime.setUninstallURL("https://your-uninstall-url.com", () => { // 可选回调逻辑 }); - 或者直接在manifest中配置卸载URL,避免API调用:
manifest: { // ...其他配置 uninstall_url: "https://your-uninstall-url.com" }
4. 检查别名与依赖兼容性
- 简化别名路径写法,避免路径解析错误:
alias: { "@shared-frontend": "../../libs/shared-frontend/src", "@shared": "../../libs/shared/src", "@shadcn/components": "../../libs/shadcn/src/components", "@shadcn/utils": "../../libs/shadcn/src/lib/utils.ts" } - 升级
@wxt-dev/module-react到与WXT v0.19匹配的版本:npm install @wxt-dev/module-react@latest
正确降级至WXT v0.18.5的方法
如果暂时不想升级,按以下步骤彻底降级:
- 卸载当前WXT及相关模块:
npm uninstall wxt @wxt-dev/module-react - 安装指定版本:
npm install wxt@0.18.5 @wxt-dev/module-react@0.18.5 - 清理依赖缓存:
rm -rf node_modules package-lock.json yarn.lock npm install - 清除WXT构建缓存:
npx wxt clean - 重新构建并加载扩展:
加载时先移除浏览器中旧版本扩展,再重新加载打包后的文件。npx wxt dev
内容的提问来源于stack exchange,提问作者k1ngalph0x
相关产品推荐
相关产品推荐

