【问题标题】:C# NativeMessaging to ChromeC# NativeMessaging 到 Chrome
【发布时间】:2015-03-12 15:41:38
【问题描述】:

我正在尝试创建一个插件/侦听器以让外部应用程序将消息泵入我的 SPA 页面/应用程序。

我创建了清单、JS 文件并添加了一个 reg 条目,但无济于事,我的侦听器没有被触发。

我有:

// JavaScript 源代码

Native.js - 插件

chrome.runtime.onMessageExternal.addListener(
  function (request, sender, sendResponse) {
      console.log(request);
      console.log(sender);
      console.log(sendResponse);
  });

Manifest.json - 插件 { "manifest_version": 2,

    "name": "Native Messaging Example",
    "version": "1.0",

    "permissions": [
        "nativeMessaging"
    ],
    "background": {
        "scripts": [ "Native.js" ]
    },
     "externally_connectable": {
        "matches": [ "*://localhost/*", "*://casetest/*", "*://case/*" ]
    }
}

C#

程序.js

using System;
using System.IO;

namespace ChromeNativeMessaging
{
    class Program
    {
        static void Main(string[] args)
        {
            OpenStandardStreamOut("data");
            Console.ReadLine();
        }

        private static void OpenStandardStreamOut(string stringData)
        {
            String str = "{\"text\": \"" + stringData + "\"}";
            //String str = stringData;
            Stream stdout = Console.OpenStandardOutput();

            stdout.WriteByte((byte)str.Length);
            stdout.WriteByte((byte)'\0');
            stdout.WriteByte((byte)'\0');
            stdout.WriteByte((byte)'\0');
            Console.Write(str);
        }
    }
}

Manifest.json

    {
    "name": "com.example.nativeMessage",
    "description": "Hello World App",
    "path": "C:\\Users\\sas\\Documents\\visual studio 2013\\Projects\\ChromeNativeMessaging\\ChromeNativeMessaging\\bin\\Debug\\ChromeNativeMessaging.exe",
    "type": "stdio",
    "allowed_origins": [
        "chrome-extension://gbdadncpjaecammkmeolpbembeedjohb/"
    ]
}

【问题讨论】:

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


    【解决方案1】:

    Chrome 从不监听来自外部的本地连接(onMessageExternal 不适用),运行您的 C# 代码也不会神奇地联系 Chrome。

    只有您的 JavaScript 端可以启动连接(通过运行本机主机的新副本),之后您可以保持连接打开(如果您使用 connect() 执行此操作)。

    因此,如果您想在浏览器外部发生事件时收到通知,则需要从扩展端启动原生主机,然后在原生主机中处理这些事件。

    总而言之,您应该(重新)阅读完整的文档:https://developer.chrome.com/extensions/nativeMessaging

    【讨论】:

    • 通过在互联网上阅读,我认为这些帖子必须缺少 C# 的“神奇”连接。我会重新阅读并重新思考我的解决方案 - 谢谢!
    • 基本上不能直接敲Chrome的门。但是你可以制作一个像代理/守护进程一样的小型 Native Host 模块,在后台运行并收听你想收听的任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2021-08-27
    相关资源
    最近更新 更多