Daizy
Daizy

Reputation: 331

How can I get the changed url of a known tab id queried before in Chrome Extension?

The situation is:

I queried the tab and know the active tab's id with:

chrome.tabs.query({active: true, currentWindow: true}, function(arrayOfTabs) {

    activeTab = arrayOfTabs[0];
    tabId = activeTab.id;
    ...
}

and injected some content script with:

    chrome.tabs.executeScript(tabId, {file: "content.js"});

which may change the url to a new location.

Something wired is that the return value of activeTab.url remains unchanged.

So is there any way to get the new url of a know tab id. Or I have to query the tabs again and find which tab matches the known id (the current active tab may not be the previous one), the get the new url?

Thanks a lot!

Upvotes: 0

Views: 71

Answers (1)

woxxom
woxxom

Reputation: 73606

  1. The tabs array parameter is just a temporary copy created for your callback's sake, so activeTab.url is a copy of the tab's URL, it's not a self-refreshing pointer to the current tab.
  2. Use chrome.tabs.get:

    chrome.tabs.get(tabId, function(tab) {
        if (chrome.runtime.lastError) {
            console.log('Tab %d was not found', tabId);
            return;
        }
        console.log('Async callback got tab %d URL: %s', tabId, tab.url);
    });
    

    You'll need "tabs" in "permissions".
    Or "activeTab" if this is an active tab and your extension was invoked via user gesture.

Upvotes: 2

Related Questions