Skip to content

Commit

Permalink
feat: add simplified chrome extension setup
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinmuenster committed Apr 20, 2024
1 parent 87330d3 commit 0dbf2ff
Show file tree
Hide file tree
Showing 15 changed files with 1,475 additions and 95 deletions.
22 changes: 22 additions & 0 deletions apps/chrome-extension/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: ['@repo/eslint-config/react-library.js'],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
'no-unused-vars': [
2,
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
},
};
24 changes: 24 additions & 0 deletions apps/chrome-extension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
30 changes: 30 additions & 0 deletions apps/chrome-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default {
// other rules...
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json'],
tsconfigRootDir: __dirname,
},
};
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
3 changes: 3 additions & 0 deletions apps/chrome-extension/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
chrome.action.onClicked.addListener(tab => {
if (tab.id) chrome.tabs.sendMessage(tab.id, { action: 'togglePane' });
});
99 changes: 99 additions & 0 deletions apps/chrome-extension/content-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const createAppFrame = () => {
const className = '__keeep-extension__';
const styleSheet = new CSSStyleSheet();

styleSheet.insertRule(`
.${className} {
position: fixed;
top: 0;
right: 0;
height: 100%;
width: 65%;
max-width: 400px;
max-width: 400px;
box-sizing: border-box;
will-change: transform, opacity;
transition-property: transform, opacity;
z-index: 10000;
background: white;
border: 0;
box-shadow:
0px 0px 1px 0px rgba(24, 26, 27, 0.04),
0px 3px 6px 0px rgba(24, 26, 27, 0.08),
0px 9px 88px 0px rgba(24, 26, 27, 0.28),
0 0 0 1px rgba(141, 141, 141, 0.24);
}`);

styleSheet.insertRule(`
@media (prefers-reduced-motion: no-preference) {
.${className}[data-visible='true'] {
transition-duration: 400ms;
transition-timing-function: cubic-bezier(0.05, 0.7, 0.1, 1);
}
}`);

styleSheet.insertRule(`
.${className}[data-visible='true'] {
opacity: 1;
transform: translateX(0);
pointer-events: auto;
}
`);

styleSheet.insertRule(`
@media (prefers-reduced-motion: no-preference) {
.${className}[data-visible='false'] {
transition-duration: 200ms;
transition-timing-function: cubic-bezier(0.3, 0, 0.8, 0.15);
}
}`);

styleSheet.insertRule(`
.${className}[data-visible='false'] {
opacity: 0;
transform: translateX(100%);
pointer-events: none;
}`);

document.adoptedStyleSheets.push(styleSheet);

const iframe = document.createElement('iframe');
iframe.src = chrome.runtime.getURL('iframe.html');
iframe.dataset.visible = 'false';
iframe.style.display = 'none';
iframe.className = className;

return iframe;
};

const iframeElement = createAppFrame();
document.body.appendChild(iframeElement);

const setPaneVisibility = (element: HTMLElement, isVisible: boolean) => {
if (isVisible) {
element.style.display = '';
} else {
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
element.addEventListener(
'transitionend',
() => {
element.style.display =
element.dataset.visible === 'true' ? 'block' : 'none';
},
{ once: true }
);
} else {
element.style.display = 'none';
}
}

requestAnimationFrame(() => {
element.dataset.visible = isVisible ? 'true' : 'false';
});
};

chrome.runtime.onMessage.addListener(message => {
if (message.action === 'togglePane') {
setPaneVisibility(iframeElement, iframeElement.dataset.visible === 'true');
}
});
12 changes: 12 additions & 0 deletions apps/chrome-extension/iframe.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>keeep Extension</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions apps/chrome-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "chrome-extension",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"prettier": "@repo/prettier-config/tailwindcss.js",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@crxjs/vite-plugin": "beta",
"@repo/eslint-config": "*",
"@types/chrome": "^0.0.266",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.53.0",
"typescript": "^5.2.2",
"vite": "^5.2.0"
}
}
Binary file added apps/chrome-extension/public/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions apps/chrome-extension/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useState } from 'react';

function App() {
const [count, setCount] = useState(0);

return (
<div className="root">
<h1>
<span>Vitedasddas11</span>
</h1>
<div className="card">
<button onClick={() => setCount(count => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</div>
);
}

export default App;
9 changes: 9 additions & 0 deletions apps/chrome-extension/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
1 change: 1 addition & 0 deletions apps/chrome-extension/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
25 changes: 25 additions & 0 deletions apps/chrome-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src", "content-script.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
11 changes: 11 additions & 0 deletions apps/chrome-extension/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["vite.config.ts"]
}
34 changes: 34 additions & 0 deletions apps/chrome-extension/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { crx, defineManifest } from '@crxjs/vite-plugin';

const manifest = defineManifest({
manifest_version: 3,
name: 'keeep Extension',
version: '1.0.0',
action: {
default_title: 'keeep Extension',
default_icon: 'icon.png',
},
background: {
service_worker: './background.ts',
},
content_scripts: [
{
js: ['./content-inject.ts'],
matches: ['https://www.linkedin.com/*'],
},
],
web_accessible_resources: [
{
resources: ['iframe.html'],
matches: ['https://www.linkedin.com/*'],
},
],
});

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), crx({ manifest })],
server: { hmr: { clientPort: 5173 } },
});
Loading

0 comments on commit 0dbf2ff

Please sign in to comment.