-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/injecting scripts from popup (#579)
Co-authored-by: JongHak Seo <unqocn@gmail.com>
- Loading branch information
1 parent
71d8e1e
commit 241f4f4
Showing
15 changed files
with
196 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import * as esbuild from "esbuild" | ||
import * as esbuild from 'esbuild'; | ||
|
||
/** | ||
* @type { import("esbuild").BuildOptions } | ||
*/ | ||
const buildOptions = { | ||
entryPoints: ["./index.ts", "./lib/**/*.ts", "./lib/**/*.tsx"], | ||
tsconfig: "./tsconfig.json", | ||
entryPoints: ['./index.ts', './lib/**/*.ts', './lib/**/*.tsx'], | ||
tsconfig: './tsconfig.json', | ||
bundle: false, | ||
target: 'es6', | ||
outdir: "./dist", | ||
outdir: './dist', | ||
sourcemap: true, | ||
} | ||
}; | ||
|
||
await esbuild.build(buildOptions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import * as esbuild from "esbuild" | ||
import * as fs from "fs"; | ||
import { resolve } from "node:path"; | ||
import * as esbuild from 'esbuild'; | ||
import * as fs from 'fs'; | ||
import { resolve } from 'node:path'; | ||
|
||
/** | ||
* @type { import("esbuild").BuildOptions } | ||
*/ | ||
const buildOptions = { | ||
entryPoints: ["./index.ts","./tailwind.config.ts", "./lib/**/*.ts", "./lib/**/*.tsx"], | ||
tsconfig: "./tsconfig.json", | ||
entryPoints: ['./index.ts', './tailwind.config.ts', './lib/**/*.ts', './lib/**/*.tsx'], | ||
tsconfig: './tsconfig.json', | ||
bundle: false, | ||
target: 'es6', | ||
outdir: "./dist", | ||
outdir: './dist', | ||
sourcemap: true, | ||
} | ||
}; | ||
|
||
await esbuild.build(buildOptions); | ||
fs.copyFileSync(resolve("lib", "global.css"),resolve("dist", "global.css")) | ||
fs.copyFileSync(resolve('lib', 'global.css'), resolve('dist', 'global.css')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useEffect } from 'react'; | ||
|
||
export default function App() { | ||
useEffect(() => { | ||
console.log('runtime content view loaded'); | ||
}, []); | ||
|
||
return <div className="runtime-content-view-text">runtime content view</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.runtime-content-view-text { | ||
font-size: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* DO NOT USE import someModule from '...'; | ||
* | ||
* @issue-url https://github.com/Jonghakseo/chrome-extension-boilerplate-react-vite/issues/160 | ||
* | ||
* Chrome extensions don't support modules in content scripts. | ||
* If you want to use other modules in content scripts, you need to import them via these files. | ||
* | ||
*/ | ||
import('@lib/root'); | ||
console.log('runtime script loaded'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createRoot } from 'react-dom/client'; | ||
import App from '@lib/app'; | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
import injectedStyle from '@lib/index.css?inline'; | ||
|
||
const root = document.createElement('div'); | ||
root.id = 'chrome-extension-boilerplate-react-vite-runtime-content-view-root'; | ||
|
||
document.body.append(root); | ||
|
||
const rootIntoShadow = document.createElement('div'); | ||
rootIntoShadow.id = 'shadow-root'; | ||
|
||
const shadowRoot = root.attachShadow({ mode: 'open' }); | ||
shadowRoot.appendChild(rootIntoShadow); | ||
|
||
/** Inject styles into shadow dom */ | ||
const globalStyleSheet = new CSSStyleSheet(); | ||
globalStyleSheet.replaceSync(injectedStyle); | ||
shadowRoot.adoptedStyleSheets = [globalStyleSheet]; | ||
shadowRoot.appendChild(rootIntoShadow); | ||
/** | ||
* In the firefox environment, the adoptedStyleSheets bug may prevent style from being applied properly. | ||
* | ||
* @url https://bugzilla.mozilla.org/show_bug.cgi?id=1770592 | ||
* @url https://github.com/Jonghakseo/chrome-extension-boilerplate-react-vite/pull/174 | ||
* | ||
* Please refer to the links above and try the following code if you encounter the issue. | ||
* | ||
* ```ts | ||
* const styleElement = document.createElement('style'); | ||
* styleElement.innerHTML = injectedStyle; | ||
* shadowRoot.appendChild(styleElement); | ||
* ``` | ||
*/ | ||
|
||
createRoot(rootIntoShadow).render(<App />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@chrome-extension-boilerplate/content-runtime-script", | ||
"version": "0.0.1", | ||
"description": "chrome extension content runtime script", | ||
"private": true, | ||
"sideEffects": true, | ||
"files": [ | ||
"dist/**" | ||
], | ||
"scripts": { | ||
"clean": "rimraf ./dist", | ||
"build": "pnpm run clean && pnpm type-check && vite build", | ||
"build:watch": "cross-env __DEV__=true vite build -w --mode development", | ||
"dev": "pnpm build:watch", | ||
"lint": "eslint . --ext .ts,.tsx", | ||
"lint:fix": "pnpm lint --fix", | ||
"prettier": "prettier . --write --ignore-path ../../.prettierignore", | ||
"type-check": "tsc --noEmit" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@chrome-extension-boilerplate/tsconfig": "workspace:*", | ||
"@chrome-extension-boilerplate/hmr": "workspace:*", | ||
"@chrome-extension-boilerplate/vite-config": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "@chrome-extension-boilerplate/tsconfig/base", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@lib/*": ["lib/*"] | ||
}, | ||
"types": ["chrome"] | ||
}, | ||
"include": ["lib"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { resolve } from 'path'; | ||
import { withPageConfig } from '@chrome-extension-boilerplate/vite-config'; | ||
|
||
const rootDir = resolve(__dirname); | ||
const libDir = resolve(rootDir, 'lib'); | ||
|
||
export default withPageConfig({ | ||
resolve: { | ||
alias: { | ||
'@lib': libDir, | ||
}, | ||
}, | ||
publicDir: resolve(rootDir, 'public'), | ||
build: { | ||
lib: { | ||
formats: ['iife'], | ||
entry: resolve(__dirname, 'lib/index.ts'), | ||
name: 'ContentRuntimeScript', | ||
fileName: 'index', | ||
}, | ||
outDir: resolve(rootDir, '..', '..', 'dist', 'content-runtime'), | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.