Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

getChromeExtensionStatus() does not follow the standard NodeJS conventions #6

Open
prbaron opened this issue Nov 8, 2017 · 1 comment

Comments

@prbaron
Copy link

prbaron commented Nov 8, 2017

According to NodeJS conventions, the callback should be error-first, meaning getChromeExtensionStatus() should return a callback with an error as the first value and the status as the second value.

I am using Bluebird's promisify function with getChromeExtensionStatus() and I have to use .catch because of that.

Promise.promisify(window.getChromeExtensionStatus)()
  .catch(function(error) {
    console.log(error.message) // will get me the status
});

here is my suggestion

window.getChromeExtensionStatus = function(callback) {
    // for Firefox:
    if (!!navigator.mozGetUserMedia) {
      callback(new Error('not-chrome'), null);
      return;
    }

    window.addEventListener('message', onIFrameCallback);

    function onIFrameCallback(event) {
      if (!event.data) return;

      if (event.data.chromeExtensionStatus) {
        if (event.data.chromeExtensionStatus === 'installed-enabled') {
          callback(null, event.data.chromeExtensionStatus);
        } else {
          callback(new Error(event.data.chromeExtensionStatus), null);
        }
      }

      // this event listener is no more needed
      window.removeEventListener('message', onIFrameCallback);
    }

    setTimeout(postGetChromeExtensionStatusMessage, 100);
  };
@prbaron
Copy link
Author

prbaron commented Nov 8, 2017

I realise it is quite opinionated in the way that it does not return a status but it will provide an error if the plugin is not installed and enabled.

Another solution could be

window.getChromeExtensionStatus = function(callback) {
    // for Firefox:
    if (!!navigator.mozGetUserMedia) {
      callback(null, 'not-chrome');
      return;
    }

    window.addEventListener('message', onIFrameCallback);

    function onIFrameCallback(event) {
      if (!event.data) return;

      if (event.data.chromeExtensionStatus) {
          callback(null, event.data.chromeExtensionStatus);
      }

      // this event listener is no more needed
      window.removeEventListener('message', onIFrameCallback);
    }

    setTimeout(postGetChromeExtensionStatusMessage, 100);
  };

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant