没有找到谷歌扩展本地消息传递主机

Google extensions Native messaging host not found

本文关键字:消息传递 主机 扩展 谷歌      更新时间:2023-09-26

本地消息有问题。

当我尝试连接本地应用程序时,我得到这个错误:

Connecting to native messaging host com.google.chrome.example.echo
Failed to connect: Specified native messaging host not found.

第一个本机应用c#

static void Main(string[] args)
        {
            string message = "test message from native app.";
            OpenStandardStreamOut(message);
            while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
            {
                OpenStandardStreamOut("Received to Native App: " + OpenStandardStreamIn());
                OpenStandardStreamOut("Recieved: " + OpenStandardStreamIn());
            }
            Console.WriteLine("END");
        }
        private static string OpenStandardStreamIn()
        {
            //// We need to read first 4 bytes for length information
            Stream stdin = Console.OpenStandardInput();
            int length = 0;
            byte[] bytes = new byte[4];
            stdin.Read(bytes, 0, 4);
            length = System.BitConverter.ToInt32(bytes, 0);
            string input = "";
            for (int i = 0; i < length; i++)
            {
                input += (char)stdin.ReadByte();
            }
            return input;
        }
        private static void OpenStandardStreamOut(string stringData)
        {
            //// We need to send the 4 btyes of length information
            string msgdata = "{'"text'":'"" + stringData + "'"}";
            int DataLength = msgdata.Length;
            Stream stdout = Console.OpenStandardOutput();
            stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
            stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
            stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
            stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
            //Available total length : 4,294,967,295 ( FF FF FF FF )
            Console.Write(msgdata);
        }

,这是manifest。

{
  "name": "com.my_company.my_application",
  "description": "Native messaging host",
  "path": "C:''Documents and Settings''xx xx''Moje dokumenty''Visual Studio 2010''Projects''KomunikacjaChrome''KomunikacjaChrome''bin''Debug''KomunikacjaChrome.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
  ]
}

这是如何chrome扩展:

// 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.
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();
});

和清单。

{
  // 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.",
  "app": {
    "launch": {
      "local_path": "main.html"
    }
  },
  "icons": {
    "128": "icon-128.png"
  },
  "permissions": [
    "nativeMessaging"
  ]
}
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>

我还添加了manifest。json (c#)到regedit

HKEY_LOCAL_MACHINE ' SOFTWARE '谷歌' ' NativeMessagingHosts ' com.my_company铬。my_application

HKEY_CURRENT_USER ' SOFTWARE '谷歌Chrome ' NativeMessagingHosts ' ' com.my_company.my_application

C:'Documents and Settings'xx xx'Moje dokumenty'Visual Studio 2010'Projects'KomunikacjaChrome'KomunikacjaChrome'bin'Debug'manifest.json

我忘记什么了吗?我想我已经迈出了所有的步骤,但还是不行。

谢谢你的帮助!

编辑

我解决了这个问题,在main.js我没有添加端口

var port = chrome.runtime.connectNative('com.my_company.my_application');

我解决了这个问题,在main.js中我没有添加端口

var port = chrome.runtime.connectNative('com.my_company.my_application');