Giter Site home page Giter Site logo

vasilyshelkov / create-react-extension Goto Github PK

View Code? Open in Web Editor NEW

This project forked from facebook/create-react-app

343.0 11.0 37.0 13.37 MB

Set up a modern React browser extension by running one command.

License: MIT License

JavaScript 98.10% CSS 0.16% AppleScript 0.09% HTML 0.21% TypeScript 0.14% Shell 1.28% Sass 0.01% SCSS 0.02%

create-react-extension's Introduction

Create-React-Extension PRs Welcome

Create React browser extensions with no build configuration.

This is a fork of create-react-app to make creating Browser Extensions in React more accessible and following Create-React-App's(CRA) philosophy, especially convention over configuration.

Features

see Create a browser extension for a more in depth start guide see CRA user guide for more information on different configurations and functionality available.

If something doesn’t work, please file an issue.

Quick Overview

npx create-react-app my-browser-extension --scripts-version react-browser-extension-scripts --template browser-extension
cd my-browser-extension
npm start

If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app to ensure that npx always uses the latest version.

(npx comes with npm 5.2+ and higher, see instructions for older npm versions)

Then open Chrome, and unpack the newly created /dev folder to see your extension added locally to your browser.
When you’re ready to ship your extension, create an optimized build with npm run build.

npm start

What do I need to know to start building my extension ?

It follows mostly the same conventions as CRA.

Only 1 new convention

There are different types of chrome extensions which can be any combination of:

  • Popup UI which renders your index.js when you click on your extension in the browser extension icon.
  • Background script which will run in the background from /background/index.js and can be use for things like state-management.
  • Content script from /contentScript/index.js which will run on configured web pages
  • Options UI which renders your /options/ when you click on your extension in the browser extension icon.
  • Dev tools page ❎ Does not support yet

These are all controlled by the all important /public/manifest.json which is configurable by you to control what kind of extension you want build.

Do not delete any of the entry files, this is a convention to remind you what your extensions could be. The build will notify you and fail if you remove any of these important files.

Creating a Browser Extension

You’ll need to have Node 10.16.0 or later version on your local development machine (but it’s not required on the server). We recommend using the latest LTS version. You can use nvm (macOS/Linux) or nvm-windows to switch Node versions between different projects.

To create a new browser extension, you may choose one of the following methods:

npx

npx create-react-app my-browser-extension --scripts-version react-browser-extension-scripts --template browser-extension

(npx is a package runner tool that comes with npm 5.2+ and higher, see instructions for older npm versions)

npm

npm init react-app my-browser-extension --scripts-version react-browser-extension-scripts --template browser-extension

npm init <initializer> is available in npm 6+

Yarn

yarn create react-app my-browser-extension --scripts-version react-browser-extension-scripts --template browser-extension

yarn create <starter-kit-package> is available in Yarn 0.25+

It will create a directory called my-browser-extension inside the current folder.
Inside that directory, it will generate the initial project structure and install the transitive dependencies:

my-browser-extension
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│   ├── img
│   │   ├── icon-16.png
│   │   ├── icon-48.png
│   │   ├── icon-128.png
│   ├── popup.html
│   ├── options.html
│   └── manifest.json
└── src
    ├── background
    │   ├── index.js
    ├── contentScripts
    │   ├── index.js
    ├── options
    │   ├── index.js
    │   ├── Options.js
    ├── App.css
    ├── App.js
    ├── App.test.js
    ├── index.css
    ├── index.js
    ├── logo.svg

No configuration or complicated folder structures, only the files you need to build your extension.
Once the installation is done, you can open your project folder:

cd my-browser-extension

Inside the newly created project, you can run some built-in commands:

npm start or yarn start

Runs the browser extension in development mode. You will see a /dev folder has been created in your project with the extension.

To view the extension in your browser, open up chrome and unpack extension.

The extension will automatically reload if you make changes to the code.
You will see the build errors and lint warnings in the console.

Build errors

npm test or yarn test

Runs the test watcher in an interactive mode.
By default, runs tests related to files changed since the last commit.

Read more about testing.

npm run build or yarn build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.

Your app is ready to be deployed.

User Guide

You can find detailed instructions on using Create React App and many tips in its documentation.

How to Update to New Versions?

Please refer to the User Guide for this and other information.

Philosophy

  • One Dependency: There is only one build dependency. It uses webpack, Babel, ESLint, and other amazing projects, but provides a cohesive curated experience on top of them.

  • No Configuration Required: You don't need to configure anything. A reasonably good configuration of both development and production builds is handled for you so you can focus on writing code.

  • No Lock-In: You can “eject” to a custom setup at any time. Run a single command, and all the configuration and build dependencies will be moved directly into your project, so you can pick up right where you left off.

What’s Included?

Your environment will have everything you need to build a modern single-page React app:

  • React, JSX, ES6, TypeScript and Flow syntax support.
  • Language extras beyond ES6 like the object spread operator.
  • Autoprefixed CSS, so you don’t need -webkit- or other prefixes.
  • A fast interactive unit test runner with built-in support for coverage reporting.
  • A live development server that warns about common mistakes.
  • A build script to bundle JS, CSS, and images for production, with hashes and sourcemaps.
  • An offline-first service worker and a web app manifest, meeting all the Progressive Web App criteria. (Note: Using the service worker is opt-in as of [email protected] and higher)
  • Hassle-free updates for the above tools with a single dependency.

