现在我想从 chrome 扩展程序启动 chrome 应用程序(事实上,我想通过 url 启动 chrome 应用程序,我不知道该怎么做)。 问题来了。我加了
"permissions": [
"management"
],
在扩展 list 中。但是,当我想使用
启动应用程序时chrome.management.launchApp("XXXX", function() {});
,控制台显示
Uncaught TypeError: Cannot read property 'launchApp' of undefined
所以我想知道为什么我不能使用 chrome 管理 API。谢谢!
最佳答案
SO 问题 Run chrome application from a web site button 提供了解决方法
正在复制 Hasitha此处的回答供直接引用。
在要调用 chrome 应用程序的地方添加以下代码
//data passed from web to chrome app chrome.runtime.sendMessage('your_chrome_app_id', { message: "version" }, function (reply) { if (reply) { if (reply.version) { //log the response received from the chrome application console.log(reply.version); } } } );
在您的 chrome 应用程序 manifest.json 中定义
externally_connectable
url/urls 如下,{ "name": "应用名称", "description": "应用描述", “版本”:“1”,
... "externally_connectable": { "matches": ["*://localhost:*/"] } }
在您的应用程序 background.js 中设置一个监听器,以便在从网页发送消息时调用,如下所示,
chrome.runtime.onMessageExternal.addListener( function(request, sender, sendResponse) { if (request) { if (request.message) { if (request.message == "version") { //my chrome application initial load screen is login.html window.open('login.html'); //if required can send a response back to the web site aswell. I have logged the reply on the web site sendResponse({version: '1.1.1'}); } } } return true; });
关于javascript - 无法使用 chrome.management API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382305/