简体   繁体   中英

C# plugin for Google Chrome

Can I communicate to Google Chrome in C#?

For writing a chrome plugin for example.

<Spoilers>

Quick short answer: No, because Extensions require JSON, JavaScript, and HTML.

</Spoilers>

Hi Tony,
There are multiple templates on the internet that you can download to build a chrome extension project using Visual Studio.

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#.

Andrey mentioned that there are libraries like Bridge.NET that will compile your code into HTML/JavaScript. That will help you write HTML and JavaScript using C#. You still need a manifest.json file.

I don't recommend that. That library is not designed for Chrome Extensions. Also, you will have to compile the code into JavaScript and store that JavaScript code in a JavaScript file. I recommend using HTML/JavaScript with no compilers when building your Chrome Extension.

You need to keep in mind that there are 3 main parts in a chrome extension. These are:
manifest.json
A JavaScript file
HTML file

There are multiple steps and it's not hard to build a google chrome extension. This link will give you the complete tutorial on developing a chrome extension with detailed explanation. This tutorial installs a template so that you can develop it in Visual Studio just like I mentioned before.

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.

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.

  2. Add a registry in windows registry pointing to the path of the executable.

  3. Create the executable in C# doing all your logic.

  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 own extension manager supports extensions written in js and html .

that said, to execute c# code within the extension you have two options:

  1. Compile c# code to javascript code which then can be added as a normal javascript extension to chrome (take a look at scriptsharp )
  2. Use c# as a back-end system. just like most of 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

  1. Listing to incoming requests
  2. parse request data eg. json and do something with it
  3. return the result to javascript extension which can display it to user or do other things with it.

The topic is quite old, but I'd like to share that sample: https://github.com/Retyped/Demos/tree/master/ChromeDemo

In a few words, the sample is implemented in C#. The Retyped.chrome NuGet package provides bindings (Chrome API) for Bridge.NET projects . So yes, now you can implement your logic in C#, then C# code will be transparently compiled into JavaScript by Bridge.NET compiler.

With that approach you can build your Chrome extension on top of .NET Framework as well as utilize thousands of JavaScript libraries .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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