in the documentation, it says, getBackgroundPage()
Returns the JavaScript 'window' object for the background page running inside the current extension.
which window is it? is it the same window that we get with: "chrome.windows.getCurrent()".
If you specify a background page in your manifest.json
, the page is actually 'open' when your extension is running (go to chrome://extensions
and look for extensions with a property Inspect views: some_background_page.html
). Although you can't actually see the page, it does exist, and as the documentation states, running getBackgroundPage()
will return that page's 'window' object, allowing you to perform all of the actions you could on a normal window (see here for a list of some of those properties/actions).
To answer your second question, you can read here that the value of current window falls back to the last active window when being called from a background page. Here is a sample background.js
that may help explain - it will show identical IDs for getCurrent
and getAllinWindow
, both of which will refer to the page from the active/visible page from which you called the extension - not the background page itself:
// This gets all current window tabs
chrome.tabs.getAllInWindow(null, function(tabs){
// This alerts the window ID when getCurrent is called from background.js
chrome.windows.getCurrent(function(window){
alert("Background ID: " + window.id);
});
// Now we loop through the tabs returned by getAllInWindow,
// showing the window ID. The ID is identical to the one above,
// meaning that getCurrent (when called from a background page)
// returns the current active window - NOT the background page
chrome.windows.get(tabs[0].windowId, function(window) {
alert("Active Window: " + window.id);
});
});
Hope that helps!