0

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()".

burhan
  • 121
  • 1
  • 9

1 Answers1

1

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!

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • rocketdonkey could you expand your explanation to your second paragraph? – burhan Oct 29 '12 at 17:46
  • @burhan Sure, dropped in an example. Let me know if that helps. – RocketDonkey Oct 29 '12 at 18:32
  • thanks rocketdonkey. say, we have popup.js like this `backgd = chrome.extension.getBackgroundPage(); ...... backgd.Clipboard.write(Text);` what does this script do then? – burhan Oct 29 '12 at 18:47
  • @burhan Hmm, it looks like it is intending to write to the clipboard, but I haven't ever seen a `Clipboard` property like that. The only way I know of (which doesn't say much :) ) to copy data to the clipboard is to use `document.execCommand('copy')` (see http://developer.chrome.com/extensions/manifest.html#permissions, by the `clipboardWrite` permission). Is that what you're trying to do? – RocketDonkey Oct 29 '12 at 19:04
  • i came across the above `popup.js` in an extension. as far as i see, too, it saves `Text` to the clipboard. i wonder which window returned with `getBackGroundPage` method and why it uses that window's clipboard `property` to write to. – burhan Oct 29 '12 at 19:14
  • @burhan It could be that there is some property specific to the background page that needs to be copied to the clipboard. However I can't say for sure :) Does the above explanation make sense in terms of the difference? – RocketDonkey Oct 29 '12 at 19:16