views:

1920

answers:

2

I have created a chrome extension and managed to open the popup.html file using window.open. however I want to open it in a new tab, I've tried lots of different ways including:

<script type="text/javascript" language="JavaScript">
  chrome.tabs.create('url': 'popup.html');

Am I just placing the code in the wrong place or is it the wrong code altogether?

+5  A: 

Hello, why would you want to open the popup.html in a new tab? You should create a different page for that. Anyways, if you want to open up the popup.html, in a new tab, you would need to pass in the extension url.

http://code.google.com/chrome/extensions/extension.html#method-getURL

chrome.tabs.create({'url': chrome.extension.getURL('popup.html')}, function(tab) {
  // Tab opened.
});
Mohamed Mansour
This snippet needs to be called from the background page, so if you're trying to open the tab from a content script, send a message to the background page that will trigger this code. Here is the appropriate page on sending messages this way: http://code.google.com/chrome/extensions/messaging.html
Arne Roomann-Kurrik
I had to wrap the 'url':chrome.extension.getURL('popup.html') in brackets. {'url': chrome.extension.getURL('popup.html')}
AdamB
A: 

to create a new tab use chrome.tabs.create(Object properties, function callback) as described on http://code.google.com/chrome/extensions/tabs.html

The object properties could contain fields for windowId, index, url and selected. The optional callback function receives a Tab object of the newly created tab.

Not sure why you would like to show the popup.html in a new tab, but I find it very useful while developing/debugging my extension ... it is quite a pain that on the extension page there is "usually" only a link to the background page.

So the simplest example to create a new tab in the current window and get it selected would look like this:

chrome.tabs.create({'url': chrome.extension.getURL('popup.html'), 'selected': 'true'});

Would love to know how to open it in a new window and maybe in a kiosk mode ;-)

kodra