Flare0n
Flare0n

Reputation: 95

How to check URLs of all tabs in all windows? (Google Chrome Extension)

I want to check URLs of all tabs in all windows, and if there is a tab which matches "http://example.com/foo/bar.html", I want to focus on the tab. (If not, I want to open "http://example.com/foo/bar.html" in a new tab.)
However, I don't know how to check URLs.

How can I do this?

Upvotes: 3

Views: 2069

Answers (2)

Métoule
Métoule

Reputation: 14472

As jshthornton mentioned, you can use the chrome.tabs.query method:

chrome.tabs.query("http://example.com/foo/bar.html", function(tabs)
{
    // if no tab found, open a new one
    if (tabs.length == 0)
        chrome.tabs.create({ url: "http://example.com/foo/bar.html" });

    // otherwise, focus on the first one
    else
        chrome.tabs.update(tabs[0].id, { active: true });
});

Note that this code won't bring the focus on the tab's window if the first found tab is not on the current active window.

You can check the following links for more details:

Upvotes: 2

jshthornton
jshthornton

Reputation: 1314

I am not going to fully outline the implementation, but take a look at this entry:

http://developer.chrome.com/extensions/tabs.html#method-query

As you can see it accepts a 'url' check.

Upvotes: 2

Related Questions