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

hcfyapp/chrome-call

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chrome call

Build Status Coverage Status dependencies Status devDependencies Status NPM Version

A really simple way to call the original chrome javascript API and return a Promise.

Install

npm i -S chrome-call

Usage

When you do:

var promise = new Promise(function(resolve,reject) {
  chrome.storage.local.get('key',function(value) {
    if(chrome.runtime.lastError){
      reject(chrome.runtime.lastError);
    }else{
      resolve(value);
    }
  });
});

It's equal to:

var promise = chromeCall('storage.local.get','key');

That's really simple, right?

Multiple arguments in callback

Most of chrome API only have one argument in callback, but someone not, such as chrome.hid.receive. In this situation, the value of promise will be an (real) Array:

chromeCall('hid.receive',connectionId)
  .then(function(args){
    Array.isArray(args); // true
    var reportId = args[0];
    var data = args[1];
  });

Scope

The global chromeCall search function on window.chrome, but you can use different scope:

var local = chromeCall.scope('storage.local'); // or chromeCall.scope(chrome.storage.local)
var promise = local('get','key');

License

MIT

About

Call the original chrome javascript API and return a Promise.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published