auo
auo

Reputation: 572

google chrome, close tab event listener

I'm working on a chrome extension and I need to get the event when the tab is closed so I can fire of an post to a server. This is what I have atm.

chrome.tabs.onRemoved.addListener(function (tabId) {
   alert(tabId);
});

But I can't get it to work. Anyone got any ideas?

Edit:

When I'm running it, it says

Uncaught TypeError: Cannot read property 'onRemoved' of undefined

Edit2: manifest.json

{
"name": "WebHistory Extension",
"version": "1.0",
"manifest_version": 2,
"description": "storing webhistory",
    "content_scripts":[
        {
            "matches": ["http://*/*"],
            "js": ["jquery-1.7.min.js","myscript.js"],
            "run_at": "document_end"
        }
    ],
    "permissions" : ["tabs"]
}

Upvotes: 0

Views: 3171

Answers (1)

Konrad Dzwinel
Konrad Dzwinel

Reputation: 37923

You can't use chrome.tabs API in content scripts:

However, content scripts have some limitations. They cannot: Use chrome.* APIs (except for parts of chrome.extension)

source

What you need to do is to establish communication between content script and background page. Background page has access to chrome.tabs API:

These limitations aren't as bad as they sound. Content scripts can indirectly use the chrome.* APIs, get access to extension data, and request extension actions by exchanging messages with their parent extension.

source

Everything is in the first five paragraphs of content script documentation.

Upvotes: 1

Related Questions