Skip to content

Commit 38a883d

Browse files
authored
Add browser extension template (huggingface#23)
* [WIP] Browser extension template * Prevent concurrent WebGPU EP execution Needed since WebGPU EP only supports one session execution at a time * Update comments * Update webpack.config.js * Use q4 model by default
1 parent 8ced4dd commit 38a883d

File tree

14 files changed

+3609
-0
lines changed

14 files changed

+3609
-0
lines changed

browser-extension/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

browser-extension/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Transformers.js - Sample browser extension
2+
3+
An example project to show how to run 🤗 Transformers in a browser extension. Although we only provide instructions for running in Chrome, it should be similar for other browsers.
4+
5+
## Getting Started
6+
7+
1. Clone the repo and enter the project directory:
8+
```bash
9+
git clone https://github.com/huggingface/transformers.js-examples.git
10+
cd transformers.js-examples/browser-extension/
11+
```
12+
1. Install the necessary dependencies:
13+
14+
```bash
15+
npm install
16+
```
17+
18+
1. Build the project:
19+
20+
```bash
21+
npm run build
22+
```
23+
24+
1. Add the extension to your browser. To do this, go to `chrome://extensions/`, enable developer mode (top right), and click "Load unpacked". Select the `build` directory from the dialog which appears and click "Select Folder".
25+
26+
1. That's it! You should now be able to open the extension's popup and use the model in your browser!
27+
28+
## Editing the template
29+
30+
We recommend running `npm run dev` while editing the template as it will rebuild the project when changes are made.
31+
32+
All source code can be found in the `./src/` directory:
33+
34+
- `background.js` ([service worker](https://developer.chrome.com/docs/extensions/mv3/service_workers/)) - handles all the requests from the UI, does processing in the background, then returns the result. You will need to reload the extension (by visiting `chrome://extensions/` and clicking the refresh button) after editing this file for changes to be visible in the extension.
35+
36+
- `content.js` ([content script](https://developer.chrome.com/docs/extensions/mv3/content_scripts/)) - contains the code which is injected into every page the user visits. You can use the `sendMessage` api to make requests to the background script. Similarly, you will need to reload the extension after editing this file for changes to be visible in the extension.
37+
38+
- `popup.html`, `popup.css`, `popup.js` ([toolbar action](https://developer.chrome.com/docs/extensions/reference/action/)) - contains the code for the popup which is visible to the user when they click the extension's icon from the extensions bar. For development, we recommend opening the `popup.html` file in its own tab by visiting `chrome-extension://<ext_id>/popup.html` (remember to replace `<ext_id>` with the extension's ID). You will need to refresh the page while you develop to see the changes you make.

browser-extension/build/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Running `npm run build` will build the project and output the files here.
2+
*
3+
!.gitignore

0 commit comments

Comments
 (0)