0.0.16 • Published 3 years ago

chrome-ext-async v0.0.16

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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:

  1. 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')
}
  1. 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')
}
  1. 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.
0.0.15

3 years ago

0.0.16

3 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago