Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the window object for a specific tab if I have that tab's tabId?

I have the tabId of a tab. How do I get it's window object?

like image 987
trusktr Avatar asked May 30 '12 18:05

trusktr


People also ask

How do I bring a window to a tab?

Bring back a tab or window Windows & Linux: Ctrl + Shift + t. Mac: ⌘ + Shift + t.


2 Answers

The window object as seen inside chrome extensions:

 chrome.tabs.get(YOUR_TAB_ID_HERE, function(tab){
      chrome.windows.get(tab.windowId, function(win){ 
           console.log(win); // THIS IS THE WINDOW OBJECT
      });
 });

But if you need the javascript runtime inside a specific tab, you'll need to use Content Scripts which are better explained here:

http://code.google.com/chrome/extensions/content_scripts.html

like image 92
Silviu-Marian Avatar answered Oct 19 '22 20:10

Silviu-Marian


To get the DOM window object from a tabId, you should insert a content script in that tab:

chrome.tabs.executeScript(tabId, {code:'var w = window; console.log(w);'});

https://developer.chrome.com/extensions/tabs#method-executeScript

Perhaps you'll need to comunicate with your background page:

https://developer.chrome.com/extensions/content_scripts#host-page-communication

like image 10
Alejandro Silvestri Avatar answered Oct 19 '22 20:10

Alejandro Silvestri