获取“需要代理身份验证”在 Chrome 扩展后台页面脚本上执行 XMLHttpRequest(跨域)时
问题
当我从 Google Chrome 扩展程序的后台页面执行跨源 XMLHttpRequest 时,收到错误 407“需要代理身份验证
”。
我将 Chrome 设置为使用 HTTP 代理,该代理需要用户名和密码(基本身份验证)。
如果我没有在 Chrome 中设置代理(如预期的那样),下面的代码就可以工作。
当我第一次打开 Chrome 时,它会要求我输入代理凭据,并且它会在会话的其余部分中记住该凭据。
我假设扩展程序位于其自己的进程中,因此没有存储代理的凭据。
我的问题是,有什么办法解决这个问题吗?有没有办法让 XMLHttpRequest 在扩展程序的后台页面脚本中工作,无论 Chrome 浏览器中的代理设置如何设置?
如果相关的话,我将在 Windows 上使用 chrome.exe --proxy-server=my.proxy.server:3128 启动 chrome。
示例代码
manifest.json
{
[...]
"background_page": "background.html",
"permissions": [
"http://example.org/",
"http://mysite.com/"
],
"content_scripts": [
{
"matches": [
"http://example.org/*"
],
"js": ["contentscript.js"]
}
]
}
background.html->background.js:
function getById(request, sender, sendResponse) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
sendResponse(JSON.parse(xhr.responseText));
}
else {
// xhr.status == 407 here
sendResponse(null);
}
}
}
var url = 'http://mysite.com/script.php?id=' + request.id;
xhr.open('GET', url, true);
xhr.send();
}
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if ( request.action == 'getById' ) {
getById(request, sender, sendResponse);
}
else {
sendResponse(null);
}
});
contentscript.js:
chrome.extension.sendRequest({
'action': 'getById',
'id': 42
}, function(response) {
alert(response)
});
Question
I am getting a Error 407 'Proxy authentication required
' when doing a cross-origin XMLHttpRequest from the background page of my Google Chrome extension.
I have Chrome set to use a HTTP Proxy which requires a username and password (Basic Auth).
The code below works if I do not have a proxy set in Chrome (as expected).
When I first open Chrome it asks for my proxy credentials, which I enter, and it remembers for the rest of the session.
I am assuming that the extension is in its own process and therefore does not have the credentials to the proxy stored.
My question is, is there any way around this? Is there a way to make the XMLHttpRequest work in the extension's background page script no matter what the proxy settings are set to in Chrome browser?
I am launching chrome with chrome.exe --proxy-server=my.proxy.server:3128
on Windows if that is relevant.
Example Code
manifest.json
{
[...]
"background_page": "background.html",
"permissions": [
"http://example.org/",
"http://mysite.com/"
],
"content_scripts": [
{
"matches": [
"http://example.org/*"
],
"js": ["contentscript.js"]
}
]
}
background.html->background.js:
function getById(request, sender, sendResponse) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
sendResponse(JSON.parse(xhr.responseText));
}
else {
// xhr.status == 407 here
sendResponse(null);
}
}
}
var url = 'http://mysite.com/script.php?id=' + request.id;
xhr.open('GET', url, true);
xhr.send();
}
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if ( request.action == 'getById' ) {
getById(request, sender, sendResponse);
}
else {
sendResponse(null);
}
});
contentscript.js:
chrome.extension.sendRequest({
'action': 'getById',
'id': 42
}, function(response) {
alert(response)
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论