opening the extension option page

1,475 views
Skip to first unread message

Paolo Casaschi

unread,
Sep 6, 2011, 2:26:06 PM9/6/11
to Chromium-extensions
If you open the extension options page using the chrome://extensions
page or the menu item in the page action / browser action, chrome has
a mechanism to remember if the options page is opened already, so it
just switches to the tab already open instead of opening a duplicate.

If I open the page with javascript, such as

chrome.tabs.create({url: "options.html"});

then chrome opens a new tab any time, creating duplicates every time.

Question: is there an API or something available to extensions to open
the options page with the same effect as chrome does?

Thanks.

Ben

unread,
Sep 6, 2011, 6:16:25 PM9/6/11
to Paolo Casaschi, Chromium-extensions
You can do this (or something similar):

extviews = chrome.extension.getViews({"type": "tab"})

function focusTab(tab) {
// Focus the given tab
chrome.tabs.update(tab.id, {"selected": true});
}

for (var i=0; i++; i<=extviews.length) {
if (i == extviews.length) {
// Create new tab if past end of list and none open
chrome.tabs.create(
chrome.extension.getURL('/path/to/options.html')
);
} else if (extviews[i].location.href ==
chrome.extension.getURL('/path/to/options.html')) {
// Get the tab's object and focus the tab
extviews[i].chrome.tabs.getCurrent(focusTab);
break;
}
}


If you know options.html is the only page you'll ever have open in a
tab, you can skip the for loop and just check to see if there is at
least one item in the list.

Paolo Casaschi

unread,
Sep 6, 2011, 7:54:31 PM9/6/11
to Chromium-extensions
On Sep 6, 11:16 pm, Ben <benjo...@gmail.com> wrote:
> You can do this (or something similar):

it works, thanks!

Nige

unread,
Sep 6, 2011, 4:53:09 PM9/6/11
to Chromium-extensions


Hi Paulo

I'm been wondering the same thing.

Currently I'm doing this:

function openOptions()
{
var id = chrome.i18n.getMessage("@@extension_id") + "/options.html";

chrome.tabs.getAllInWindow( undefined, function(tabs)
{
for( var i = 0, tab; tab = tabs[ i ]; i++)
{
if( tab.url && tab.url.indexOf( id ) != -1 )
{
chrome.tabs.update( tab.id, {selected: true} );
return;
}
}

chrome.tabs.create( {'url': chrome.extension.getURL( 'options.html' ) },
function( tab )
{
}
);
});
}


which seems to work, but it feels like it should be easier...

Maybe somebody can put me right 8-)

Cheers,
Nige

PS It's my first post - please be gentle!

Dirk Durka

unread,
Jul 15, 2014, 12:49:56 PM7/15/14
to chromium-...@chromium.org
Alan that works once you make sure your variable names are consistent, i.e. change two instances of "options" to "options_url".

On Sunday, February 17, 2013 12:32:36 AM UTC-6, Alan Hamlett wrote:
Even better:

var options_url = chrome.extension.getURL('options.html');
chrome.tabs.query({
    url: options,
}, function(results) {
    if (results.length)
        chrome.tabs.update(results[0].id, {active:true});
    else
        chrome.tabs.create({url:options});
})

Rajesh Katalkar

unread,
Jul 15, 2014, 1:17:08 PM7/15/14
to Dirk Durka, Chromium-extensions

I think Google should minify our JavaScript after reviewing and then publish...

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/4b4a357b-c394-4254-a3ef-8d40eec7a174%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.
Reply all
Reply to author
Forward
0 new messages