在 this文档中有适用于 Windows、Mac OS 和 Linux 的位置。
我认为 Chrome 操作系统的工作方式与“标准”Linux 相同,但我无法让我的应用程序工作......
com.my_app.host.json(位于/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>
它只是说:
连接到 native 消息传递主机 com.my_app.host
连接失败:找不到指定的 native 消息传递主机。
此外,我无法按照文档中的说明启用日志记录,因为在 Chrome 操作系统上,您无法仅通过命令打开 Chrome。
如果有人能帮助我那就太好了:)
基本上我只是想创建一个小 GUI 来启动 Crouton 命令。
最佳答案
Chrome 操作系统不支持第三方 native 消息传递主机。截至撰写本文时,仅支持两个 native 消息传递主机。一份用于测试,一份用于 Chrome 远程桌面(来源: native_message_host_chromeos.cc )。
在 Chrome 操作系统上,日志位于 /var/log/chrome/chrome
和/var/log/ui/ui.LATEST
。还有其他方法可以读取/切换日志(请参阅
https://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/building-chromium-browser#TOC-Debugging和
https://github.com/ds-hwang/wiki/wiki/Build-Chromium-for-Chromium-OS-and-Deploy-to-real-device#log )。
但是缺乏对 native 消息传递主机的内置支持并不意味着您无法实现您想要的目标。在 Crouton 中启动本地 HTTP 服务器,并通过标准 Web API( XMLHttpRequest
、 fetch
、 WebSocket
、...)在 Chrome 扩展程序/应用程序和 HTTP 服务器之间进行通信。在服务器上(在 Crouton 中运行),您可以做任何您想做的事情(例如启动本地脚本)。
确保服务器实现正确的身份验证(以防止其他网站或扩展程序未经授权的访问)(最好绑定(bind)到本地地址,以使服务器无法通过网络访问)。
关于google-chrome-app - 在 Chrome 操作系统中的何处注册 native 消息传递主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459259/