6

I am building a chrome app, which will simply open a link, for example "http://www.cnn.com/" in a new tab in chrome.

I have the following code in my manifest.json

{
  "manifest_version": 2,
  "name": "CNN",
  "version": "2.1",
  "permissions": ["webview", "pointerLock", "geolocation", "videoCapture"],
  "app": {
    "background": {
      "scripts": ["main.js"]
    }
  }
}

And this is what i have in main.js:

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('http://www.cnn.com/', {

  });
});

I have also tried,

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create({ "url": "http://cloudsupport.neonova.net/home" });
});

as well as:

chrome.app.runtime.onLaunched.addListener(function(tab) {
  chrome.app.tab.create({ "url": "http://cloudsupport.neonova.net/home" });
});

PLease help.

Thank you

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
user3464774
  • 63
  • 1
  • 3

3 Answers3

6

Anyway, I've tried window.open and it forked like a charm:

'use strict';

chrome.app.runtime.onLaunched.addListener(function() {
    window.open("https://google.com/");
});

So it might work for you as well.

Ale
  • 1,998
  • 19
  • 31
6

As of chrome 42, chrome.browser may help:

chrome.app.runtime.onLaunched.addListener(function() {
    chrome.browser.openTab({
      url: 'https://google.com/'
    });
});
Ivan Yan
  • 475
  • 6
  • 8
-3

Reference: https://developer.chrome.com/extensions/tabs#method-create

var options= { url: "http://cloudsupport.neonova.net/home" };

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.tabs.create(options);
});

then in manifest.json. add this permission.

...
"permissions": ["tabs","webview", "pointerLock", "geolocation", "videoCapture"]
...
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
  • Seems like more native way to do it, but why would you need all these permissions (especially `geolocation` and `videoCapture`)? – Ale Mar 27 '14 at 18:37
  • i don't know that's what op has...for this you only need tabs permission because you are accessing url...Though i am not sure if you need any permission. But just in case if it's needed i'll wont hesitate to add tabs permission. there is one more permission type called activeTab – Muhammad Umer Mar 27 '14 at 19:22
  • And i am not sure what will happen if no chrome instance is running. Ill have to check, what happens if no windows is open...there can't be a tab if there is no window. maybe code should check that if window is open or not. Or maybe tabs.create itself opens a window if there isn't one open already.. – Muhammad Umer Mar 27 '14 at 19:33
  • 2
    Note that chrome.tabs.create will only work in extensions or legacy packaged apps. – danf Jan 09 '15 at 16:14