javascript - Chrome 扩展 native 消息同步

标签 javascript google-chrome-extension chrome-native-messaging

我对 Windows 上的 native 消息同步有疑问。我正在尝试同步 backgroundPage 和 hostApp 之间的消息。通常,我们像这样使用本地消息传递:

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

function sendNativeMessage() {
  message = {"command": 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.dmtest1";
  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();
});


<html>
  <head>
    <script src='./popup.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>

但是 sendNativeMessage() 和 onNativeMessage(..) 函数是异步的,我想让它们同步。我尝试了下面的方法,但它无法从主机(c++ exe)获取响应数据,导致 chrome 崩溃。

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

  port.onMessage.addListener(function(msg) { 
    appendMessage("Receive message: <b>" + JSON.stringify(msg) + "</b>");
  });
}

我该怎么做,有可能吗,有什么帮助吗?

最佳答案

经过几天的搜索和测试,我终于找到了答案(这里:how to handle chrome.runtime.sendNativeMessage() in native app)并解决了我的问题。我放弃了让我的回调函数同步的想法,我只是使用另一种方式在 Chrome 扩展后台和我的本地主机应用程序之间进行通信。而不是 var port = chrome.runtime.connectNative(hostName);port.onMessage.addListener(onNativeMessage);port.postMessage(message); 我使用下面的代码在后台和后台之间发送和接收数据hostapp,并且是同步的:

chrome.runtime.sendNativeMessage(hostName, sendMsg, function(response) {
            if (chrome.runtime.lastError) {
                alert("ERROR: " + chrome.runtime.lastError.message);
            } else {
                sendResponse({farewell: ParseJSON(response)});
            }
        });

关于javascript - Chrome 扩展 native 消息同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201420/

相关文章:

javascript - jQuery - 获取元素的高度并将该高度应用于同一父元素内的另一个元素

html - 如何在内容脚本中的 Chrome 扩展中使用@font-face

macos - 使用 Xamarin Mac、C# 的跨浏览器、跨平台 native 消息传递

c++ - 在/Library/Google/Chrome/NativeMessagingHosts 中创建 list 文件需要 super 用户权限

c++ - chrome 原生消息 : can a native host executable start with arguments?

javascript - 如何更新组件安装路径?

javascript - Angular HTTP 请求延迟拦截器未按预期工作

javascript - 从 Chrome 扩展发送 AJAX

javascript - 为什么另一个函数看不到我的全局变量?

javascript - SUBLIME TEXT 编辑器的控制台中未定义窗口