12

Can I communicate to Google Chrome in C#?

For writing a chrome plugin for example.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415

4 Answers4

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

Mohammed
  • 396
  • 4
  • 15
  • this is neither related to your answer nor about question, just want to ask this chrome template described in this article only work for VS2013/15. I tried installing this template on my machine having **only** visual studio 2017,but it is failing? is this extension only build for visual studio 2015? – Prasad Telkikar Nov 19 '17 at 13:28
  • I am facing similar kind of error while installing chrome extension template on VS2017. So as per my point of view this extension only supports version VS15 , 13 and so on https://stackoverflow.com/questions/41215916/vsixinstaller-noapplicableskusexception-this-extension-is-not-installable-on-an – Prasad Telkikar Nov 19 '17 at 13:31
  • 5
    Did you accidentally add a bounty to the Question? – Jeremy Thompson Nov 20 '17 at 03:47
3

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.

Tiskolin
  • 167
  • 17
Zorkind
  • 607
  • 12
  • 21
0

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 a c# 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.
Abdo
  • 322
  • 6
  • 15
-1

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.

AndreyCh
  • 1,298
  • 1
  • 14
  • 16