Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
867877a
Render React component in content script
AndrewJudson Jan 26, 2023
8ab639b
Turn off newtab
AndrewJudson Jan 26, 2023
43d3278
Call gpt3
AndrewJudson Jan 26, 2023
7184705
Select key on options page, use chrome storage
AndrewJudson Jan 26, 2023
8983cb0
Remove user setting API key from content script
AndrewJudson Jan 26, 2023
de552ce
Add test note to Anki
AndrewJudson Jan 26, 2023
450420b
Set Anki API key
AndrewJudson Jan 27, 2023
96aa9c2
Switch websocket to fix https issue
AndrewJudson Jan 27, 2023
28da578
Show an error if not https
AndrewJudson Jan 27, 2023
6b59b67
Improve CSS
AndrewJudson Jan 27, 2023
ac14dbb
Close and open extension with popup button
AndrewJudson Jan 27, 2023
2866b32
Add prompt template and result parsing, set Anki deck
AndrewJudson Jan 30, 2023
fde9dda
Add README
AndrewJudson Jan 30, 2023
48bd10b
Change icons and description
AndrewJudson Jan 30, 2023
4e66623
Add close button
AndrewJudson Jan 30, 2023
5c51994
Close and disable buttons
AndrewJudson Jan 30, 2023
137d556
Add word size slider
AndrewJudson Jan 31, 2023
425bbfa
Add tailwind
AndrewJudson Jan 31, 2023
ac5cda8
Add Storybook
AndrewJudson Jan 31, 2023
ce32632
Make Options page nicer
AndrewJudson Jan 31, 2023
0ab67fa
Isolate content CSS from parent page (both directions)
AndrewJudson Feb 1, 2023
2b04181
Fix z-index on content container
AndrewJudson Feb 1, 2023
cf6d01f
Storybook for container
AndrewJudson Feb 1, 2023
8c9f3b9
Move content to components
AndrewJudson Feb 1, 2023
259440c
Set up storybook for content
AndrewJudson Feb 1, 2023
b3b3911
Some styling changes
AndrewJudson Feb 2, 2023
844e350
Add clear ability to options
AndrewJudson Feb 2, 2023
f5447da
More styling
AndrewJudson Feb 2, 2023
3882430
Link video demo tweet
AndrewJudson Feb 2, 2023
9d0c4cc
Temporarily remove livereload in dev, more build instructions
AndrewJudson Feb 2, 2023
9033544
Add better ankiconnect instructions
AndrewJudson Feb 2, 2023
9db6b76
Escape key to close window
AndrewJudson Feb 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
legacy-peer-deps=true
15 changes: 15 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.@(js|jsx|ts|tsx)",
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions"
],
"framework": "@storybook/react",
"core": {
"builder": "@storybook/builder-webpack5"
}
}
11 changes: 11 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "../src/assets/styles/tailwind.css";

export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}
160 changes: 18 additions & 142 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,150 +1,26 @@
<img src="src/assets/img/icon-128.png" width="64"/>
This is a Chrome extension that allows you to quickly generate question/answer pairs via GPT-3 and add them to your Anki deck while browsing the web.

# Chrome Extension (MV3) Boilerplate with React 18 and Webpack 5
To use it:

