Native messaging problem

346 views
Skip to first unread message

Fernando Sanchez Ortega

unread,
Nov 9, 2023, 5:00:35 AM11/9/23
to Chromium Extensions
Hi there, 

I am trying to create a simple Native messaging application on my MacOS.

I was following the steps discussed in the following link:
https://developer.chrome.com/docs/extensions/mv3/nativeMessaging/

I was able to open any application that I have installed in my MacOS such as Slack, vscode etc etc.

I was wondering if I could launch a script that I have in my extension folder, or a python3 application... because everytime that I try to launch some of these kind of files it return me the following error::

Failed to connect: Failed to start native messaging host.

I will attach the JS code that is using to send the information and the manifest.

popup.js:




var port = null;

var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}


function appendMessage(text) {
document.getElementById('response').innerHTML += "<p>" + text + "</p>";
}

function updateUiState() {
if (port) {
document.getElementById('connect-button').style.display = 'none';
document.getElementById('input-text').style.display = 'block';
document.getElementById('send-message-button').style.display = 'block';
} else {
document.getElementById('connect-button').style.display = 'block';
document.getElementById('input-text').style.display = 'none';
document.getElementById('send-message-button').style.display = 'none';
}
}

function sendNativeMessage() {
message = {"text": document.getElementById('input-text').value};
port.postMessage(message);
appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>");
}

function onNativeMessage(message) {
appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
}

function onDisconnected() {
appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
port = null;
updateUiState();
}

function connect() {
var hostName = "com.google.chrome.example.echo";
appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
updateUiState();
}

document.addEventListener('DOMContentLoaded', function () {
document.getElementById('connect-button').addEventListener(
'click', connect);
document.getElementById('send-message-button').addEventListener(
'click', sendNativeMessage);
updateUiState();
});




Manifest.js

{
// Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
"name": "Chrome Extension binaryMessaging MV3",
"description": "Learning limitations and use of binaries",
"version": "0.1.0",
"manifest_version": 3,
"icons": {
"16": "/images/Z.png",
"32": "/images/Z.png",
"48": "/images/Z.png",
"128": "/images/Z.png"
},
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "/images/Z.png",
"32": "/images/Z.png",
"48": "/images/Z.png",
"128": "/images/Z.png"
}
},
"permissions": [
"nativeMessaging"
]
}


Thanks in advance!

Oliver Dunk

unread,
Nov 9, 2023, 5:08:24 AM11/9/23
to Fernando Sanchez Ortega, Chromium Extensions
Hi Fernando,

That should definitely be possible.


Is it possible you haven't set the write permissions on the file or included `#!/usr/bin/env python` to mark the file as a Python executable?
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/dbd60e1f-82d9-43ed-847b-6b91df935451n%40chromium.org.

Fernando Sanchez Ortega

unread,
Nov 9, 2023, 5:18:42 AM11/9/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Fernando Sanchez Ortega
Hi Oliver, 

Thanks for your quick response:

I give them all the permission just in case xd:

I will share with you more information:
com.google.chrome.example.echo.json:
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
  "name": "com.google.chrome.example.echo",
  "description": "Chrome Native Messaging API Example Host",
  "path": "/Applications/native-messaging-example-host",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
  ]
}

The code in python is exactly the same one of the repository that you shared with me, and the permission given to the python application are:

-rwxrwxrwx@  1 fernando  admin  3605 Nov  9 10:45 native-messaging-example-host

Any idea?

Oliver Dunk

unread,
Nov 9, 2023, 5:25:37 AM11/9/23
to Fernando Sanchez Ortega, Chromium Extensions

Hmm, that seems ok to me then.

Do you definitely have Python installed and available as `python` in your command line?

Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB

Fernando Sanchez Ortega

unread,
Nov 9, 2023, 5:31:20 AM11/9/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Fernando Sanchez Ortega
yeah :(! 

I can run it from a simple terminal without problem:

`python`
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 19 2020, 20:48:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information

Also the python application is working when I call it.

Oliver Dunk

unread,
Nov 10, 2023, 7:15:05 AM11/10/23
to Fernando Sanchez Ortega, Chromium Extensions
Hmm, that all seems fine to me.

And if you run `/Applications/native-messaging-example-host` in the terminal (just that, without python beforehand) it works?
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB

Reply all
Reply to author
Forward
0 new messages