问题
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