0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

爆速でReactでChrome拡張機能を作る

Posted at

ReactでChromeの拡張機能を作る方法です。

Chromeの拡張機能はHTML、JavaScript、CSSで作れますが、JavaScriptよりもReactの方が作りやすいので、やり方をご紹介します。

「ReactでとりあえずChromeの拡張機能を作って動かす」を目的としているので、Reactの解説などは行わないのでご注意ください。

Reactのプロジェクト作成

まずはReactのプロジェクトを作成します。

npm create vite@latest

App.tsxの修正

Reactのプロジェクトが作成されたら、App.tsxを以下のように修正します。

App.tsx
function App() {
  return <button onClick={() => alert(1)}>テスト</button>;
}

export default App;

Reactアプリのビルド

yarn build or npm run buildでReactアプリをビルドします。
すると、distディレクトリが作成されます。

image.png

manifest.jsonの作成

Reactアプリとは別にディレクトリを作成します(ここではchrome-extensionとします)。
そのchrome-extensionの中にmanifest.jsonを作成し、さらにdistディレクトリ以下のファイル全てをコピーします。
chrome-extensionのディレクトリ構成とmanifest.jsonは以下のようになります。

image.png

{
  "name": "Sample",
  "description": "Sample Extension. Show alert!",
  "version": "1.0.0",
  "manifest_version": 3,
  "action": { "default_popup": "index.html" }
}

Chromeで拡張機能を動かす

あとは、Chromeから作成した拡張機能を読み込み(「パッケージ化されていない拡張機能を読み込む」からchrome-extensionのディレクトリを選択)すれば拡張機能が動かせます

image.png

image.png

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?