Search code examples
javascriptgoogle-chrome-extensiontabsirc

Why isn't chrome tabs working?


For reference, here's my project.

Here's my manifest:

{
  "name": "Chatty",
  "description": "Yet another IRC client written as a Chrome application",
  "version": "0.0.1",
  "manifest_version": 2,
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  }
}

Here's my SUPER basic web page that I would like to open in a new tab:

<!doctype HTML>
<html>
  <body>
    Hello, Chatty IRC client!
  </body>
</html>

And, here's how I've been trying to open the new tab:

chrome.app.runtime.onLaunched.addListener(function(){ 
  chrome.tabs.create({ url: chrome.extensions.getUrl("irc.html") });
});

Finally, here's my issue: it doesn't want to open in a new tab. Can someone help me get this working?


Solution

  • You're mixing the extension API with the Chrome app APIs. There is some overlap in APIs, but there are also APIs that are only available to one of them.

    Your current manifest declares a Chrome app. If you really want to have a Chrome app, then you cannot create tabs in the browser. To launch the UI, you'll have to use the chrome.app.window API to create a window (create).

    If you prefer to integrate in the browser, then you have to create an extension. The manifest file is similar to the app, but you have to remove "app" (as shown below). Extensions don't have an icon in the launcher, nor access to the chrome.app.runtime API, so you have to choose a different way of launching your UI, e.g. via a browser action.

    {
      "name": "Chatty",
      "version": "0.0.1",
      "manifest_version": 2,
      "background": {
        "scripts": ["background.js"]
      }
    }
    

    Since you're developing an IRC client, you probably want to create a Chrome App instead of an extension. chrome.sockets can be used to communicate using IRC over TCP (this low-level network API is not available to extensions).