Now I want to start a chrome app from a chrome extension(in fact, I want to start chrome app through a url, which I have no idea to do). Here comes the question. I added
"permissions": [
"management"
],
in the manifest of extension. However, when I want to start app by using
chrome.management.launchApp("XXXX", function() {});
, the console says
Uncaught TypeError: Cannot read property 'launchApp' of undefined
So I wonder why I cannot use chrome management API. Thanks!
A workaround for this is available at the SO question Run chrome application from a web site button
Copying Hasitha's answer here for direct reference.
Add the following piece of code where you want to invoke the chrome app
//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);
}
}
}
);
in your chrome application manifest.json define the externally_connectable
url/ urls as follows,
{ "name": "App name", "description": "App Desc", "version": "1",
...
"externally_connectable": {
"matches": ["*://localhost:*/"]
}
}
In your application background.js setup a listener to be invoked when a message is sent from the web page as follows,
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With