How to get a window object for a specific tab if I have that tabId tab?

I have tabId tabs. How to get its window object?

+6
source share
2 answers

To get the DOM window object from tabId, you must insert the contents of the script on this tab:

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

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

You may need to familiarize yourself with the background page:

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

+9
source

Window object visible 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 a javascript runtime inside a specific tab, you need to use Content Scripts, which are better explained here:

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

+10
source

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


All Articles