Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access window object from a background Chrome extension

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.

like image 284
f.ardelian Avatar asked Mar 21 '14 23:03

f.ardelian


People also ask

Do Chrome extensions run in background?

Since Chrome is already running in the background, there is less that has to take place behind the scenes before your new browser window appears. This makes the browser feel quick, even on slower machines. Running in the background also allows any Chrome extensions you might have installed to continue to operate.

How do I debug a chrome background?

To find out whats wrong with your background. html, under chrome://chrome/extensions/ (i.e., manage extensions), click on the background. html link. This will load the developer tools but without background.

What is chrome background page extension?

As the architecture overview explains, the background page is an HTML page that runs in the extension process. It exists for the lifetime of your extension, and only one instance of it at a time is active.

Can we use jquery in chrome extension?

You can just put jquery. js into extension folder and include it in the manifest: { "name": "My extension", ... "content_scripts": [ { "matches": ["http://www.google.com/*"], "css": ["mystyles.


1 Answers

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.

like image 145
Jeffrey Yasskin Avatar answered Sep 25 '22 06:09

Jeffrey Yasskin