11

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.

1
  • 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. Commented Mar 21, 2014 at 23:48

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And then I have to inject code using <script> tags to pass messages from the web page to the content script and from the content script to the background script. I figured it out after a lot of searching, so I'm just adding to your answer. There's no way to access objects, but I can send JSON data (which is serialized into strings when passed to message listeners).
You may be able to avoid the <script> tags, depending on exactly what you need to do: the web page can fire events on DOM objects, and the content script can listen to those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.