简体   繁体   English

适用于Google Chrome的C#插件

[英]C# plugin for Google Chrome

Can I communicate to Google Chrome in C#? 我可以用C#与谷歌浏览器沟通吗?

For writing a chrome plugin for example. 例如,用于编写chrome插件。

<Spoilers>

Quick short answer: No, because Extensions require JSON, JavaScript, and HTML. 快速简答:不,因为扩展需要JSON,JavaScript和HTML。

</Spoilers>

Hi Tony, 嗨托尼,
There are multiple templates on the internet that you can download to build a chrome extension project using Visual Studio. Internet上有多个模板,您可以使用Visual Studio下载这些模板来构建chrome扩展项目。

Downloading one of these templates just gives you the files and folders that you need which I call "the setup". 下载其中一个模板只会为您提供所需的文件和文件夹,我称之为“设置”。

That won't let you build a Google extension using C#. 这不会让您使用C#构建Google扩展程序。

Andrey mentioned that there are libraries like Bridge.NET that will compile your code into HTML/JavaScript. Andrey提到有像Bridge.NET这样的库可以将你的代码编译成HTML / JavaScript。 That will help you write HTML and JavaScript using C#. 这将有助于您使用C#编写HTML和JavaScript。 You still need a manifest.json file. 您仍然需要manifest.json文件。

I don't recommend that. 我不推荐。 That library is not designed for Chrome Extensions. 该库不适用于Chrome扩展程序。 Also, you will have to compile the code into JavaScript and store that JavaScript code in a JavaScript file. 此外,您必须将代码编译为JavaScript并将JavaScript代码存储在JavaScript文件中。 I recommend using HTML/JavaScript with no compilers when building your Chrome Extension. 在构建Chrome扩展程序时,我建议使用不带编译器的HTML / JavaScript。

You need to keep in mind that there are 3 main parts in a chrome extension. 您需要记住,Chrome扩展中有3个主要部分。 These are: 这些是:
manifest.json 的manifest.json
A JavaScript file 一个JavaScript文件
HTML file HTML文件

There are multiple steps and it's not hard to build a google chrome extension. 有多个步骤,并不难建立谷歌Chrome扩展。 This link will give you the complete tutorial on developing a chrome extension with detailed explanation. 此链接将为您提供有关开发chrome扩展的完整教程,并提供详细说明。 This tutorial installs a template so that you can develop it in Visual Studio just like I mentioned before. 本教程安装了一个模板,以便您可以像我之前提到的那样在Visual Studio中开发它。

What I have done to address is use Simple Message Host , it will trigger an executable on the local machine that you code in c#, sending stdin messages and listening to stdout messages so you can build this host to use as a bridge, but like I said, it needs to be on your local network at least, and you have to do some editing in the windows registry , so it has its limitations. 我要做的就是使用Simple Message Host ,它将触发你在c#中编码的本地机器上的可执行文件,发送stdin消息并监听stdout消息,这样你就可以构建这个主机用作桥接器,但就像我一样说,它至少需要在你的local network上,你必须在windows registry做一些编辑,所以它有其局限性。

But for the system I am working with, this solution worked perfectly because I have a controlled environment that I can set up all these prerequisites. 但对于我正在使用的系统,这个解决方案工作得很好,因为我有一个受控制的环境,我可以设置所有这些先决条件。

So, just to clarify, what I did here is: 所以,为了澄清,我在这里做的是:

  1. Create a chrome extension with background.js opening up the listener to the website's javascript. 使用background.js创建chrome扩展,打开网站javascript的监听器。

  2. Add a registry in windows registry pointing to the path of the executable. windows registry添加一个指向可执行文件路径的windows registry

  3. Create the executable in C# doing all your logic. 在C#中创建可执行文件,完成所有逻辑。

  4. Send a response from the executable to the extension and then back to the website. 将可执行文件的响应发送到扩展,然后返回到网站。

There are several guides on how to do this, so I won't detail these steps here so I don't replicate it. 关于如何执行此操作有几个指南,因此我不会在此详细介绍这些步骤,因此我不会复制它。

But for the moment, it is the best way to do what you want, if you have control of your environment that is. 但目前,如果您掌控自己的环境,这是做您想要的最佳方式。

So, if your plugin (extension or chrome app) will work on a controlled environment, this is the way to go, otherwise, I would consider something related to ClickOnce perhaps or WebAssembly but that's not fully released yet. 因此,如果您的插件(扩展程序或Chrome应用程序)可以在受控环境中工作,那么这是可行的方法,否则,我会考虑与ClickOnceWebAssembly相关的内容,但尚未完全发布。

Chrome own extension manager supports extensions written in js and html . Chrome自带的扩展程序管理器支持用jshtml编写的扩展程序。

that said, to execute c# code within the extension you have two options: 也就是说,要在扩展程序中执行c#代码,您有两个选择:

  1. Compile c# code to javascript code which then can be added as a normal javascript extension to chrome (take a look at scriptsharp ) c#代码编译为javascript代码,然后将其作为普通的javascript扩展添加到chrome(请查看scriptsharp
  2. Use c# as a back-end system. 使用c#作为后端系统。 just like most of download managers : 就像大多数download managers

for case 2 you need ac# application installed in client device(or in the cloud) listing to a specific port (using httplistener or self hosted webapi (you can use netcore) which do these steps 对于案例2,您需要在客户端设备(或云端)中安装ac#应用程序列表到特定端口(使用httplistenerself hosted webapi (you can use netcore)执行这些步骤

  1. Listing to incoming requests 列出传入的请求
  2. parse request data eg. 解析请求数据,例如。 json and do something with it json并用它做点什么
  3. return the result to javascript extension which can display it to user or do other things with it. 将结果返回到javascript扩展名,该扩展名可以将其显示给用户或使用它执行其他操作。

The topic is quite old, but I'd like to share that sample: https://github.com/Retyped/Demos/tree/master/ChromeDemo 这个主题很老了,但我想分享一下这个样本: https//github.com/Retyped/Demos/tree/master/ChromeDemo

In a few words, the sample is implemented in C#. 简而言之,样本是用C#实现的。 The Retyped.chrome NuGet package provides bindings (Chrome API) for Bridge.NET projects . Retyped.chrome NuGet包为Bridge.NET项目提供绑定(Chrome API)。 So yes, now you can implement your logic in C#, then C# code will be transparently compiled into JavaScript by Bridge.NET compiler. 所以,是的,现在您可以在C#中实现您的逻辑,然后C#代码将通过Bridge.NET编译器透明地编译为JavaScript。

With that approach you can build your Chrome extension on top of .NET Framework as well as utilize thousands of JavaScript libraries . 通过这种方法,您可以在.NET Framework上构建Chrome扩展,并使用数千个JavaScript库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM