Anne O'Nyme
Anne O'Nyme

Reputation: 5

Chrome extension - Detect URL and execute an application

I know that with a basic chrome extension we can select domains where our extension will work (<all_urls>), but is this possible to execute an application ( internal ) when the user visit a specific domain?

I looked previously and saw "Native Messaging". ( I'm currently studying arithmetics ) I want my extension to open automaticly calc.exe when I'm on my working lab's website.

I already did this:

manifest.json

{
    "name": "Mon extension",
    "version": "0.0.1",
    "background": {
        "scripts": [ "background.js" ]
    },
    "browser_action": {
        "default_title": "Ouvrir la calculatrice"
    },
    "permissions": [
        "nativeMessaging"
    ],
    "manifest_version": 2
}

But I did not understand how to manage the javascript part, can someone explain it to me ?

My calc.bat's manifest.json :

{
    "name": "application",
    "description": "Lauching my app",
    "path": "C:\\Users\\root\\Documents\\calc.bat",
    "type": "stdio",
    "allowed_origins": [
        "chrome-extension://blfgmcilkaooeokpomhcpnfnhppjklcb"
    ]
}

Calc.bat is simply: @echo off & start calc.exe

Thanks in advance. If my question is not clear enough, tell me, I'll modify it.

Upvotes: 0

Views: 931

Answers (1)

juvian
juvian

Reputation: 16068

Couldn´t get yours to work but the example I linked worked perfectly so started editing that one and ended up with this :

  • Download sample host
  • Edit native-messaging-example-host.bat and replace with :

    @echo off

    calc.exe

  • Run install_host.bat

  • Make a folder called app on same directory as host folder
  • Create 3 files : background.js, main.js, manifest.json

The content is:

background.js:

chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
    if (message.action == "open app") {
        chrome.runtime.connectNative("com.google.chrome.example.echo");
    }
});

main.js:

chrome.runtime.sendMessage({action: "open app"}, function(response) {
    console.log(response);
});

manifest.json:

    {
      // Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
      "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
      "name": "Native Messaging Example",
      "version": "1.0",
      "manifest_version": 2,
      "description": "Send a message to a native application.",
      "content_scripts": [{
        "js": ["main.js"],
        "matches": ["<all_urls>"]
      }],
      "background": {
        "scripts": ["background.js"]
      },
      "permissions": [
        "nativeMessaging"
      ]
    }

Finally, install the extension. This one in particular will open calc.exe every time you open/load a page, which can be easily changed with matches.

Another thing to note is that without the key in the manifest, it does not seem to work.

Upvotes: 1

Related Questions