Skip to content

Commit 0380b7b

Browse files
authored
Merge pull request #8 from janek26/feature/auto-connect-for-extensions
Feature/auto-connect-for-extensions
2 parents 4faa5fd + 8d69f87 commit 0380b7b

File tree

9 files changed

+3633
-2353
lines changed

9 files changed

+3633
-2353
lines changed

examples/with-plasmo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"@trpc/client": "^10.3.0",
1212
"@trpc/server": "^10.3.0",
13-
"plasmo": "^0.59.1",
13+
"plasmo": "^0.83.0",
1414
"react": "^18.2.0",
1515
"react-dom": "^18.2.0",
1616
"zod": "^3.19.1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { relay } from 'trpc-browser/relay';
2+
import { autoConnect } from 'trpc-browser/shared/chrome';
3+
4+
void autoConnect(chrome, (port) => {
5+
return relay(window, port); // return so it has the cleanup function for disconnect
6+
}).then(() => {
7+
return console.log('Content script loaded - bridge initialized');
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createTRPCProxyClient } from '@trpc/client';
2+
import type { PlasmoCSConfig } from 'plasmo';
3+
import type { FC } from 'react';
4+
import { windowLink } from 'trpc-browser/link';
5+
import type { AppRouter } from '~background';
6+
7+
console.log('inpage script loaded');
8+
9+
export const config: PlasmoCSConfig = {
10+
matches: ['<all_urls>'],
11+
world: 'MAIN',
12+
};
13+
14+
const windowClient = createTRPCProxyClient<AppRouter>({
15+
links: [/* 👉 */ windowLink({ window })],
16+
});
17+
18+
const ExtensionInpageUi: FC = () => {
19+
return (
20+
// a button to open google in a new tab
21+
<button
22+
style={{
23+
padding: 16,
24+
fontSize: 16,
25+
fontWeight: 'bold',
26+
borderRadius: 8,
27+
border: 'none',
28+
backgroundColor: '#000',
29+
color: '#fff',
30+
cursor: 'pointer',
31+
}}
32+
onClick={async () => {
33+
await windowClient.openNewTab.mutate({
34+
url: 'https://google.com',
35+
});
36+
}}
37+
>
38+
Open Google
39+
</button>
40+
);
41+
};
42+
43+
export default ExtensionInpageUi;

examples/with-plasmo/src/popup.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ function Popup() {
3030
flexDirection: 'column',
3131
padding: 16,
3232
gap: 8,
33+
34+
width: 420,
35+
height: 600,
3336
}}
3437
>
3538
<h2>Extension using tRPC & Plasmo</h2>

examples/with-plasmo/tsconfig.json

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
{
2-
"extends": "plasmo/templates/tsconfig.base",
3-
"exclude": ["node_modules"],
4-
"include": [".plasmo/**/*", "./**/*.ts", "./**/*.tsx"],
5-
"compilerOptions": {
6-
"strict": true,
7-
"baseUrl": ".",
8-
"paths": {
9-
"~*": ["./src/*"]
10-
}
11-
}
12-
}
1+
{
2+
"extends": "plasmo/templates/tsconfig.base",
3+
"exclude": ["node_modules"],
4+
"include": [".plasmo/index.d.ts", ".plasmo/**/*", "./**/*.ts", "./**/*.tsx"],
5+
"compilerOptions": {
6+
"strict": true,
7+
"baseUrl": ".",
8+
"ignoreDeprecations": "5.0",
9+
"paths": {
10+
"~*": ["./src/*"]
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)