Arpan Das Arpan Das - 6 months ago 19
Javascript Question

How to Capture Chrome Tab URL when the Tab was moved to get a new window

I wrote a chrome extension to capture the URL of the current tab in the Chrome window. This works fine when I have 1 Chrome window, and multiple tabs inside it.

However, lets say I drag one tab out of the Chrome Window, and take it to my extended screen - then it becomes fuzzy.

Either there is no URL that is returned when I used the extension or it returns the URL of the last active tab on the previous window.

This is the script Im using :

chrome.tabs.query({'active': true}, function(tabs) {
var url = tabs[0].url;
document.getElementById('myframe').setAttribute('src', loc);
});

Answer

The callback of chrome.tabs.query is passing an array of all active tabs across all windows. Since you're always accessing the first element of that array, you're always getting the active tab of the same window.

If you want the active tab of the current window you should use lastFocusedWindow:

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function(tabs) {
    ...
});
Comments