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

*爱你&永不变心* 提交于 2019-12-21 02:34:40

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/10822133/how-do-i-get-the-window-object-for-a-specific-tab-if-i-have-that-tabs-tabid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!