Writing chrome extension in C#?

Viewed 21581

Today I started looking at the Chrome extensions the first time ever, I have a very silly questions, I am sure the answer is NO to this as per google search but I just wanted to make sure from the community here.

  1. Is it possible to use C# to write code instead of javascript?
  2. Is it possible to use Partial Views (ASP.NET MVC) in chrome extension as it renders HTML?
  3. I found this in VS Marketplace https://marketplace.visualstudio.com/items?itemName=MadsKristensen.GoogleChromeExtensionProjectTemplate Is there any other templates which have bootstrap etc

Cheers

7 Answers

You can create browser extension with C#. Specifically, Using Client-side Blazor.

To publish, the following operations are required.

First, you publish like normal standalone app. https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/blazor/webassembly?view=aspnetcore-3.1#standalone-deployment

Then, Remove the underscore from directory name. Since it cannot be used in the extension.

# remove underbar from directory name
mv _framework/_bin _framework/bin
mv _framework framework
# rewrite
sed 's/_bin/bin/g' framework/blazor.webassembly.js
sed 's/_framework/framework/g' framework/blazor.webassembly.js index.html

Then, Add manifest.json and setting CSP like this,

"content_security_policy": "script-src 'self' 'unsafe-eval' 'sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA='; object-src 'self'",

Blazor boot script add script tag to html, so you should add scripts hash to allow execute bootup script.

This is sample app I created. https://github.com/key-moon/WeatherForecastExtensionWithBlazor And, This is commentary (wrote in Japanese). https://qiita.com/keymoon/items/03357e58eddf75871527

Chrome Extension runs in the browser so you can not use C# in Chrome Extension Development.

Again the Chrome extension runs in the browser so you can not use ASP.NET MVC in Chrome Extension Development, but you can use ASP.NET MVC or any other language at server to generate the views and render them in the chrome extension using ajax.

Have a look at this: https://github.com/Ehesp/Chrome-Extension-Twitter-Bootstrap-3-Template

You can definitely create a Chrome Extension (fairly) easily using C# now with the help of Blazor WebAssembly.

If you are still interested in trying out or if anyone else is interested, I have created a package in the repository Blazor.BrowserExtension. Just follow the steps in the link to create the project.

With this package, you get the things that you need in an extension. Currently it supports Background, Popup (Browser Action), Options page and even Content Scripts (extra setup required). Just build the project and then you can load the unpacked extension from the output of the build.

Chrome Extensions API are accessible in Razor pages or from dependency injection. The APIs are imported from another package WebExtension.Net.

Yes you can write Chrome extension in C#, and it's easy.

I tried the below. It was fast and works right away:

  • Run dotnet new --install Blazor.BrowserExtension.Template.
  • Run dotnet new browserext --name to initialize a new project with the template.
  • Change the working directory into the newly created project directory.
  • Run dotnet build to build the project.

On Google Chrome:

  • Launch the Extensions page ( ⋮ → More tools → Extensions).
  • Switch on Developer mode.
  • Click on the Load unpacked button, then navigate to %ProjectDir%\bin\Debug\net5.0\ and select the foler browserextension.

Source: https://github.com/mingyaulee/Blazor.BrowserExtension

Thank you very much to "mingyaulee" for such an awesome work and sharing it.

Technically, you can use a C# binary as a Native Host that talks to the JavaScript portion in the browser.

However, that would complicate your extension distribution, as you can't bundle the two to CWS; the native host component has to be separately installed.

This should only be used when you have legitimate reason to do something that's impossible within the browser sandbox.

No chrome extensions have to distributed in JavaScript.

So technically, you can write in anything you can compile to JavaScript, like Dart or TypeScript.

See here for a brief introduction: https://developer.chrome.com/extensions

Using Blazor and write it as normal html/css maybe? You still need to build blazor project though but I think it count as technically possible to write extension in C#

Related