未找到Google扩展程序原生邮件主机

时间:2015-07-09 10:55:10

标签: javascript c# google-chrome-extension chrome-native-messaging

我遇到了本地消息的问题。

当我尝试连接本机应用时,我收到此错误:

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.json这个应用程序。

{
  "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();
});

和manifest.json这个

{
  // 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 \ Google \ Chrome \ NativeMessagingHosts \ com.my_company.my_application

HKEY_CURRENT_USER \ SOFTWARE \谷歌\铬\ 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');

1 个答案:

答案 0 :(得分:1)

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

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