Check out this guide for an overview of how these tools fit together.

The tradeoff is that these tools are preconfigured to work in a specific way. If your project needs more customization, you can "eject" and customize it, but then you will need to maintain this configuration.

Check out this awesome browser extensions list for more great packages to help you create a great web extension

Contributing

We'd love to have your helping hand on create-react-extension! See CONTRIBUTING.md for more information on what we're looking for and how to get started.

Acknowledgements

We are grateful to everyone who has been a part of the following existing related projects for their ideas and collaboration:

License

Create React Extension is open source software licensed as MIT.

create-react-extension's People

Contributors

amyrlam avatar andriijas avatar arcanis avatar bondz avatar bradfordlemley avatar brunolemos avatar enoahnetzach avatar existentialism avatar fson avatar gaearon avatar greengremlin avatar heyimalex avatar iamandrewluca avatar ianschmitz avatar iansu avatar jeffposnick avatar michaeldeboey avatar mrmckeb avatar mxstbr avatar n3tr avatar ro-savage avatar ryansully avatar selbekk avatar sidoshi avatar tharakawj avatar timer avatar tuchk4 avatar viankakrisna avatar vjeux avatar xiaoxiangmoe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

create-react-extension's Issues

Improve init.js script

Adapt to be more specific to Create Browser Extension. This will help new users understand how the create-react-extension works better

How to use with newtab.html?

My use case is a new tab. In public/manifest.json I want to be able to add public/newtab.html so that:

"chrome_url_overrides": {
    "newtab": "newtab.html"
},

and it to magically reload every time I make a change to src/App.js. It works if I change manifest.json to

"chrome_url_overrides": {
    "newtab": "popup.html"
},

but I'd like the ability to have more than one file being watched/served, or to customise the one that is being watched/served.

Thanks

How to develop UI Options Page?

That is a great library for fast creates chrome extension. But there is one thing which confuses me a little bit. Maybe somebody of you have a smart idea to solve that.

Which approach is the best and how can I develop options page for the extension not having cra support?

"chrome is not defined"

When I add any reference to chrome, e.g. "chrome.cookies.getAll(...", npm start displays the error, "chrome is not defined". How do I fix that?

For example, without making any modifications to the app as it is generated and then dropping the following code into the background/index.js file causes this error:

chrome.cookies.getAll({
domain: ".youtube.com"
}, function (cookies) {
for (var i = 0; i < cookies.length; i++) {
console.log(cookies[i] + "deleted");
chrome.cookies.remove({
url: "https://" + cookies[i].domain + cookies[i].path,
name: cookies[i].name
});
}
});

Add some configuration options

First if all. I love your work. I was just amazed how easy the extension was created.

The only thing i am missing is some configuration options regarding the location of the files.
I would suggest to add an. env file in which you can specify the location of the options/popup/... location.

I had a quick look at thr source code and it looks like you just have to modify the packages/react-scripts/config/paths.js. I would like to create a PR in which some configuration options for the location het added.

Manifest V3

Hello, I'm using this extension and am very happy with it, is there any chance of supporting Manifest V3?

Make manifest V3 the new default setup

Manifest V2 is being phased out

As many of you are aware, Google is doing away with manifest V2. Whether most of us disagree with their decision is irrelevant, and they have already stated that in 2023 manifest V2 extensions will not be allowed into the Chrome Web Store, or work in the Chrome browser.

I propose making the changes to the manifest.json file, and any other files that need to updated, the standard when creating a new project with this tool.

Relevant Issues:
#36

Tailwind css is not working

Thank you everyone for creating this wonderful project. Am starting to create a chrome extension for my side project. I am using tailwind css for my pophtml, which is inside App.js. But it is not rendering properly. Can anyone let me know if am missing anything in the setup or configuration?

Following are the changes i made in the respective file.

App.js

import "tailwindcss/tailwind.css";

tailwind.config.js

module.exports = {
  content: ["./src/**/*.{js,jsx,ts,tsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
};

package.json

{
  "name": "nudgeme",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.3",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-browser-extension-scripts": "4.0.10",
    "react-dom": "^17.0.2",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "autoprefixer": "^10.4.4",
    "postcss": "^8.4.12",
    "tailwindcss": "^3.0.23"
  }
}

postcss.config.js

module.exports = {
  plugins: [
    require("postcss-import"),
    require("tailwindcss"),
    require("autoprefixer"),
  ],
};

Cannot import scripts in background script with importScripts (service worker)

As you can see here,

image

using self.importScripts('../pathtojavascriptfile.js') is broken. I'm not sure if this is a webpack issue,
but using this normally in a background.js script without create-react-extension works.

Does anyone have a possible fix for this?

Edit

The reason this is so important is because in MV3 with the limitation of service workers, you cannot import files the way you would in another javascript or react file, you must use self.importScripts() to import local files.

