This is a lightweight and efficient RPC (Remote Procedure Call) library designed specifically for Chrome extensions to facilitate full type safe communication between content scripts, popup, options, and background workers.
- Simple and intuitive API
- Lightweight and fast
To install the RPC library, you can use npm:
npm install @x10sion/rpc
Or if you prefer using yarn:
yarn add @x10sion/rpc
Here is a basic example of how to use the RPC library in a Chrome extension:
import { createRouter, handleRPC } from '@x10sion/rpc'
const userControllers = {
signin: async ({
username,
password
}: {
username: string
password: string
}) => {
console.log('signin', username, password)
return Promise.resolve({ username, msg: 'signin done' })
},
signout: async () => {
console.log('signout')
return Promise.resolve({ msg: 'signout done' })
}
}
export const userRouter = createRouter(userControllers)
const controllers = {
p: async ({ a, b }: { a: number; b: number }) => {
return Promise.resolve(a + b)
},
user: userRouter
}
const rootRouter = createRouter(controllers)
handleRPC(rootRouter, chrome)
import { bgCaller } from '@x10sion/rpc'
export const rpc = bgCaller((message, resolve, reject) => {
chrome.runtime.sendMessage(message, (response) => {
if (response.error) {
reject(new Error(response.error))
} else {
resolve(response.data)
}
})
})
// Usage in a React component
import React, { useEffect } from 'react'
import { rpc } from '@/utils/bgCaller'
export default function Content(): JSX.Element {
useEffect(() => {
;(async () => {
try {
const res = await rpc('user.signin', {
username: 'admin',
password: 'admin'
})
console.log(res)
} catch (e) {
console.error(e)
}
})()
}, [])
return <div>{/* Your component JSX */}</div>
}
We welcome contributions to the RPC library. To contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes and commit them with a clear message.
- Push your changes to your fork.
- Create a pull request to the main repository.
This project is licensed under the MIT License. See the LICENSE file for details.