chrome-ext-async
v0.0.16
Published
This module is a typed promise wrapper for the Chrome Extension API that has(or lacks) the following features: - Does not contain a locally modified copy of @types/chrome. References @types/chrome so you can stay up to date with any fixes. - Original para
Downloads
21
Readme
This module is a typed promise wrapper for the Chrome Extension API that has(or lacks) the following features:
- Does not contain a locally modified copy of @types/chrome. References @types/chrome so you can stay up to date with any fixes.
- Original parameter types are maintained including overloads.
- Does not monkey patch the existing Chrome API.
Installation
Run npm install chrome-ext-async
Usage
You can use this module in a variety of ways:
- Promisify the entire Chrome API(but only functions that accept callbacks):
import { asyncifyAll } from 'chrome-ext-async'
const as = asyncifyAll()
interface Response {
type: string
interestingData: any
}
async function main() {
const cpuInfo = await as.system.cpu.getInfo()
const response = await as.runtime.sendMessage('msg')
// You can also set the promise's return type. It should extend the type of the callback parameter. For chrome.runtime.sendMessage it is 'any'.
const response = await as.runtime.sendMessage<Response>('msg')
}
- Promisify only the functions you need for your project. Values should be a Chrome API function that accepts a callback:
import { asyncifyObj } from 'chrome-ext-async'
const customApi = asyncifyObj({
r : {
sendMessage: chrome.runtime.sendMessage
},
t : {
sendMessage: chrome.tabs.sendMessage
}
})
async function main() {
const res = await customApi.r.sendMessage('message')
const tabRes = await customApi.t.sendMessage(0, 'message')
}
- Promisify a single function:
import { asyncifyChromeFn } from 'chrome-ext-async'
async function main() {
const res = await asyncifyChromeFn(chrome.runtime.sendMessage)('message')
}
To do
- Write tests. Use in production at your own risk.
- Remove callback parameter from the function type.