How to launch a Chrome App from a Chrome Extension?

Typically an app is opened from chrome://apps/ or in the button Chrome apps, but how would from an external Chrome Extension?

My extension:

->manifest.json
->background.js

My app:

->manifest.json
->background.js

Then, how to launch my app from my extension?

Upvotes: 3

Views: 3686

Answers (2)

Xan
Xan

Reputation: 77482

It's in the docs: chrome.management.launchApp

chrome.management.launchApp("YourAppsId", function(){
  if(chrome.runtime.lastError) console.error(chrome.runtime.lastError);
  else console.log("App launched");
});

You will need the "management" permission for that.


Edit: see Daniel Herr's answer for a less invasive solution.

Upvotes: 4

Daniel Herr
Daniel Herr

Reputation: 20438

If you own both items you can just use messaging to open the app window when receiving a message from the extension. This does not have a permissions warning that will repel users like management. https://developer.chrome.com/extensions/messaging

First, add this to both manifests:

"externally_connectable": { "ids": [ "idoftheotheritem" ] }

Then, listen for messages in your app and create a app window:

chrome.runtime.onMessageExternal.addListener(function() {
  chrome.app.window.create("app.html");
}

Finally, send from the extension:

chrome.runtime.sendMessage("idoftheapp", { launch: true })

Upvotes: 8

Related Questions