10

I want to access the properties of a window object from a background script. I have this in manifest.json:

{
    "..": "..",
    "permissions": ["http://*.mysite.net/"],
    "background": {
        "scripts": ["extension.js"]
    }
}

and this in extension.js:

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    if (changeInfo.status === 'complete') {

        var tabWindowObject = ??

        setInterval(tabWindowObject.someFunction, 10);
    }
});

I need it here, not in another place (no content scripts and no script injection). How do I get the tabWindowObject in extension.js? In other words, I want to access the context of a tab inside a background script Chrome extension.

f.ardelian
  • 6,716
  • 8
  • 36
  • 53
  • 1
    You can't. If it's an object defined by the webpage code, then you can only access it through script injection from a content script. – rsanchez Mar 21 '14 at 23:48

1 Answers1

12

You can't. The extension's background page runs in one process, while the tab that was updated runs in a separate process. Different processes can't share objects, so you can't directly access the window object of a tab from an extension's background page. You have to use a content script to get extension code to run inside the tab's process.

Jeffrey Yasskin
  • 5,171
  • 2
  • 27
  • 39
  • And then I have to inject code using ` – f.ardelian Mar 24 '14 at 20:23
  • 1
    You may be able to avoid the ` – Jeffrey Yasskin Mar 24 '14 at 23:10