[![npm](https://img.shields.io/npm/v/chrome-extension-boilerplate-react)](https://www.npmjs.com/package/chrome-extension-boilerplate-react)
[![npm-download](https://img.shields.io/npm/dw/chrome-extension-boilerplate-react)](https://www.npmjs.com/package/chrome-extension-boilerplate-react)
[![npm](https://img.shields.io/npm/dm/chrome-extension-boilerplate-react)](https://www.npmjs.com/package/chrome-extension-boilerplate-react)
Before installing extension:
1. Install and configure anki-connect plugin for your desktop Anki client (ankiweb does not provide API access so this is necessary to programmatically insert cards).
2. Add ankiconnect API key [ankiconnect](/ankiconnect.png)
1. Go to Add Ons Page in Anki
2. Click on AnkiConnect
3. It will pop up a JSON that you can edit. Insert your secret key (something you make up), and then use that same key you made up in the chrome extension. The reason for setting this is because the extension will be talking to your Anki instance, and if we don't set a key than any random website that decides to call localhost at that port will be able to interact with your deck which you don't want.
3. Obtain an OpenAI API Key if you do not already have one

## Announcements
Installing extension:
1. npm install
2. npm run dev (you can cancel after the build folder is generated)
3. go to chrome://extensions, click "load unpacked" button and use the build folder from this repo as the directory to use.
4. Set the OpenAI key, anki-connect API key, and name of your anki deck in the options page of the Chrome extension (right click on options picture, or see the "Settings" button when you bring it up by selecting text - click on the messages to fill them in)

- Recently updated from **[React](https://reactjs.org)** ~~17~~ to **18**!
- **_This boilerplate adopts [Manifest V3](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-overview/)!_**
- For V2 users, please check out the [manifest-v2](https://github.com/lxieyang/chrome-extension-boilerplate-react/tree/manifest-v2) branch, or use version [3.x](https://www.npmjs.com/package/chrome-extension-boilerplate-react/v/3.3.0).
- Check out the [Manifest V3 Migration Guide](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/).
- Recently added [devtools](https://developer.chrome.com/docs/extensions/mv3/devtools/) Support! Thanks [GeekaholicLin](https://github.com/lxieyang/chrome-extension-boilerplate-react/issues/17)!
- Recently updated from **[Webpack Dev Server](https://webpack.js.org/configuration/dev-server/)** ~~3.x~~ to **4.x** and **[Webpack](https://webpack.js.org/)** ~~4~~ to **5**!
- Recently added [TypeScript](https://www.typescriptlang.org/) Support!
There was a problem with the production webpack build that I couldn't resolve, so I am just using the dev version for now. I will aim to fix it at some point

## Features
Happy prompt making!

This is a basic Chrome Extensions boilerplate to help you write modular and modern Javascript code, load CSS easily and [automatic reload the browser on code changes](https://webpack.github.io/docs/webpack-dev-server.html#automatic-refresh).
Video of it in action: https://twitter.com/i/status/1621025729013239813

This boilerplate is updated with:

- [Chrome Extension Manifest V3](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-overview/)
- [React 18](https://reactjs.org)
- [Webpack 5](https://webpack.js.org/)
- [Webpack Dev Server 4](https://webpack.js.org/configuration/dev-server/)
- [React Refresh](https://www.npmjs.com/package/react-refresh)
- [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin)
- [eslint-config-react-app](https://www.npmjs.com/package/eslint-config-react-app)
- [Prettier](https://prettier.io/)
- [TypeScript](https://www.typescriptlang.org/)

This boilerplate is heavily inspired by and adapted from [https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate](https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate), with additional support for React 18 features, Webpack 5, and Webpack Dev Server 4.

Please open up an issue to nudge me to keep the npm packages up-to-date. FYI, it takes time to make different packages with different versions work together nicely.

## Installing and Running

### Procedures:

1. Check if your [Node.js](https://nodejs.org/) version is >= **18**.
2. Clone this repository.
3. Change the package's `name`, `description`, and `repository` fields in `package.json`.
4. Change the name of your extension on `src/manifest.json`.
5. Run `npm install` to install the dependencies.
6. Run `npm start`
7. Load your extension on Chrome following:
1. Access `chrome://extensions/`
2. Check `Developer mode`
3. Click on `Load unpacked extension`
4. Select the `build` folder.
8. Happy hacking.

## Structure

All your extension's code must be placed in the `src` folder.

The boilerplate is already prepared to have a popup, an options page, a background page, and a new tab page (which replaces the new tab page of your browser). But feel free to customize these.

## TypeScript

This boilerplate now supports TypeScript! The `Options` Page is implemented using TypeScript. Please refer to `src/pages/Options/` for example usages.

## Webpack auto-reload and HRM

To make your workflow much more efficient this boilerplate uses the [webpack server](https://webpack.github.io/docs/webpack-dev-server.html) to development (started with `npm start`) with auto reload feature that reloads the browser automatically every time that you save some file in your editor.

You can run the dev mode on other port if you want. Just specify the env var `port` like this:

```
$ PORT=6002 npm run start
```

## Content Scripts

Although this boilerplate uses the webpack dev server, it's also prepared to write all your bundles files on the disk at every code change, so you can point, on your extension manifest, to your bundles that you want to use as [content scripts](https://developer.chrome.com/extensions/content_scripts), but you need to exclude these entry points from hot reloading [(why?)](https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate/issues/4#issuecomment-261788690). To do so you need to expose which entry points are content scripts on the `webpack.config.js` using the `chromeExtensionBoilerplate -> notHotReload` config. Look the example below.

Let's say that you want use the `myContentScript` entry point as content script, so on your `webpack.config.js` you will configure the entry point and exclude it from hot reloading, like this:

```js
{
entry: {
myContentScript: "./src/js/myContentScript.js"
},
chromeExtensionBoilerplate: {
notHotReload: ["myContentScript"]
}
}
```

and on your `src/manifest.json`:

```json
{
"content_scripts": [
{
"matches": ["https://www.google.com/*"],
"js": ["myContentScript.bundle.js"]
}
]
}
```

## Intelligent Code Completion

Thanks to [@hudidit](https://github.com/lxieyang/chrome-extension-boilerplate-react/issues/4)'s kind suggestions, this boilerplate supports chrome-specific intelligent code completion using [@types/chrome](https://www.npmjs.com/package/@types/chrome).

## Packing

After the development of your extension run the command

```
$ NODE_ENV=production npm run build
```

Now, the content of `build` folder will be the extension ready to be submitted to the Chrome Web Store. Just take a look at the [official guide](https://developer.chrome.com/webstore/publish) to more infos about publishing.

## Secrets

If you are developing an extension that talks with some API you probably are using different keys for testing and production. Is a good practice you not commit your secret keys and expose to anyone that have access to the repository.

To this task this boilerplate import the file `./secrets.<THE-NODE_ENV>.js` on your modules through the module named as `secrets`, so you can do things like this:

_./secrets.development.js_

```js
export default { key: '123' };
```

_./src/popup.js_

```js
import secrets from 'secrets';
ApiCall({ key: secrets.key });
```

:point_right: The files with name `secrets.*.js` already are ignored on the repository.

## Resources:

- [Webpack documentation](https://webpack.js.org/concepts/)
- [Chrome Extension documentation](https://developer.chrome.com/extensions/getstarted)

---

Michael Xieyang Liu | [Website](https://lxieyang.github.io)
This uses the React Chrome Extension template by Michael Xieyang Liu | [Website](https://lxieyang.github.io) and code from https://github.com/yewsiang/tailwind-react-chrome-extension-template to make Tailwind work
Binary file added ankiconnect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading