Google chrome extension 从Chrome扩展请求资源时,如何获取重定向的目标URL?

Google chrome extension 从Chrome扩展请求资源时,如何获取重定向的目标URL?,google-chrome-extension,xmlhttprequest,greasemonkey,Google Chrome Extension,Xmlhttprequest,Greasemonkey,我正在开发一个跨浏览器用户脚本/扩展。我尝试从该脚本获取我查询的URL的重定向目标 现在,使用Firefox和GreaseMonkey,这很容易,因为GM_xmlhttpRequest的响应对象有一个属性finalUrl 在Google Chrome中,GM_xmlhttpRequest是跨域xmlhttpRequest的包装器,响应对象根本不知道“真实”URL 那么,有没有其他方法可以从用户脚本/扩展获取重定向目标?如果您知道重定向URL,可以使用为扩展提供的google.*API对其进行解析

我正在开发一个跨浏览器用户脚本/扩展。我尝试从该脚本获取我查询的URL的重定向目标

现在,使用Firefox和GreaseMonkey,这很容易,因为GM_xmlhttpRequest的响应对象有一个属性
finalUrl

在Google Chrome中,GM_xmlhttpRequest是跨域xmlhttpRequest的包装器,响应对象根本不知道“真实”URL


那么,有没有其他方法可以从用户脚本/扩展获取重定向目标?

如果您知道重定向URL,可以使用为扩展提供的
google.*
API对其进行解析

(让我们假设的示例,其中成功URL为
https://www.facebook.com/connect/login_success.html
)。您需要将
选项卡和URL添加到您的权限中,例如:

"permissions": [
    "tabs",
    "https://facebook.com/connect/*"
]
当用户单击login/auth按钮时,需要执行两个步骤:

第一步 将侦听器添加到选项卡更新,以搜索所有选项卡中的成功URL:

chrome.tabs.onUpdated.addListener(function() {
    var lis = this; 
    chrome.tabs.getAllInWindow(null, function(tabs) {
                        for (var i = 0; i < tabs.length; i++) {
                            if (tabs[i].url.indexOf("https://www.facebook.com/connect/login_success.html") == 0) {
                                var token = tabs[i].url.match(/[\\?&#]auth_token=([^&#])*/i)
                                chrome.tabs.onUpdated.removeListener(lis);
                                return;
                            }
                        }
                    });
});

检查标题并获取
位置


这就是我在不看代码的情况下所能想到的。

这只有一个问题:XHR遵循所有重定向(根据规范),而您永远看不到“位置”标题。
chrome.tabs.create(
    {
    'url': "https://www.facebook.com/dialog/oauth?client_id=client_id>&redirect_uri=https://www.facebook.com/connect/login_success.html"
    }, null);
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);