Skip to content

Commit

Permalink
Update docs content
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyaulee committed Jun 8, 2024
1 parent 9b351b3 commit dc3952e
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/Pages/01_02_ManifestV3.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Manifest V3 is supported

What is Manifest V3? Read more about manifext V3 [here](https://developer.chrome.com/docs/extensions/develop/migrate/what-is-mv3).
What is Manifest V3? Read more about manifest V3 [here](https://developer.chrome.com/docs/extensions/develop/migrate/what-is-mv3).

At the moment, Chromium based browsers (Chrome & Edge) has support for the manifest V3 specification and manifest V2 is deprecated.
At the moment, Chromium based browsers (Chrome & Edge) have support for the manifest V3 specification and manifest V2 is deprecated.

Firefox's implementation has slight difference with Chromium based browsers in the [manifest format](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json).
Firefox's implementation has slight differences with Chromium based browsers in the [manifest format](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json).

> **Important Note**
>
Expand Down
3 changes: 2 additions & 1 deletion docs/Pages/01_03_ExtensionAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ When you create a new Razor page, add `@inherits BasePage` on top to get access
}
```

Each of the property in the `IWebExtensionsApi` class provides a set of APIs for different functionalities that can be implemented by an extension. These property sometimes include description of additional setup required to use the API.
Each of the properties in the `IWebExtensionsApi` class provides a set of APIs for different functionalities that can be implemented by an extension.
These properties sometimes include description of additional setup required to use the API.

For example, the `Alarms` API shows that it requires manifest permission `alarms`.

Expand Down
5 changes: 3 additions & 2 deletions docs/Pages/01_04_RunningAndDebugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ MockResolvers.Configure(configure =>
});
```

The [WebExtensions.Net page](https://github.com/mingyaulee/WebExtensions.Net) shows more example of configuring the `MockResolvers`. Some APIs have been mocked by default in [WebExtensions.Net `DefaultMockResolver`](https://github.com/mingyaulee/WebExtensions.Net/blob/09eb19608f5310f3d5b1b9b889f8d3378a13eb06/src/WebExtensions.Net/Mock/Resolvers/DefaultMockResolver.cs#L56).
The [WebExtensions.Net page](https://github.com/mingyaulee/WebExtensions.Net) shows more examples of configuring the `MockResolvers`.
Some APIs have been mocked by default in [WebExtensions.Net `DefaultMockResolver`](https://github.com/mingyaulee/WebExtensions.Net/blob/09eb19608f5310f3d5b1b9b889f8d3378a13eb06/src/WebExtensions.Net/Mock/Resolvers/DefaultMockResolver.cs#L56).

> **Important Note**
>
> At the moment, debugging when the application is loaded as an extension in the browser is not possible.
>
> This is because debugging requires a NodeJs debugging proxy launched by the DevServer, which is not available when loaded as extension in the browser.
> This is because debugging requires a NodeJs debugging proxy launched by the DevServer, which is not available when loaded as an extension in the browser.
>
> The best way to debug when loaded as an extension is to use `Console.WriteLine` which writes the logs to the browser console window, accessible from the Developer Tools window (`F12`).
Expand Down
28 changes: 28 additions & 0 deletions docs/Pages/01_05_Publishing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Publishing the extension

1. Run the `dotnet publish` command to publish the extension.
0. The extension output is in the `bin/Release/net8.0/publish/browserextension` directory.
0. The contents of this directory should be used when packing your extension. The contents can also be loaded unpacked the same way as the build output.

## Packing the extension

Typically the extension stores require that you pack your extension when submitting it to the store.
Most of them require just a simple zip format, where the zipped file is renamed with `.crx` extension.

Refer to the guidance for the respective store for more details.

- [Chrome Web Store](https://developer.chrome.com/docs/webstore/publish)
- [Microsoft Edge Add-ons](https://learn.microsoft.com/en-us/microsoft-edge/extensions-chromium/publish/publish-extension)
- [AMO (addons.mozilla.org)](https://extensionworkshop.com/documentation/publish/submitting-an-add-on/)
- [Opera addons](https://dev.opera.com/extensions/publishing-guidelines/)
- [Safari App Store](https://developer.apple.com/documentation/safariservices/safari_web_extensions/distributing_your_safari_web_extension)

## Optimization

Built-in optimization has been done by this package to use Brotli compression by default, removing all the uncompressed files from the publish output to reduce the package size.

If you would like to use AOT compilation, it is best to benchmark the size before and after using AOT compilation as your extension might end up having an increase in size.

You can also enable IL trimming when publishing and disable unused features such as timezone support.

Refer to [Blazor performance best practices](https://learn.microsoft.com/en-us/aspnet/core/blazor/performance?view=aspnetcore-8.0#minimize-app-download-size) documentation page for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
1. Install the latest `Blazor.BrowserExtension` NuGet package to the Blazor project.
0. Add `<BrowserExtensionBootstrap>true</BrowserExtensionBootstrap>` under the `<PropertyGroup>` node in your `.csproj` project file.
0. Build the project.
0. This should automatically setup the project files to be compatible for building into browser extension and the `BrowserExtensionBootstrap` property is removed from the `.csproj` project file.
0. This should automatically set up the project files to be compatible for building into browser extension and the `BrowserExtensionBootstrap` property is removed from the `.csproj` project file.

## Manual Setting Up

You can setup the project manually as well, if for some reason you encounter any problem with the bootstrapping step above.
You can set up the project manually as well, if for some reason you encounter any problem with the bootstrapping step above.

1. Add a new file `manifest.json` under the `wwwroot` directory. An example of minimal `manifest.json` file:
```json
Expand All @@ -34,7 +34,7 @@ You can setup the project manually as well, if for some reason you encounter any
]
}
```
0. Add `@using Blazor.BrowserExtension.Pages` into `_Imports.razor` file.
0. Add `@using Blazor.BrowserExtension.Pages` into the `_Imports.razor` file.
0. In `Pages/Index.razor` replace the first line `@page "/"` with the following lines:
```razor
@page "/index.html"
Expand All @@ -52,7 +52,7 @@ You can setup the project manually as well, if for some reason you encounter any
});
});
```
0. Add the following into `Program.cs` file to wrap the `RootComponents` setup.
0. Add the following into the `Program.cs` file to wrap the `RootComponents` setup.
```csharp
public static async Task Main(string[] args)
{
Expand Down
4 changes: 2 additions & 2 deletions docs/Pages/03_02_BackgroundWorker.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Different browsers have different support for [background script and extension service worker](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/background#browser_support).

Chrome calls it extension service worker in manifest V3 whereas Mozilla calls it background scripts, but both are declared by.the `background` key in `manifest.json`. For simplicity, we will just call them background worker in this page.
Chrome calls it extension service worker in manifest V3 whereas Mozilla calls it background scripts, but both are declared by.the `background` key in `manifest.json`. For simplicity, we will just call them "background worker" in this page.

Background worker can be used to support the complexity in browser extensions. The pattern of content scripts, popup, or any other page communicating with the background worker to get data or instructions resembles a client-server communication.

Expand Down Expand Up @@ -36,7 +36,7 @@ In the manifest, the background worker is declared in the `background` key.

## Mozilla Firefox

The implementation in Firefox requires a small adjustments in the `manifest.json`.
The implementation in Firefox requires a small adjustment in the `manifest.json`.

```json
{
Expand Down
35 changes: 35 additions & 0 deletions docs/Pages/03_03_Messaging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Messaging between the contexts

Communication between the different contexts is a common requirement, the way to achieve this depends on the source and the target context of the message.

## Sending a message

The `Runtime.SendMessage` API can be used from any context to send a message to all other instances except for Content Scripts.

The `Tabs.SendMessage` API can be used from any context to send a message to all the instances in a tab, i.e. Content Scripts and extension iframes.

Any object can be passed when invoking the API, so you can just create a message wrapper to include the metadata for the message, e.g. sender, intended recipient, timestamp, message type, payload, etc.


## Receiving a message

The `Runtime.OnMessage` API can be used from any context to receive a message.

It is essential to be aware that there may be multiple instances receiving the same message, therefore you can use something to filter out noise messages, for example, having a property in the message object to indicate which context the message is intended for.

The pages with listeners will only get the message if the page is active when the message is sent.
However, for Background Worker that listens to `OnMessage` event, it will be activated if it is already idle when the message is sent.


## Continuous Messaging

The contexts can also use the `Runtime.Connect` and `Tabs.Connect` API to create a `Port` that you can use to continuously send messages to the target context.

The recipient context should then use the `Runtime.OnConnect` event to listen to port creation events and respond to messages on the `Port` object.


# Reference

- [Chrome for Developers](https://developer.chrome.com/docs/extensions/develop/concepts/messaging)
- [MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime)
- [Messaging Sample Project](https://github.com/mingyaulee/Blazor.BrowserExtension.Samples/tree/main/Messaging)
2 changes: 1 addition & 1 deletion docs/Pages/NotFound.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 404 Not Found

Page requested is not found
The page requested is not found.

0 comments on commit dc3952e

Please sign in to comment.