How to get the redirect destination URL when requesting resources from a Chrome extension?

I am working on a cross-browser user script / extension. I am trying to get the goal of redirecting a url request from this script.

Now using Firefox and GreaseMonkey is easy because the GM_xmlhttpRequest response object has the finalUrl property.

In Google Chrome, GM_xmlhttpRequest is a wrapper for XMLHttpRequest that supports cross-domain communication, and the response object does not know the "real" URL at all.

So, is there another way to get the redirect target from the user - script / extension?

+4
source share
2 answers

If you know the redirect URL, you can parse it using the google.* API provided for extensions.

(Assume Facebook Desktop Flow , where the success URL is https://www.facebook.com/connect/login_success.html ). You need to add tabs and URLs to your permissions - for example:

 "permissions": [ "tabs", "https://facebook.com/connect/*" ] 

When the user presses the login / authorization button, you need to perform two steps:

Step 1

Add a listener to the Updates tab that view all tabs for success URLs:

 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; } } }); }); 

Step 2

Redirect user to OAUTH page:

 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); 
0
source

Check the headers and get location ?

As in

 var req = new XMLHttpRequest(); req.open('GET', document.location, false); req.send(null); var headers = req.getAllResponseHeaders().toLowerCase(); alert(headers); 

This is all I can think of without seeing your code.

0
source

Source: https://habr.com/ru/post/1379907/


All Articles