通过Native Messaging从Chrome扩展程序接收C#中的邮件 [英] Receiving message in C# from Chrome Extension through Native Messaging

查看:176
本文介绍了通过Native Messaging从Chrome扩展程序接收C#中的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取有效的标题标题以及来自Google Chrome的网址。我手动安装了Chrome扩展程序并创建了一个控制台应用程序,用于获取活动标签标题和网址。



首先,我安装了Chrome扩展程序,其中包含清单等文件。 json,background.js和popup.html。以下是我的代码。



background.js



I am trying to get active Tab Title as well as URL from Google Chrome. I have installed Chrome extension manually and created a console application for getting the active tab title and url.

First of all I have installed Chrome Extension which contains file like manifest.json, background.js and popup.html. Below is my code.

background.js

var port = chrome.runtime.connectNative('com.example.native');
  
function onActivate(activeInfo) 
{
     chrome.tabs.get(activeInfo.tabId, MyCurrentTab);
}   
function getUrl(title, url)
{
    var o = { title: title, url: url };
    try 
    {       
        port.postMessage(o);
    }
    catch(err)
    {

         port = chrome.runtime.connectNative('com.example.native');
         port.postMessage(o);
    }       
}

function MyCurrentTab(tab) 
{
     getUrl( tab.title, tab.url );
}

chrome.tabs.onActivated.addListener(onActivate);





manifest.json



{

name:watchURL,

description:阅读有效的Chrome网址,

版本:2.0,

权限:[

标签,

nativeMessaging

],

背景:{

scripts:[background.js],
持久:false

},

browser_action:{

default_title:活动网址读取,

default_popup:popup.html



},

manifest_version:2

}





然后我为本机消息创建了主机本机json文件。下面是代码。



{

name:com.example.native,

说明:对Chrome扩展程序的本机支持,

路径:ChromeExt.exe,

类型:stdio,

allowed_origins:[

chrome-extension:// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx /

]

}





之后,我为同名的本地消息创建了注册表项(com.example.native)。



C#控制台应用程序的代码如下所示:





manifest.json

{
"name": "watchURL",
"description": "Reading Active Chrome URL",
"version": "2.0",
"permissions": [
"tabs",
"nativeMessaging"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_title": "The active url read",
"default_popup": "popup.html"

},
"manifest_version": 2
}


Then I have created the host native json file for native messaging. Below is the code.

{
"name": "com.example.native",
"description": "Native support for Chrome Extension",
"path": "ChromeExt.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"
]
}


After that, I have created registry entry for native messaging with same name (com.example.native).

The code for C# console application is shown below,

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;
}





我保留了system32文件夹下的本机主机json文件和C#application .exe文件。但是当我要在Chrome浏览器上打开www.google.com或www.yahoo.com时,没有任何反应。我还在C#应用程序中编写了文件转储代码,但没有运气。



我将稍后在谷歌商店发布我的扩展程序,一旦手动成功完成。



现在我的问题是我需要如何执行此控制台应用程序并从Chrome扩展程序中获取标题和URL。



我现在应该做什么?请帮助我。



任何形式的帮助都将受到高度赞赏。



I have kept the native host json file and C# application .exe file under the system32 folder. But when I am going to open www.google.com or then www.yahoo.com at the chrome browser, nothing happens. I have also written code for a file dump inside that C# app but no luck.

I will publish my extension later on google store once it is manually done successfully.

Now my question is how I need to execute this console application and fetch the Title and URL from the Chrome Extension.

What I am supposed to do now? Please help me in this regard.

Any kind of help will be highly appreciated.

推荐答案

I找到了。我在上面发布的所有代码都是正确的。问题出在我的C#代码的另一种方法中。我评论说,在system32文件夹下替换了新版本的C#代码,每当我使用谷歌浏览器时,文件转储就会自动发生。无需单独运行C#.exe文件。我花了很长时间,因为这是我第一次使用Chrome Extension开发,我正在寻找如何调试整个事情。
I found it. All the codes I posted above are correct. The problem was inside another method of my C# code. I commented that, replaced the newer build of the C# code under system32 folder and the file dump is happening automatically whenever I am working with Google Chrome. No need to run the C# .exe file separately. It took time for me as it was my first time with Chrome Extension development and I was looking for how to debug the whole thing.


这篇关于通过Native Messaging从Chrome扩展程序接收C#中的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