79
votes

How to start an external application from a Google Chrome Extension?

So basically I have an executable file which does the job when you launch it. I need to be able to start it without a window (it is a console application) and pass the current URL to it in an argument,

6
If there is a god then Google will never allow anyone to do this under any circumstances.Azeem.Butt
could you have at least have your own exe file that you can start...without thinking of c or C++.Muhammad Umer
@Azeem.Butt what if you are in a corporate environment and all users explicitly allow this sort of action? Seems like a very helpful workflow to me. Hence native messaging (which first requires a separate native app to be installed/run) seems the way to go (see jonny's answer)JoeCool
@msangel Since the question you mention is posterior to this one it should be considered as the duplicate.C.Champagne

6 Answers

84
votes

Previously, you would do this through NPAPI plugins.

However, Google is now phasing out NPAPI for Chrome, so the preferred way to do this is using the native messaging API. The external application would have to register a native messaging host in order to exchange messages with your application.

23
votes

You can't launch arbitrary commands, but if your users are willing to go through some extra setup, you can use custom protocols.

E.g. you have the users set things up so that some-app:// links start "SomeApp", and then in my-awesome-extension you open a tab pointing to some-app://some-data-the-app-wants, and you're good to go!

2
votes

I go for hypothesys since I can't verify now.

With Apache, if you make a php script on your local machine calling your executable, and then call this script via POST or GET via html/javascript?

would it function?

let me know.

1
votes

There's an extension for Chrome (SimpleGet) that has a plugin for Windows and Linux that can execute an app with command line parameters.....
http://pinel.cc/
http://code.google.com/p/simple-get/
http://www.chromeextensions.org/other/simple-get/

0
votes

Question has a good pagerank on google, so for anyone who's looking for answer to this question this might be helpful.

There is an extension in google chrome marketspace to do exactly that: https://chrome.google.com/webstore/detail/hccmhjmmfdfncbfpogafcbpaebclgjcp

0
votes

Native messaging host

Chrome-extensions

{
  "name": "AppName",
  "description": "",
  "version": "1.0",
  "manifest_version": 3,
  "permissions": [
    "nativeMessaging"  // 👈 https://developer.chrome.com/docs/extensions/mv3/declare_permissions/
  ]
  // ...
}

Host

Add schema

@echo off
:: If you add "/f" then you can force write.
REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.my_company.my_application" ^
 /ve /t REG_SZ ^
 /d "%~dp0Mymanifest.json"
// Mymanifest.json
{
  "name": "com.my_company.my_application",
  "description": "My Application",
  "path": "relative_dir/my.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://nbjjflbnekmabedahdolabcpahfjojjb/"
  ]
}

chrome.runtime.sendNativeMessage

example:

// your.js
chrome.runtime.sendNativeMessage("com.my_company.my_application",
  {key1: "value1", key2: "value2"}, // 👈 Send those parameters to your program.
  (response) => {
    console.log(response)
  }
)

Example repository

I have created a project thunder/e11fde9 whose ultimate goal is to be able to use a browser as input and then open a specified file locally (without a mouse, if possible)

It is still in development, but I think the early code is enough. The link is below.

Which already has a log that records the results of the browser's transmission, while the browser can also get the program's return value.

Reference