Where to register the Native Messaging Host in Chrome OS

二次信任 提交于 2019-12-13 01:00:00

问题


In this documentation there's the location for Windows, Mac OS and Linux.

I assumed Chrome OS would work the same as "standard" linux, but i couldnt get my app working ...

com.my_app.host.json (located in /etc/opt/chrome/native-messaging-hosts/)

{
  "name": "com.my_app.host",
  "description": "My Host",
  "path": "/home/user/bfd93db2180e0d7645b1f4cce2d2c7ed9e0d835c/Downloads/host.sh",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://APP_ID/"
  ]
}

main.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.my_app.host";
  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();
});

index.html
<html>

<head>
  <script src='./main.js'></script>
</head>

<body>
  <button id='connect-button'>Connect</button>
  <input id='input-text' type='text' />
  <button id='send-message-button'>Send</button>
  <div id='response'></div>
</body>

</html>

It just says:

Connecting to native messaging host com.my_app.host

Failed to connect: Specified native messaging host not found.

Also i cant enable logging as explained in the documentation because on Chrome OS you cant just open Chrome via a command.

Would be great if someone could help me :)

Basically i just want to create a little GUI for launching Crouton commands.


回答1:


Chrome OS does not support third-party native messaging hosts. As of writing, only two native messaging hosts are supported. One for testing, and one for Chrome Remote Desktop (source: native_message_host_chromeos.cc).

On Chrome OS, logs are available at /var/log/chrome/chrome and /var/log/ui/ui.LATEST. There are other ways to read/toggle the log (see https://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/building-chromium-browser#TOC-Debugging and https://github.com/ds-hwang/wiki/wiki/Build-Chromium-for-Chromium-OS-and-Deploy-to-real-device#log).

But the absence of built-in support for native messaging hosts does not mean that you cannot achieve what you want. Start a local HTTP server in Crouton, and communicate between the Chrome extension / app and the HTTP server via the standard Web APIs (XMLHttpRequest, fetch, WebSocket, ...). On the server (running in Crouton), you can do whatever you want (e.g. starting local scripts). Make sure that the server implements proper authentication (to prevent unauthorized access by other web sites or extensions) (and preferably bind to a local address only to make the server inaccessible over network).



来源:https://stackoverflow.com/questions/34459259/where-to-register-the-native-messaging-host-in-chrome-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!