Skip to content

Commit 4f94065

Browse files
committed
upgrade to react 18
1 parent fa700e3 commit 4f94065

23 files changed

+1233
-1182
lines changed

.babelrc

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
{
22
"presets": [
3-
"@babel/preset-env",
4-
"@babel/preset-react"
3+
[ "@babel/preset-env", {
4+
"modules": "auto",
5+
"targets": {
6+
"browsers": [
7+
"last 2 Chrome versions",
8+
"last 2 Firefox versions",
9+
"last 2 Safari versions",
10+
"last 2 iOS versions",
11+
"last 1 Android version",
12+
"last 1 ChromeAndroid version",
13+
"ie 11"
14+
]
15+
}
16+
} ],
17+
["@babel/preset-react", {
18+
"runtime": "automatic"
19+
}]
520
],
621
"plugins": [
7-
"@babel/plugin-proposal-class-properties" ,
22+
"@babel/plugin-proposal-class-properties",
23+
"@babel/plugin-transform-runtime",
824
"@babel/plugin-syntax-class-properties",
925
"@babel/plugin-syntax-dynamic-import",
10-
"@babel/plugin-transform-runtime"
26+
["@babel/plugin-transform-react-jsx", {
27+
"runtime": "automatic"
28+
}]
1129
]
12-
}
30+
}

.eslintrc

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"plugin:react/recommended"
2525
],
2626
"rules": {
27+
"react/jsx-uses-react": "off",
28+
"react/react-in-jsx-scope": "off",
2729
"semi": [2, "always"],
2830
"indent": ["error", 2],
2931
"object-curly-spacing": ["error", "always"],

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# A Browser(Chrome/Edge/Firefox/Brave) extension boilerplate.
1+
# A Browser(Chrome/Edge/Opera/Firefox/Brave) extension boilerplate.
22

33
## Prerequisites
44

5-
- yarn v1.17.3
6-
- node v12.3.1
5+
- yarn 1.22.10
6+
- node v16.1.0
77

88
## Pre-configurations
99

@@ -76,6 +76,7 @@ yarn build:firefox
7676
> 8. Some (useful) services e.g. DbService, messagePassing, chromeService and helper functions(helpers).
7777
> 9. Some (useful) react components e.g. FrameMUI.js (To mount react mui component in iframe in content script).
7878
> 10. Locale translation using unofficial google translate API (Free version).
79+
> 11. Comes with React 18(alpha release). you can downgrade it to 17.* but not lower than that.
7980
8081
## directory structure
8182

constants.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ const constants = {
1010
"EDGE_STORE_URL",
1111
},
1212
// put extension key here if required which would only be used in development mode
13-
key:
14-
// eslint-disable-next-line max-len
15-
"-----BEGIN PUBLIC KEY-----\nMssIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhnjCHD79hDbWfqjnP9SW\nN0AXmXHh5eVk2D0oMILadmj/2uRQm/jNpnlvjVHY+899gdYRH/k7v4f+qF6gV3Yh\nwb/2PnW9PEo2VksgiixSesccvcwRXEJSngtxNWOARYSOTqTCCum9rjFVPZL+voh7\ndUsTN45D+RBLQORYbu5NtSM6RcGhUbYvhAosKwo+lsOAjM5niQDLJi//dZ9R32tc\n6tht4XrP59CTWBo8JCpIcHT6edN2HQ2vRiO5CHVb2jfT1hrO82WZ9LWsErzo/gCr\nXyIvnnXjg1wif7++WTi5mQl05Ohv3vKazNLeFpl/2XxvJ1xw4Pfh7m2qMaBJd7ll\n3QIDAQAB\n-----END PUBLIC KEY-----",
13+
key: "SSH_PUBLIC_KEY", // gather it from extension store
1614
},
1715
contentScript: {
1816
mountId: generateGuid(),

package.json

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
2-
"name": "chrome-extension-boilerplate",
2+
"name": "browser-extension-boilerplate",
33
"version": "0.0.1",
44
"description": "",
5-
"main": "index.js",
65
"homepage": "https://fxnoob.github.io/chrome-extension-boilerplate",
76
"scripts": {
87
"build:tailwind": "tailwindcss build src/styles/style.css -o dist/style.css",
@@ -11,10 +10,10 @@
1110
"prebuild": "yarn rm && yarn build:tailwind",
1211
"rm": "rm -rf dist/",
1312
"rm:zip": "rm dist.zip",
14-
"dev:chromium": "yarn build:tailwind && webpack --watch --progress --colors --config chromium.webpack.js --mode=development",
15-
"dev:firefox": "yarn build:tailwind && webpack --watch --progress --colors --config firefox.webpack.js --mode=development",
16-
"build:chromium": "yarn build:tailwind && webpack --progress --colors --config chromium.webpack.js --mode=production",
17-
"build:firefox": "yarn build:tailwind && webpack --progress --colors --config firefox.webpack.js --mode=production",
13+
"dev:chromium": "yarn build:tailwind && webpack --stats-children --watch --progress --config chromium.webpack.js --mode=development",
14+
"dev:firefox": "yarn build:tailwind && webpack --watch --progress --config firefox.webpack.js --mode=development",
15+
"build:chromium": "yarn build:tailwind && webpack --progress --config chromium.webpack.js --mode=production",
16+
"build:firefox": "yarn build:tailwind && webpack --progress --config firefox.webpack.js --mode=production",
1817
"jsdoc": "jsdoc src -r",
1918
"deploy_jsdoc": "yarn jsdoc && gh-pages -d out",
2019
"zip": "zip -r dist.zip dist/*",
@@ -31,52 +30,61 @@
3130
"author": "",
3231
"license": "ISC",
3332
"devDependencies": {
34-
"@babel/core": "^7.5.5",
33+
"@babel/core": "^7.9.0",
3534
"@babel/plugin-proposal-class-properties": "^7.5.5",
3635
"@babel/plugin-syntax-class-properties": "^7.2.0",
3736
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
37+
"@babel/plugin-transform-react-jsx": "^7.14.3",
3838
"@babel/plugin-transform-runtime": "^7.5.5",
3939
"@babel/preset-env": "^7.5.5",
40-
"@babel/preset-react": "^7.0.0",
40+
"@babel/preset-react": "^7.9.0",
41+
"@tailwindcss/ui": "^0.5.0",
42+
"arg": "^5.0.0",
4143
"babel-eslint": "^10.1.0",
4244
"babel-loader": "^8.0.6",
45+
"chalk": "^4.1.1",
4346
"chokidar": "^3.5.1",
4447
"chokidar-cli": "^2.1.0",
4548
"concurrently": "^5.3.0",
4649
"copy-webpack-plugin": "^5.0.4",
4750
"css-loader": "^4.3.0",
4851
"dotenv": "^8.2.0",
4952
"esbuild-loader": "^2.4.0",
50-
"eslint": "^7.6.0",
53+
"eslint": "^7.27.0",
5154
"eslint-loader": "^4.0.2",
5255
"eslint-plugin-react": "^7.20.5",
5356
"eslint-watch": "^7.0.0",
57+
"esm": "^3.2.25",
5458
"file-loader": "^4.2.0",
5559
"gh-pages": "^2.1.1",
5660
"husky": "^3.0.9",
5761
"image-webpack-loader": "^5.0.0",
62+
"inquirer": "^8.1.0",
5863
"jsdoc": "^3.6.3",
64+
"node-google-translate-skidz": "^1.1.2",
5965
"prettier": "^2.2.1",
6066
"pretty-quick": "^2.0.0",
6167
"react-hot-loader": "^4.12.10",
6268
"style-loader": "^1.2.1",
63-
"webpack": "^4.39.1",
64-
"webpack-cli": "^3.3.6",
65-
"webpack-dev-server": "^3.7.2",
66-
"node-google-translate-skidz": "^1.1.2",
67-
"zx": "^1.0.2",
6869
"tailwindcss": "^1.8.3",
69-
"@tailwindcss/ui": "^0.5.0"
70+
"webpack": "^5.38.1",
71+
"webpack-cli": "^4.7.0",
72+
"webpack-dev-server": "^3.7.2",
73+
"worker-loader": "^3.0.8",
74+
"zx": "^1.0.2"
7075
},
7176
"dependencies": {
7277
"@babel/polyfill": "^7.4.4",
7378
"@babel/runtime": "^7.5.5",
79+
"@emotion/css": "^11.1.3",
7480
"@material-ui/core": "^4.3.3",
7581
"@material-ui/icons": "^4.2.1",
82+
"comlink": "^4.3.1",
7683
"jss": "^10.6.0",
7784
"jss-rtl": "^0.3.0",
78-
"react": "^16.8.6",
79-
"react-dom": "^16.8.6",
85+
"react": "^18.0.0-alpha-e6be2d531",
86+
"react-content-loader": "^6.0.3",
87+
"react-dom": "^18.0.0-alpha-e6be2d531",
8088
"react-frame-component": "5.0.0"
8189
}
8290
}

scripts/transform.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ const manifestTransform = (content, path, options) => {
77
// copy-webpack-plugin passes a buffer
88
const manifest = JSON.parse(buffer.toString());
99
// make any modifications you like, such as
10-
if (mode == "development") {
10+
if (mode === "development") {
11+
manifest["content_security_policy"] = "script-src 'self' 'unsafe-eval'; object-src 'self'";
1112
manifest.key = constants.appConfig.key;
1213
}
1314
manifest.version = pkg.version;
15+
manifest.browser_action.default_title = constants.appConfig.appName;
1416
// if browser is firefox then put gecko id and other specific stuff
15-
if (browser == "firefox") {
17+
if (browser === "firefox") {
1618
Object.keys(constants.browser.firefox.manifest).map((key) => {
1719
manifest[key] = constants.browser.firefox.manifest[key];
1820
});

src/app/manifest.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@
3131
"run_at": "document_end"
3232
}
3333
],
34-
"manifest_version": 2,
35-
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
34+
"manifest_version": 2
3635
}

src/components/Demo.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import React from "react";
2-
3-
export default class Index extends React.Component {
4-
constructor(props) {
5-
super(props);
6-
}
7-
render() {
8-
return <div>Demo</div>;
9-
}
10-
}
1+
export default function Demo() {
2+
return (
3+
<div>Demo</div>
4+
);
5+
}

src/components/FrameMUI.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable react/prop-types */
2-
import React from "react";
2+
import { useState, useRef, useCallback } from "react";
33
import { create } from "jss";
44
import { useTheme, jssPreset, StylesProvider } from "@material-ui/core/styles";
55
import NoSsr from "@material-ui/core/NoSsr";
@@ -8,11 +8,11 @@ import IFrame from "react-frame-component";
88
function FrameComponent(props) {
99
const { children, ...other } = props;
1010
const theme = useTheme();
11-
const [state, setState] = React.useState({
11+
const [state, setState] = useState({
1212
ready: false
1313
});
14-
const instanceRef = React.useRef();
15-
const handleRef = React.useCallback(ref => {
14+
const instanceRef = useRef();
15+
const handleRef = useCallback(ref => {
1616
instanceRef.current = {
1717
contentDocument: ref ? ref.node.contentDocument : null,
1818
contentWindow: ref ? ref.node.contentWindow : null

src/content-scripts/App.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from "react";
21
import Demo from "../components/Demo";
32
function App() {
43
return <Demo/>;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React from "react";
21
import ReactDOM from "react-dom";
32
import App from "./App";
4-
import constants from "../../constants";
5-
const mountId = constants.contentScript.mountId;
3+
import Constants from "../../constants";
4+
const mountId = Constants.contentScript.mountId;
65
const Element = document.createElement("div");
76
Element.setAttribute("id", mountId);
87
document.body.appendChild(Element);
9-
ReactDOM.render(<App />, document.getElementById(mountId));
8+
const root = ReactDOM.createRoot(document.getElementById(mountId));
9+
root.render(<App />);

src/option-page/App.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function App() {
2+
return <div>Options</div>;
3+
}

src/option-page/App.jsx

-11
This file was deleted.

src/option-page/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ReactDOM from "react-dom";
2+
import App from "./App";
3+
const root = ReactDOM.createRoot(document.getElementById('app'));
4+
root.render(<App />);

src/popup-page/App.jsx

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
import React from "react";
2-
import ReactDOM from "react-dom";
3-
import Index from "../components/Demo";
4-
5-
const Element = document.createElement("div");
6-
Element.setAttribute("id", "dfghbnjmERHJKFGHNMVBNMFBNMbmvvxnbdgf");
7-
document.body.appendChild(Element);
8-
ReactDOM.render(
9-
<Index />,
10-
document.getElementById("dfghbnjmERHJKFGHNMVBNMFBNMbmvvxnbdgf")
11-
);
1+
export default function App() {
2+
return <div>App</div>;
3+
}

src/popup-page/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import ReactDOM from "react-dom";
3+
import App from "./App";
4+
5+
const Element = document.createElement("div");
6+
Element.setAttribute("id", "dfghbnjmERHJKFGHNMVBNMFBNMbmvvxnbdgf");
7+
document.body.appendChild(Element);
8+
const root = ReactDOM.createRoot(document.getElementById("dfghbnjmERHJKFGHNMVBNMFBNMbmvvxnbdgf"));
9+
root.render(<App />);

src/services/chromeService.js

-11
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ class ChromeApi {
4242
chrome.tabs.sendMessage(tab.id, payload, callback);
4343
return true;
4444
};
45-
takeScreenShot = () => {
46-
return new Promise((resolve, reject) => {
47-
try {
48-
chrome.tabs.captureVisibleTab((screenshotUrl) => {
49-
resolve(screenshotUrl);
50-
});
51-
} catch (e) {
52-
reject(e);
53-
}
54-
});
55-
};
5645
/**
5746
*Set Badge on extension icon
5847
* @method

src/services/dbService.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import schema from "./schema";
22
/**
33
* Chrome storage abstraction class
44
*
@@ -39,7 +39,7 @@ class DbService {
3939
if (items === undefined) {
4040
reject(new Error("Error"));
4141
} else {
42-
resolve(items);
42+
resolve({ ...schema.data, ...items });
4343
}
4444
});
4545
} catch (e) {
@@ -88,6 +88,20 @@ class DbService {
8888
}
8989
});
9090
}
91+
/**
92+
* callback for value change in db for any key
93+
* @memberof Db
94+
*/
95+
onChange = (keyToCheck, callback) => {
96+
// eslint-disable-next-line no-unused-vars
97+
chrome.storage.onChanged.addListener((changes, namespace) => {
98+
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
99+
if (keyToCheck == key) {
100+
callback(oldValue, newValue);
101+
}
102+
}
103+
});
104+
}
91105
}
92106
const db = new DbService();
93107
export default db;

0 commit comments

Comments
 (0)