├── README.md ├── LICENSE └── WebNotification.js /README.md: -------------------------------------------------------------------------------- 1 | WebNotification.js 2 | =================== 3 | 4 | Wrapper for the W3C Web Notifications API, to make the faulty WebKit implementations work as expected. 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Kenneth Auchenberg 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /WebNotification.js: -------------------------------------------------------------------------------- 1 | /** @license 2 | * WebNotification.js 3 | * Released under the MIT license 4 | * Author: Kenneth Auchenberg 5 | * Version: 0.1.0 6 | */ 7 | /* global define */ 8 | 9 | 10 | ;(function(window) { 11 | 12 | 'use strict'; 13 | 14 | var NativeNotification = window.Notification, 15 | PrefixedNotification = window.webkitNotifications; 16 | 17 | var utils = { 18 | isFunction: function(obj) { 19 | return typeof obj === 'function'; 20 | } 21 | }; 22 | 23 | function WebNotification(title, options) { 24 | return new NativeNotification(title, options); 25 | } 26 | 27 | WebNotification.getPermission = function() { 28 | 29 | if (NativeNotification) { 30 | 31 | // Official W3C/WHATWG Web Notifications API 32 | if (NativeNotification.permission) { 33 | return NativeNotification.permission; 34 | } 35 | 36 | // Older WebKit API 37 | if (utils.isFunction(NativeNotification.permissionLevel)) { 38 | return NativeNotification.permissionLevel(); 39 | } 40 | } 41 | 42 | // Oldprefixed WebKit API 43 | if (PrefixedNotification && utils.isFunction(PrefixedNotification.checkPermission)) { 44 | switch (PrefixedNotification.checkPermission()) { 45 | case 0: 46 | return 'granted'; 47 | case 1: 48 | return 'default'; 49 | case 2: 50 | return 'denied'; 51 | } 52 | } 53 | 54 | return 'default'; 55 | }; 56 | 57 | WebNotification.requestPermission = function(callback) { 58 | var context = this; 59 | 60 | function _onPermissionRequested() { 61 | context.permission = context.getPermission(); 62 | 63 | if (callback) { 64 | callback.call(context); 65 | } 66 | } 67 | 68 | // Native first, then prefxied 69 | if (NativeNotification && utils.isFunction(NativeNotification.requestPermission)) { 70 | NativeNotification.requestPermission(_onPermissionRequested); 71 | } else if (PrefixedNotification && utils.isFunction(PrefixedNotification.requestPermission)) { 72 | PrefixedNotification.requestPermission(_onPermissionRequested); 73 | } else { 74 | throw 'Could not call requestPermission'; 75 | } 76 | 77 | }; 78 | 79 | WebNotification.permission = WebNotification.getPermission(); 80 | 81 | if (typeof define === 'function' && define.amd) { 82 | define(function() { 83 | if('Notification' in window) { 84 | window.Notification = WebNotification; 85 | return WebNotification; 86 | } 87 | }); 88 | } else { 89 | if('Notification' in window) { 90 | window.Notification = WebNotification; 91 | } 92 | } 93 | 94 | })(this); 95 | --------------------------------------------------------------------------------