1
votes

I've a problem getting the tab ID of a tab and using it in other tab.

There is one tab open (google.com) i've placed a button on google.com using my content script. The Button should create a tab when clicked with url as "cricinfo.com" . contentscript.js

$(body).prepend('(Open up)

</button><textarea id="followup_text"></textarea>');


chrome.extension.sendRequest({"acturl":'http://cricinfo.com',"type":""});   

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
    if(request.greeting=="hello")
{
 alert(sender.tab.url); 
sendresponse({farwell:"thanks"});
} 
else
sendresponse({farwell:"not recieved"});
});

}); 

Background.html

<script type="text/javascript" charset="utf-8">


       chrome.extension.onRequest.addListener(
        function(request, sender, sendResponse) {  

       chrome.tabs.create({"url":request.acturl,"selected":false},function(tab){

       });  

    });   
    chrome.tabs.getSelected(null, function(tab){ 
        chrome.extension.sendRequest(tab.id, {greeting:"hello"},function(response){console.log(response.farwell);}); 
    }
    })

</script>   

Now cricinfo.com redirects to "espncricinfo.com" , So i want this url to be displayed in my original tab(i.e. in google.com) and get it displayed in the textarea#follow_text.

To perform this i want the tabID of google.com for sending request from background.html when on espncricinfo.com. The extensions doesn't allow to use tabs in contentscripts. I'm not able to use it on background.html.

Thanks. Lemme know if i'm not clear.

1
So the problem with redirect? Do you want to display cricinfo.com or espncricinfo.com in textarea? Are you building this extension for this specific sites or it should work in general case? - serg
i want to display the tab url after it gets redirected(or updated) i.e. i want to display espncricinfo.com in the textarea. And as you asked i want it to work only on the parent tab (google.com) and as we require some information from the cricinfo's tab (considering we dont know the url after it gets redirected) it should work on this aswell. am i clear - RaviTeja
How about this - you create a new tab and remember it, then monitor all url chnages that are happening in that tab (and displaying those urls in a textarea). Would that work? Just redirect is a tough case, it would be hard to separate http redirect from a user redirect (clicking link). - serg
I don't know a way of detecting http redirects, chrome extension doesn't have full access to http headers. There is experimental API which looks like should be able to detect redirects: code.google.com/chrome/extensions/dev/… - serg
Hi Serg , thanks for your quick responses, the behaviour of this is when i click th butoon from google.com it should create a tab with a url. the url i know is cricinfo.com , so it takes as a parameter and creates a tab , and it redirects to espncricinfo.com directly until this its fine . Now can we keep and event listener when the tab got updated with the redirected url and send that information to google.com tab and display it there. - RaviTeja

1 Answers

3
votes

Well, here is some code, but it is kind of a shaky solution. The trick is that when you listen to chrome.tabs.onUpdated it receives redirected url already (and if there was no redirect it receives direct url).

var createdTabId = 0;
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(tabId == createdTabId && changeInfo.status == "loading") {
        createdTabId = 0;

        //tab.url contains redirected or direct url, send it to google tab
        var tabUrl = tab.url;
        chrome.tabs.getSelected(null, function(tab){ 
            chrome.tabs.sendRequest(tab.id, {tabUrl: tabUrl}); 
        });

    }
});

chrome.tabs.create({"url":"http://cricinfo.com","selected":false},function(tab){
    createdTabId = tab.id;
});