browser-notification
Version:
Small library built around browsers native Notification-API adding useful default behaviour.
176 lines (142 loc) • 4.88 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["BrowserNotification"] = factory();
else
root["BrowserNotification"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var settings = {
ignoreFocused: true,
timeout: 0,
cooldown: 0
};
var available = false;
var cooldownActive = false;
var focused = true;
function initNotifications(options) {
// Handle old browsers - this way we can skip polyfill Promise and Object.assign
if (!window.hasOwnProperty('Notification')) {
console.info('This browser does not support notifications');
// Mock promise always resolving to false
return {
then: function then(fn) {
return fn(false);
}
};
}
settings = Object.assign({}, settings, options);
window.onfocus = function () {
focused = true;
};
window.onblur = function () {
focused = false;
};
var availablePromise = new Promise(function (resolve, reject) {
if (Notification.permission === 'granted') {
resolve(true);
} else if (Notification.permission !== 'denied') {
Notification.requestPermission().then(function (permission) {
if (permission === 'granted') {
resolve(true);
}
resolve(false);
});
} else {
resolve(false);
}
});
availablePromise.then(function (result) {
available = result;
});
return availablePromise;
}
function notify(title, notifyOptions) {
if (!available || settings.ignoreFocused && focused || cooldownActive) {
return null;
}
var notification = new Notification(title, notifyOptions);
notification.onclick = function () {
window.focus();
notification.close();
};
if (settings.timeout !== 0) {
window.setTimeout(function () {
notification.close();
}, settings.timeout);
}
if (settings.cooldown !== 0) {
cooldownActive = true;
window.setTimeout(function () {
cooldownActive = false;
}, settings.cooldown);
}
return notification;
}
exports.initNotifications = initNotifications;
exports.notify = notify;
/***/ })
/******/ ]);
});