This is the only way for services like Firebase to be integrated into your extension.
And this is currently impossible because of this bug.

How to auto bundle the HTML file containing chrome.tabs.create?

I'm trying to do chrome.tabs.create but don't know where to put it. I tried the code below in a Welcome.html page but I don't know where to put the page so it is automatically bundled. I've tried "public", etc. The only way it works is if I manually copy the Welcome.html page into the "dev" dir.

Or maybe there's some way to do this without an HTML page? E.g., chrome.tabs.create({url: 'Magic.js'}); ??

background.js -
chrome.runtime.onInstalled.addListener(() => {
console.log('Chrome extension successfully installed!');
chrome.tabs.create({
url: 'Welcome.html'
});
return;
});

After ejecting, can't run start server

Describe the bug

After ejecting, if I try to run yarn start, it fails with the following error:

Cannot find module './utils/copyPublicFolder'

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

This is an issue with create-react-extension

Environment

Environment Info:

  Binaries:
    Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v14.15.4/bin/yarn
    npm: 6.14.10 - ~/.nvm/versions/node/v14.15.4/bin/npm
  Browsers:
    Chrome: Not Found
    Firefox: 86.0
  npmPackages:
    react: ^17.0.1 => 17.0.1 
    react-dom: ^17.0.1 => 17.0.1 
    react-scripts: Not Found
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

(Write your steps here:)

  1. yarn create react-app my-browser-extension --scripts-version react-browser-extension-scripts --template browser-extension
  2. yarn eject
  3. yarn start

Expected behavior

I expected yarn start to start the server.

Actual behavior

yarn start failed with error:

yarn run v1.22.10
$ node scripts/start.js
Cannot find module './utils/copyPublicFolder'
Require stack:
- /path/to/project/browser-ext/scripts/start.js
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Reproducible demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

Using typescript with CRA-chrome-extensions

It's a bit unclear how to configure this fork of CRA with typescript. Is there any documentation or examples of how to set this up for chrome extensions? Also, really appreciate the work you're doing here!

Is there anyway to build one more page like options?

Hi, currently my extensions have a page called dashboard.html using react for render, I develop and build by changing the original options folder to options_default and create new folders named options but inside this folder is the dashboard site, so whenever I build or develop I need to change the name of the folder to options so it can work, is there any tips for easier than this way? thanks

Name of extension isn't adhere pakcage.json name

Running this command

yarn create react-app test-extension --scripts-version react-browser-extension-scripts --template browser-extensionyarn create react-app my-browser-extension --scripts-version react-browser-extension-scripts --template browser-extension

I'm expecting this to test extension instead of Create React App Sample.

Allow deleting unused files

Is your proposal related to a problem?

Do not delete any of the entry files, this is a convention to remind you what your extensions could be. The build will notify you and fail if you remove any of these important files.

That reminder doesn't feel useful to me, and the resulting clutter is distracting to me, and it'd be much more satisfying to have a tidy repository.

My extension only needs to be a content script, and is very unlikely to ever need the other capabilities. If I ever did want to add one, though, I could simply copy those files over from a fresh install.

Describe the solution you'd like

Allow the files to be deleted without the build failing. I don't think that violates "convention over configuration", because the files will still all exist in the exact same locations if they're actually used.

hot reload broken in v3

@VasilyShelkov , I migrated the extension to v3. After migration, hot reload of background js script is broken.
When I change background.js file, I see this error in the service_worker inspect view.

background.bundle.js:10849 Uncaught TypeError: self.location.reload is not a function
    at Object.contentChanged [as content-changed] (background.bundle.js:10849)
    at background.bundle.js:11092
    at WebSocket.<anonymous> (background.bundle.js:10704)
contentChanged @ background.bundle.js:10849
(anonymous) @ background.bundle.js:11092
(anonymous) @ background.bundle.js:10704

My manifest file

  "manifest_version": 3,
  "version": "0.0.1",
  "short_name": "React App",
  "name": "Same React App",
  "description": "Sample application showing how to create an extension with Create React App",
  "action": {
    "default_title": "CRE",
    "default_popup": "popup.html"
  },
  "background": {
    "service_worker": "background.bundle.js"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["contentScript.bundle.js"]
    }
  ],
  "icons": {
    "16": "img/icon-16.png",
    "48": "img/icon-48.png",
    "128": "img/icon-128.png"
  },
  "options_page": "options.html",
  "permissions": ["tabs"],
  "content_security_policy": {
    "extension_pages": "script-src 'self'; object-src 'self'"
  }
}

I have tried deleting dev folder too. Any suggestions?

Originally posted by @rashid301 in #19 (comment)

Multiple content scripts

Would it be possible to create multiple content scripts?

The use case is needing different permissions on different scripts e.g. one content script is used for post-installation, another for logging a user in, another for the general functionality of the page.

Happy to raise a PR, with some direction

Using Content Script to Update DOM on active tab

I am attempting to use the built-in content script to update the dom on the active tab I am currently viewing. The issue I am running into is that the content scripts are updating the extension itself instead of the active Tab's DOM. Is there a specific way to use the content scripts so that I can inject JS into the active tab? Or are there any resources I can take a look at.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.