Chrome扩展报错Uncaught TypeError:无法读取onAuthRequired属性求助
问题根源
你把webrequest.js同时配置成了内容脚本和后台脚本,这就是错误的核心原因:
- 内容脚本是注入到网页上下文运行的,网页环境没有权限访问
chrome.webRequest这类扩展专属API,所以执行到chrome.webRequest.onAuthRequired时会抛出Cannot read properties of undefined错误 - 后台脚本运行在扩展独立的后台环境中,拥有完整的扩展API调用权限,因此后台控制台能正常输出日志
解决方法
直接修改manifest.json,移除content_scripts字段下对webrequest.js的引用——你的自动登录逻辑只需要在后台监听网络请求,完全不需要注入到网页中。
修改后的manifest.json如下:
{ "manifest_version": 2, "name": "Authentication", "description": "This chrome extension is used to auto load the automation users while running the test on Jenkins", "version": "2.1.0", "author": "", "permissions": [ "storage", "<all_urls>", "webRequest", "webRequestBlocking", "management" ], "background": { "scripts": ["webrequest.js"] }, "icons": { "128": "/images/icon128.png", "48": "/images/icon48.png", "32": "/images/icon32.png", "16": "/images/icon16.png" } }
验证方式
修改完成后重新加载扩展:
- 主浏览器控制台不会再出现
Uncaught TypeError错误 - 通过「Inspect view: background page for the extension」查看后台控制台,依然能正常看到请求拦截、凭证传递的日志输出
内容的提问来源于stack exchange,提问作者himanshu gusain
相关产品推荐
相关产品推荐

