chrome扩展错误:Uncaught ReferenceError:窗口未定义



我正在开发一个Chrome扩展(没有很多经验),我有以下代码:

window.onload = function () {
validateCNPJ();
};
function validateCNPJ() {
document.querySelector('div[data-field-name="CNPJ"] > input').addEventListener('keydown', (bla) => {
let ble = bla.target.value;
console.log(ble.length)
if (ble.length > 17) {
bla.preventDefault()
bla.stopPropagation()
return
}
ble = ble.replace(/D/g, "")
ble = ble.replace(/^(d{2})(d)/, "$1.$2")
ble = ble.replace(/^(d{2}).(d{3})(d)/, "$1.$2.$3")
ble = ble.replace(/.(d{3})(d)/, ".$1/$2")
ble = ble.replace(/(d{4})(d)/, "$1-$2")
bla.target.value = ble;
})
}

我添加了窗口。这样当页面加载时,validateCNPJ函数就会被激活。该函数实时验证表单中的特定字段。

问题是,当我上传扩展它显示一个错误消息说:

Uncaught ReferenceError: window is not defined

我在浏览器的控制台中测试了函数validateCPNJ中的代码,它正常工作。

清单文件是这样的:

{
"name": "CRM Validation Extension",
"description": "Extension that validate fields of a opportunity",
"version": "0.1",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
}
}

我能做些什么来解决这个问题?

谢谢!

我设法解决了这个问题。以前是我的舱单。Json是这样的:

{
"name": "CRM Validation Extension",
"description": "Extension that validate fields of a opportunity",
"version": "0.1",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
}
}

我更改了背景对象,并替换为以下内容:

"content_scripts":[
{
"matches": [
"<all_urls>"
],
"js": ["background.js"]
}
]

这修复了我遇到的问题。

相关内容

  • 没有找到相关文章

最新更新