GithubHelp home page GithubHelp logo

lxieyang / chrome-extension-boilerplate-react Goto Github PK

View Code? Open in Web Editor NEW
3.2K 3.2K 1.0K 1.74 MB

A Chrome Extensions boilerplate using React 18 and Webpack 5.

License: MIT License

JavaScript 75.62% HTML 7.31% CSS 13.93% SCSS 0.41% TypeScript 2.72%
boilerplate chrome-extension javascript npm react typescript webpack

chrome-extension-boilerplate-react's People

Contributors

adam-lynch avatar dependabot[bot] avatar geekaholiclin avatar lxieyang avatar meandavejustice 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chrome-extension-boilerplate-react's Issues

Using Tailwindcss

Anyone tried using TailwindCss with this boilerplate? If you have, can you provide some guidance on how to implement this? Thank. you very much!

Several problems currently encountered (important )

problem

  1. During hot loading, the previously packaged files will not be removed, resulting in a large package (so Oops)
  2. When there is an error, no error will be prompted when npm run build is executed, and it can only be found through npm run start (so Oops)

Suggest

  1. Can the template be replaced with typescript and less or sass?
  2. add git commit verification

Typescript support

Do you have any advice for extending this to support Typescript?

I'd be happy to submit a PR back if you help outline what would be involved.

SCSS/SASS?

First of all, I'd like to say that I'm genuinely amazed by this boilerplate. By far the best I've seen so far. Thank you very much for the efforts and the kindness to open source it.

Secondly, would it be possible to add support for SCSS and SASS?

Thank you!

static assets

Hey is there anyway I can point to the location of static assets like you use public folder inside any react app. I have many static assets I wanna use in my project other that images, like json. What do you think the best way to go about this in this boilerplate

Webpack `file-loader` has been deprecated

If you check https://v4.webpack.js.org/loaders/file-loader/ it says that it has been deprecated for V5 and https://webpack.js.org/guides/asset-modules/ should be used instead.

I've changed this to:

      {
        test: new RegExp('.(' + fileExtensions.join('|') + ')$'),
        type: 'asset/resource',
        exclude: /node_modules/,
      },

And it seems to be working fine. I ended up doing this change cause images targeted from React css files were not being emitted properly by webpack.

Webpack failing to compile

After a fresh install build and start webpack is failing to compile.

cacheable modules 1.34 MiB
modules by path ./node_modules/ 1.3 MiB 47 modules
modules by path ./src/ 36.9 KiB
modules by path ./src/pages/ 33.5 KiB 30 modules
modules by path ./src/assets/img/ 115 bytes
./src/assets/img/logo.svg 52 bytes [built] [code generated]
./src/assets/img/tmp-launcher128.png 63 bytes [built] [code generated]
./src/containers/Greetings/Greetings.jsx 3.28 KiB [built] [code generated]
./node_modules/webpack/hot/ sync nonrecursive ^./log$ 170 bytes [built] [code generated]

ERROR in Unexpected string in JSON at position 355

webpack 5.23.0 compiled with 1 error in 10130 ms
i 「wdm」: Failed to compile.

JQuery

Hey sorry if this is the wrong place but how do install jquery into this so i can run jquery in the content script? I dont see any thing in the docs for this.

hot reload newly added entry point

Let me say pretty marvellous repo, I've learned a bit about webpack.

I decided to add another entry point in the webpack.config

  indexScript: path.join(__dirname, 'src', 'index.jsx'),

inside this file is this:
it makes use of InboxSDK.js
it adds functionality to various parts of Gmail web UI and accepts React nicely for UI building
I added it to webpack config and it gets copied over where it needs to be

import React from 'react';
import ReactDOM from 'react-dom';

const TestFeature = () => <div>boo hoo</div>; //editing these components does not trigger a reload
const Sidebar = () => <div>Boo hoo hoo</div>;

InboxSDK.load(2, 'app-id').then((sdk) => {

    //Add Compose new window bottom toolbar
    sdk.Compose.registerComposeViewHandler((composeView) => {
        composeView.addButton({
            title: "Test Feature",
            iconUrl: chrome.runtime.getURL('images/template-list-256x256.svg'),
            hasDropdown: true,
            onClick: (event) => {
                ReactDOM.render(<TestFeature />, event.dropdown.el);
            }
        });
    });

    //Sidebar 
    let el = document.createElement('div')
    ReactDOM.render(<Sidebar />, el)

    const panelView = sdk.Global.addSidebarContentPanel({
        title: 'Sidebar test',
        iconUrl: 'sidebarlogo.png',
        el: el
    })

    //Auto Open sidebar
    //Mainly handle for development
    panelView.then(function (panel) {
        panel.open()
    });

});

Everything loads up and works except no magic reload for indexScript
When I save indexScript the webserver recompiles build updates but I need to refresh extension page setting and then reload browser

Any insight much appreciated

chrome.scripting.executeScript issues - return value an files loading

A of chromium version 91 chrome.tabs.executeScript was deprecated and chrome.scripting.executeScript was introduced as the new API for executing scripts.

I'm having 2 different issues with that.

  1. loading specific scripts files will not work since the scripts files are being ignored by the build process.
let results = await chrome.scripting.executeScript({
    target: {tabId: tab.id, allFrames: true},
    files: ['my-script.js', 'my-other-script.js'],
  });
  1. We can't get a return value since the return value is omitted in build time.
    In the case of using a function instead of scripts files the function do run but to get a return value we need to write the desired return value as the final statement in the script and this will be evaluated as the return value.
// script function:
function myScript () {
  var headers = document.querySelectorAll('p');
  headers.length;
}

// executing the script
chrome.scripting.executeScript({
    target: {tabId, allFrames: true},
    func: myScript
}, (results => {
  // results[0] value is {frameId: 0, result: null}
}));

The issue here is that the headers.length; statement will be omitted during build (although I tried to manually add it to the build output itself, that didn't work either, and I didn't get the chance to keep investigating it)

All the relevant mentioned information can be found on chrome developer blog - scripting article and chrome.scripting docs

Architecture

Hey buddy, how about if we add structure like MVC or something to organize our code more better way, When our extension went bigger and bigger it always tough to maintain the code so any architecture would be best thing. :)

content.styles.css @import doesn't work

Hi!

I tried your boilerplate and content-script's css-file was copied. But there is another problem.
I want to use some react library (Ant.Design) and I need to use their styles.
According to Use in create-react-app I added @import '~antd/dist/antd.css'; but actual styles weren't added to /build/content.styles.css.
Any solutions to this problem?

Thanks in advance!

The build folder is too large

I developed with jQuery, and the code was about 270 KB in size. But using this scaffold, I found that the packed build folder was 7MB, it's too large.

help with webpack config

can you teach me how to setup svg-url-loader so i can use inline svgs?

Code_vxuoupmlmq
also is that correct for the secret configs?
for dev i use, NODE_ENV=development npm run build
testing: NODE_ENV=testing npm run build
production: NODE_ENV=production npm run build

ES5 / ES6

Hello, thanks for sharing your boilerplate.

I just want to know if there is a reason of why you don't use ES6 ?

Regards.

Running background script continously

Service worker auto get into inactive mode and background script is not running in background at all. We can not use persistent in manifest 3.0, What is the work around on that. I am running setInterval callback in my background script, which only running when background page open, on close service worker auto get into inactive mode.

build folder gets huge

Hi, first of all: thanks for the great work. Very helpful!

One question: when running in hot reload the build folders does not empty automatically and contains thousands of files after a while. Am I doing something wrong -- or how can I avoid it?

Thanks
Leander

More guidance on how to use Secrets?

I have created a file at root called secrets.development.js:

export default config = {
  apiKey: 'string',
  authDomain: 'string',
  databaseURL: 'string'
};

  1. How do I import this object within my files?
  2. How do I call to the specific key value pairs in my document?

Thanks!

OAuth Package Compatibility Issue?

When I try to import oauth it throws an error:

Uncaught ReferenceError: process is not defined
    at eval (webpack-internal:///./node_modules/pbkdf2/lib/default-encoding.js:3)
    at Object../node_modules/pbkdf2/lib/default-encoding.js (popup.bundle.js:1905)
    at __webpack_require__ (popup.bundle.js:3056)
    at fn (popup.bundle.js:3278)
    at eval (webpack-internal:///./node_modules/pbkdf2/lib/async.js:4)
    at Object../node_modules/pbkdf2/lib/async.js (popup.bundle.js:1895)
    at __webpack_require__ (popup.bundle.js:3056)
    at fn (popup.bundle.js:3278)
    at eval (webpack-internal:///./node_modules/pbkdf2/browser.js:1)
    at Object../node_modules/pbkdf2/browser.js (popup.bundle.js:1885)

[suggestion] reference @types/chrome as a dependency

To have better intelligent code completion and type hint, I suggest to reference @types/chrome as a dependency. It also saves time from looking for function signatures in the docs.

建议把 @types/chrome 作为 dependency 引入,这样在 VS Code 里面能自动提示属性名、方法名和函数签名。写起来会快捷很多。

This is a very good boilerplate to start creating a new extension with. Great work!

How do disable newTab redirect

sorry for disturb, i am very new for chrome-extention, i did not find any info about how to disable or enbale dirrent pages.

waiting for your help and thanks.

JS source maps fail to load

image

It might be a chrome bug. Just recently started seeing this in my webpack based setup. I'm looking around for other open-source extensions/boilerplates and it appears that all face the same issue?

Are you aware of this? Thanks!

Content Scripts SCSS

Hi there,

I was wondering how can I use SCSS for the content.styles.css file?

Thank you!

Uncaught TypeError: self.location.reload is not a function

The error:

Uncaught TypeError: self.location.reload is not a function
    at Object.staticChanged [as static-changed] (background.bundle.js:2632)
    at background.bundle.js:3869
    at WebSocket.<anonymous> (background.bundle.js:2475)

I have just written some lines of code at Background/index.js and those errors appeared. How can I solve this?

I have seen too in the background.bundle.js this:
image

Building with Content Security Policy

I'm curious if you've experimented with any solutions to allow for using a hashed SHA256 CSP rather than including unsafe-eval as this is highly discouraged according to Google's documentation. Using create-react-app this can be achieved by including INLINE_RUNTIME_CHUNK=false in .env I believe, but am not experienced enough with webpack to really know how a workaround can be built into this project. Have you worked on this at all? Any ideas on where to begin to build around this?

Conflict with React inside the content script

Hi,

i have a problem when i try to load the content script (with 'react' and 'react-dom' imported) when i'm in other websites that are developed also with React. It seems that rerenders the website after the content script is loaded and it causes a "conflict" with the current page and that makes that the page looks different (I mean, different when my extension is disabled).

I tried with dynamic imports but it is impossible because the dynamic imports code ran it the browser.

It is possible to isolate my React modal (rendered in the content script) to avoid the conflict with other React websites?

The content script code is similar to this (simplified):

import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import AddWebsiteModal from '../components/AddWebsiteModal/AddWebsiteModal';

function existsModalDivTag() {
  return document.getElementById('myapp-modal-webadd');
}

export function createProductModal(authData, websiteData) {
  console.log(websiteData);

  if (!existsModalDivTag()) {
    const element = document.createElement('div');
    element.setAttribute('id', 'myapp-modal-webadd');
    document.body.appendChild(element);

    return render(<AddWebsiteModal auth={authData} websiteData={websiteData} />, document.querySelector('#myapp-modal-webadd'));
  } else {
    unmountComponentAtNode(document.getElementById('myapp-modal-webadd'));
    document.getElementById('myapp-modal-webadd').remove();
  }
}

Thanks.

How to get content and background scripts to hot reload as well?

I'm having trouble figuring out what to change in webpack config in order to get my content and background scripts to hot reload with the rest of my app.

This is important for me because my extension is injecting a react component into a user's active tab via the content script. For this reason I need the content script to hot reload.

Here is my webpack config, or at least what I think are the relevant parts.

var options = {
  mode: process.env.NODE_ENV || 'development',
  entry: {
    newtab: path.join(__dirname, 'src', 'pages', 'Newtab', 'index.jsx'),
    content: path.join(__dirname, 'src', 'pages', 'Content', 'index.js'),
    options: path.join(__dirname, 'src', 'pages', 'Options', 'index.jsx'),
    popup: path.join(__dirname, 'src', 'pages', 'Popup', 'index.jsx'),
    pageInject: path.join(__dirname, 'src', 'pages', 'PageInject', 'index.jsx'),
    background: path.join(__dirname, 'src', 'pages', 'Background', 'index.js'),
    contentScript: path.join(__dirname, 'src', 'pages', 'Content', 'index.js'),
    devtools: path.join(__dirname, 'src', 'pages', 'Devtools', 'index.js'),
    panel: path.join(__dirname, 'src', 'pages', 'Panel', 'index.jsx'),
  },
  chromeExtensionBoilerplate: {
    notHotReload: ['devtools'],
  },

As you can see I removed contentScript from chromeExtensionBoilerPlate.notHotReload.

Honestly I'm having trouble even understanding how this hot reloading is working, any help here is appreciated!

Embed iframes inside content page

Hey @lxieyang, thanks for an amazing starter kit. I'm new to webpack and this really helped me get started with extension dev.

What I want to do is to create a custom widget (in an iframe) and embed it on the bottom right corner on activated webpages.

But the issue I'm facing is the document object on src/pages/Content/index.js is referring to the actual page document, which is good but first I have to develop the iframe like a react app by running:

ReactDOM.render(<App />, document.querySelector("#app-container")

NB: app-container is from a HTML doc we created at path: src/pages/Content/index.html.

and then I'd embed it into the actual document. So I'm not finding a suitable way to solve this.

I hope I explained it well, if not I'm open to discussions.

Thanks.

Guidance on upgrading from Samuel's version

Hi!

do you have any guidance on upgrading from Samuel's older version of the boiler plate? i'm running webpack 4\react17\etc for for a fairly big personal project.

thanks,
Shravan

Not able to receive logs from React Popup

Hi there, first of all: amazing boilerplate! It's working fantastic and has made the setup of our chrome extension sooo much easier - thanks a lot for your work!!

I've encountered an issue with logging anything from within pages/Popup. In there I'm using React and everything renders correctly and all logic is executed as expected. However, no logs from Popup/index.tsx are displayed in the browser console. Am I missing anything?
Is there something in the webpack config which omits any console.log call in the build folder?

Any help would be much appreciated!

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.