Skip to content

Commit

Permalink
Merge pull request #1 from kai918/edge-support
Browse files Browse the repository at this point in the history
Handle the browsers which don't support promises (eg. Edge)
  • Loading branch information
kai918 authored Jul 13, 2017
2 parents a1fd64c + d36f7d7 commit f4e2720
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/browser-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

if (typeof browser === "undefined") {
var _supportsPromises = false;
try {
_supportsPromises = browser.runtime.getPlatformInfo() instanceof Promise;
} catch (e) {
}

if (typeof browser === "undefined" || !_supportsPromises) {
var browser = window.browser || window.msBrowser || window.chrome;

// Wrapping the bulk of this polyfill in a one-time-use function is a minor
// optimization for Firefox. Since Spidermonkey does not fully parse the
// contents of a function until the first time it's called, and since it will
Expand Down Expand Up @@ -62,7 +70,7 @@ if (typeof browser === "undefined") {
* Creates and returns a function which, when called, will resolve or reject
* the given promise based on how it is called:
*
* - If, when called, `chrome.runtime.lastError` contains a non-null object,
* - If, when called, `browser.runtime.lastError` contains a non-null object,
* the promise is rejected with that value.
* - If the function is called with exactly one argument, the promise is
* resolved to that value.
Expand All @@ -82,8 +90,8 @@ if (typeof browser === "undefined") {
*/
const makeCallback = promise => {
return (...callbackArgs) => {
if (chrome.runtime.lastError) {
promise.reject(chrome.runtime.lastError);
if (browser.runtime.lastError) {
promise.reject(browser.runtime.lastError);
} else if (callbackArgs.length === 1) {
promise.resolve(callbackArgs[0]);
} else {
Expand Down Expand Up @@ -340,7 +348,7 @@ if (typeof browser === "undefined") {
},
};

return wrapObject(chrome, staticWrappers, apiMetadata);
return wrapObject(browser, staticWrappers, apiMetadata);
};

// The build process adds a UMD wrapper around this file, which makes the
Expand Down

0 comments on commit f4e2720

Please sign in to comment.