如何从Chrome扩展程序启动Chrome应用程序?

时间:2014-06-13 05:30:55

标签: javascript google-chrome google-chrome-extension google-chrome-app

通常,应用程序是从 chrome:// apps / 按钮Chrome应用中打开的,但是外部Chrome扩展程序会如何?

我的扩展名:

->manifest.json
->background.js

我的应用:

->manifest.json
->background.js

然后,如何从我的扩展程序中启动我的应用

2 个答案:

答案 0 :(得分:6)

如果您拥有这两个项目,则只需使用消息传递即可在从扩展程序接收消息时打开应用程序窗口。这没有权限警告会拒绝用户管理。 https://developer.chrome.com/extensions/messaging

首先,将其添加到两个清单中:

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

然后,在应用中收听消息并创建应用窗口:

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

最后,从扩展名发送:

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

答案 1 :(得分:3)

它位于文档中:chrome.management.launchApp

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

您需要获得"management"权限。


编辑:请参阅Daniel Herr的answer了解侵入性较小的解决方案。