Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiecong committed Feb 9, 2023
1 parent e706934 commit 1ddfbe9
Show file tree
Hide file tree
Showing 38 changed files with 312 additions and 177 deletions.
2 changes: 1 addition & 1 deletion examples/crx-basic/src/content.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('content script')
console.log('content script')
1 change: 1 addition & 0 deletions examples/crx-basic/src/popup.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './style.css'
console.log('popup')
10 changes: 10 additions & 0 deletions examples/crx-basic/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
body{
width: 200px;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
font-size: 14px;
font-weight: bold;
background-color: blueviolet;
}
5 changes: 2 additions & 3 deletions examples/crx-contentscript-sass-less/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'vite'
import crx from 'vite-plugin-crx-mv3'

export default defineConfig(({mode}) => {
export default defineConfig(({ mode }) => {
return {
plugins: [
crx({
Expand All @@ -10,7 +10,6 @@ export default defineConfig(({mode}) => {
],
build: {
emptyOutDir: mode == 'production'
},
}
}
})

20 changes: 10 additions & 10 deletions examples/crx-devtools/panel.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
panel
</body>
</html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
panel
</body>
</html>
8 changes: 3 additions & 5 deletions examples/crx-devtools/src/devtools.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
chrome.devtools.panels.create(
'DevPanel',
null,
'../panel.html',
function (panel) {

}
'panel.html',
function (panel) {}
)

chrome.devtools.panels.elements.createSidebarPane(
'Sidebar',
function (sidebar) {
sidebar.setPage('../sidebar.html')
sidebar.setPage('sidebar.html')
}
)
2 changes: 1 addition & 1 deletion examples/crx-executescript-files/src/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
chrome.action.onClicked.addListener((tab) => {
if (!tab.url.includes('chrome://')) {
if (!tab.url?.startsWith('chrome://')) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['dynamicScript.js']
Expand Down
10 changes: 5 additions & 5 deletions examples/crx-executescript-function/src/background.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function setBgColor() {
document.body.style.backgroundColor = '#ff0';
document.body.style.backgroundColor = '#f00'
}

chrome.action.onClicked.addListener((tab) => {
if(!tab.url.includes("chrome://")) {
chrome.action.onClicked.addListener((tab) => {
if (!tab.url?.startsWith('chrome://')) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: setBgColor
});
})
}
});
})
16 changes: 0 additions & 16 deletions examples/crx-jquery/package.json

This file was deleted.

Binary file removed examples/crx-jquery/src/assets/icons/icon128.png
Binary file not shown.
Binary file removed examples/crx-jquery/src/assets/icons/icon16.png
Binary file not shown.
Binary file removed examples/crx-jquery/src/assets/icons/icon48.png
Binary file not shown.
1 change: 0 additions & 1 deletion examples/crx-jquery/src/background.js

This file was deleted.

6 changes: 0 additions & 6 deletions examples/crx-jquery/src/content.js

This file was deleted.

7 changes: 0 additions & 7 deletions examples/crx-jquery/src/main.js

This file was deleted.

22 changes: 0 additions & 22 deletions examples/crx-jquery/src/manifest.json

This file was deleted.

9 changes: 9 additions & 0 deletions examples/crx-react-sandbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Usage
The dist directory will be created after starting the project.Then Open chrome://extensions/ and import the dist directory.
```bash
# development
pnpm dev

# production
pnpm build
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
<title>title</title>
</head>
<body>
<div id="app">
<button id="btn">button</button>
</div>
<script type="module" src="./src/main.js"></script>
<div id="app"></div>
<script type="module" src="./src/main.tsx"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/crx-react-sandbox/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "crx-react-sandbox",
"version": "0.0.1",
"scripts": {
"dev": "vite build --watch --mode development",
"build": "vite build"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@types/react": "^17.0.33",
"@types/react-dom": "^17.0.10",
"@vitejs/plugin-react": "^1.0.7",
"vite": "^3.0.9",
"typescript": "^4.5.4",
"vite-plugin-crx-mv3": "workspace:*"
}
}
12 changes: 12 additions & 0 deletions examples/crx-react-sandbox/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
</head>
<body>
<div id="app"></div>
<script type="module" src="./src/main.tsx"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions examples/crx-react-sandbox/sandbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
</head>
<body>
<div id="app"></div>
<script type="module" src="./src/sandbox.tsx"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions examples/crx-react-sandbox/src/Popup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect, useRef, useState } from "react"

function Popup() {
const iframeRef = useRef<HTMLIFrameElement>(null)
const [count, setCount] = useState("");

useEffect(() => {
window.addEventListener("message", (event) => {
setCount(event.data)
console.log("EVAL output: " + event.data)
})
}, [])

return (
<div
style={{
display: "flex",
flexDirection: "column",
padding: 12,
width: 175
}}>
<button
onClick={() => {
setCount(count + 1)
iframeRef.current?.contentWindow?.postMessage("10 + 20", "*")
}}>
Trigger iframe eval
</button>
<div style={{marginTop:5}}>
Use eval to execute "10 + 20": {count}
</div>
<iframe src="sandbox.html" ref={iframeRef} style={{ display: "none" }} />
</div>
)
}

export default Popup
7 changes: 7 additions & 0 deletions examples/crx-react-sandbox/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ReactDOM from 'react-dom'
import Popup from './Popup.tsx'

ReactDOM.render(
<Popup />,
document.getElementById('app')
)
13 changes: 13 additions & 0 deletions examples/crx-react-sandbox/src/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "crx-react-sandbox",
"description": "A Chrome extension demo.",
"version": "1.0.0",
"icons": {},
"action": {
"default_popup": "../popup.html"
},
"sandbox":{
"pages": ["../sandbox.html"]
},
"manifest_version": 3
}
21 changes: 21 additions & 0 deletions examples/crx-react-sandbox/src/sandbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ReactDOM from 'react-dom'

window.addEventListener("message", async function (event) {
const source = event.source as {
window: WindowProxy
}
source.window.postMessage(eval(event.data), event.origin)
})


ReactDOM.render(
<div
style={{
display: "flex",
flexDirection: "column",
padding: 16
}}>
HELLO SANDBOX
</div>,
document.getElementById('app')
)
21 changes: 21 additions & 0 deletions examples/crx-react-sandbox/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": false,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
8 changes: 8 additions & 0 deletions examples/crx-react-sandbox/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"composite": true,
"module": "esnext",
"moduleResolution": "node"
},
"include": ["vite.config.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import crx from 'vite-plugin-crx-mv3'

export default defineConfig(({ mode }) => {
return {
plugins: [
react(),
crx({
manifest: './src/manifest.json'
manifest: './src/manifest.json',
}),
],
build: {
emptyOutDir: mode == 'production',
rollupOptions: {
}
},
}
})
2 changes: 1 addition & 1 deletion examples/crx-solid/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"128": "assets/icons/icon128.png"
},
"action": {
"default_title": "Hello World",
"default_title": "crx-solid",
"default_popup": "../popup.html"
},
"background": {
Expand Down
Loading

0 comments on commit 1ddfbe9

Please sign in to comment.