Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

hcfyapp/chrome-call

Repository files navigation

chrome-call

Build Status Coverage Status dependencies Status devDependencies Status NPM Version

Call the original chrome javascript API and return a Promise.

Install

Use with Webpack

If you build your project with webpack, then you can install chrome-call from npm:

npm install chrome-call 

then you can import it in your project:

// es6 import chromeCall from 'chrome-call' // commonjs const chromeCall = require('chrome-call')

Use with <script>

Download chrome-call.js from unpkg(min version), then reference it in your html:

<script src="path/to/chrome-call.js"></script> <!-- now you will get a global variable named chromeCall --> <sciprt> typeof chromeCall // function </sciprt>

Usage

When you do:

const promise = new Promise((resolve, reject) => { chrome.storage.local.get('key', items => { if(chrome.runtime.lastError){ reject(chrome.runtime.lastError) } else { resolve(items) } }) })

It's equal to:

const promise = chromeCall(chrome.storage.local, 'get', 'key')

That's really simple, right?

Some Chrome API methods has more than one arguments, then pass an array of params for the third argument:

chromeCall(chrome.tabs, 'sendMessage', [tabId, message, options])

Multiple arguments in callback

Most of chrome API only has zero or one argument in callback, but someone not, such as chrome.hid.receive.

In this situation, pass true as the fourth argument, then the value of promise will be an real Array:

import chromeCall from 'chrome-call' chromeCall(chrome.hid, 'receive', connectionId, true) .then(args => { Array.isArray(args) // true const reportId = args[0] const data = args[1] })

License

MIT

About

Call the original chrome javascript API and return a Promise.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published