谷歌浏览器插件MV3报错"Uncaught ReferenceError: window is not defined"
出错
配置mv3后,在后台代码background.js使用DOMPurify发现无法访问window,会一直报错
Uncaught ReferenceError: window is not defined
查看后台,globalThis变成了一个叫ServiceWorkerGlobalScope的玩意
原因
mv3使用了一个叫Service workers的东西替代原来的background页面,不提供dom API,所以不管是window还是document、HTMLElement……都会xx is not defined
。
chrome官方介绍:
Manifest V3 replaces background pages with service workers.
Like their web page counterparts, extension service workers listen for and respond to events in order to enhance the end user's experience. For web service workers this typically means managing cache, preloading resources, and enabling offline web pages. While extension service workers can still do all of this, the extension package already contains a bundle of resources that can be accessed offline. As such, extension service workers tend to focus on reacting to relevant browser events exposed by Chrome's extensions APIs.
另附mv2和mv3后台页区别对比表:
MV2 - Background page MV3 - Service worker Can use a persistent page. Terminates when not in use. Has access to the DOM. Doesn't have access to the DOM. Can use XMLHttpRequest(). Must use fetch() to make requests.
解决
1. 放弃在后台页直接调用dom API
chrome官方态度还是比较明确的,所以最好不要花太大力气去搞。能用chrome API的全都用chrome API实现,特别是标签和窗口的操作。另外chrome也提供了一些mv2到mv3迁移的建议migrating_to_service_workers
2. 使用undom模拟
undom是比jsdom轻量的Document接口实现,基本满足dom操作的需求,简单易用。
安装
pnpm install --save undom
使用
import undom from 'undom';
let document = undom();
// 简单操作dom
let foo = document.createElement('foo');
foo.appendChild(document.createTextNode('Hello, World!'));
document.body.appendChild(foo);
// 驱动第三方库DOMPurify
const purify = DOMPurify(document);
purify.sanitize(html..)
另外undom本身不支持直接输出HTML,因为它只实现了Document的骨架,不能直接用innerHTML、querySelector等,提供了手动解析的方法
function serialize(el) {
if (el.nodeType===3) return el.textContent;
var name = String(el.nodeName).toLowerCase(),
str = '<'+name,
c, i;
for (i=0; i<el.attributes.length; i++) {
str += ' '+el.attributes[i].name+'="'+el.attributes[i].value+'"';
}
str += '>';
for (i=0; i<el.childNodes.length; i++) {
c = serialize(el.childNodes[i]);
if (c) str += '\n\t'+c.replace(/\n/g,'\n\t');
}
return str + (c?'\n':'') + '</'+name+'>';
}
function enc(s) {
return s.replace(/[&'"<>]/g, function(a){ return `&#${a};` });
}
// 输出完整html
console.log(serialize(document.childNodes[0]));
// 转义html
console.log(enc(serialize(document.childNodes[0])));
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了