我需要在我的chrome扩展中获得pacScript的响应。pacScript将返回DIRECT
字符串,以防我们不需要代理,而我想要检测到这一点。
var config = {
mode: "pac_script",
pacScript: {
url: "https://www.example.com/proxy.pac"
}
};
chrome.proxy.settings.set({value: config, scope: 'regular'},function() {
//how can i get the pac response string here
});
编辑:我试着使用JQuery.getScript
从远程的pac文件加载FindProxyForURL
,但是现在像isPlainHostName
这样的pac特定函数是未定义的。
我可以从mozilla获得实现,但肯定有更好的方法,因为这些都是浏览器功能,应该已经可以使用了。
发布于 2019-10-06 14:47:13
你想要做的事情是不可能的。这是因为您请求的每个url都会对pac文件进行评估。因此,'pac response string‘不是一个可以在设置代理设置时返回的常量。
如果您正在尝试调试pac文件,则可以在返回设置之前在FindProxyForURL
中执行alert('settings')
。此警报会创建一个可通过chrome://net-internals/#events
功能访问的日志条目。
如果您只想测试用户使用的是直接连接还是代理,您可以比较设置代理设置前后的外部ip地址。签出ipify.org,或者你甚至可以在你自己的web服务器上使用脚本。
下面是一些示例代码:
let originalIp = "";
async function getCurrentIp() {
var res = await fetch('http://api.ipify.org/');
return await res.text();
}
// Get ip before setting proxy
getCurrentIp().then(ip => {
originalIp = ip;
});
var config = {
mode: "pac_script",
pacScript: {
url: "https://www.example.com/proxy.pac"
}
};
chrome.proxy.settings.set({
value: config,
scope: 'regular'
}, function() {
// Get ip after setting proxy and compare it with original ip
getCurrentIp().then(ip => {
if (ip == originalIp)
console.log('DIRECT');
else
console.log('PROXY: ' + ip)
})
});
https://stackoverflow.com/questions/58156492
复制相似问题