Notification.js

Notification.js by Andrew Dodson

当前为 2017-10-23 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/34465/225714/Notificationjs.js

  1. /**
  2. * Notification JS
  3. * Shims up the Notification API
  4. *
  5. * @author Andrew Dodson
  6. * @website http://adodson.com/notification.js/
  7. */
  8.  
  9. //
  10. // Does the browser support the the Notification API?
  11. // .. and does it have a permission property?
  12. //
  13.  
  14. (function(window, document){
  15.  
  16. var PERMISSION_GRANTED = 'granted',
  17. PERMISSION_DENIED = 'denied',
  18. PERMISSION_UNKNOWN = 'unknown';
  19. var a = [], iv, i=0;
  20.  
  21. //
  22. // Swap the document.title with the notification
  23. //
  24. function swaptitle(title){
  25. if(a.length===0){
  26. a = [document.title];
  27. }
  28.  
  29. a.push(title);
  30.  
  31. if(!iv){
  32. iv = setInterval(function(){
  33.  
  34. // has document.title changed externally?
  35. if(a.indexOf(document.title) === -1 ){
  36. // update the default title
  37. a[0] = document.title;
  38. }
  39. document.title = a[++i%a.length];
  40. }, 1000);
  41. }
  42. }
  43.  
  44. function swapTitleCancel(){
  45.  
  46. // dont do any more if we haven't got anything open
  47. if(a.length===0){
  48. return;
  49. }
  50. // if an IE overlay is present, kill it
  51. if("external" in window && "msSiteModeClearIconOverlay" in window.external ){
  52. window.external.msSiteModeClearIconOverlay();
  53. }
  54.  
  55. clearInterval(iv);
  56. iv = false;
  57. document.title = a[0];
  58. a = [];
  59. }
  60. //
  61. // Add aevent handlers
  62. function addEvent(el,name,func){
  63. if(name.match(" ")){
  64. var a = name.split(' ');
  65. for(var i=0;i<a.length;i++){
  66. addEvent( el, a[i], func);
  67. }
  68. }
  69. if(el.addEventListener){
  70. el.removeEventListener(name, func, false);
  71. el.addEventListener(name, func, false);
  72. }
  73. else {
  74. el.detachEvent('on'+name, func);
  75. el.attachEvent('on'+name, func);
  76. }
  77. }
  78.  
  79.  
  80. function check_permission(){
  81. // Check whether the current desktop supports notifications and if they are authorised,
  82. // PERMISSION_GRANTED (yes they are supported and permission is granted),
  83. // PERMISSION_DENIED (yes they are supported, permission has not been granted),
  84. // -1 (Notifications are not supported)
  85. // IE9
  86. if(("external" in window) && ("msIsSiteMode" in window.external)){
  87. return window.external.msIsSiteMode()? PERMISSION_GRANTED : PERMISSION_UNKNOWN;
  88. }
  89. else if("webkitNotifications" in window){
  90. return window.webkitNotifications.checkPermission() === 0 ? PERMISSION_GRANTED : PERMISSION_DENIED;
  91. }
  92. else if("mozNotification" in window.navigator){
  93. return PERMISSION_GRANTED;
  94. }
  95. else {
  96. return PERMISSION_UNKNOWN;
  97. }
  98. }
  99.  
  100. function update_permission(){
  101. // Define the current state
  102. window.Notification.permission = check_permission();
  103. return window.Notification.permission;
  104. }
  105.  
  106.  
  107. if(!Object(window.Notification).permission){
  108.  
  109. //
  110. // Bind event handlers to the body
  111. addEvent(window, "focus scroll click", swapTitleCancel);
  112.  
  113. // Assign it.
  114. window.Notification = function(message, options){
  115.  
  116. // ensure this is an instance
  117. if(!(this instanceof window.Notification)){
  118. return new window.Notification(message,options);
  119. }
  120.  
  121. var n, self = this;
  122.  
  123. //
  124. options = options || {};
  125.  
  126. this.body = options.body || '';
  127. this.icon = options.icon || '';
  128. this.lang = options.lang || '';
  129. this.tag = options.tag || '';
  130. this.close = function(){
  131.  
  132. // remove swapTitle
  133. swapTitleCancel();
  134.  
  135. // Close
  136. if(Object(n).close){
  137. n.close();
  138. }
  139.  
  140. self.onclose();
  141. };
  142. this.onclick = function(){};
  143. this.onclose = function(){};
  144.  
  145. //
  146. // Swap document.title
  147. //
  148. swaptitle(message);
  149.  
  150. //
  151. // Create Desktop Notifications
  152. //
  153. if(("external" in window) && ("msIsSiteMode" in window.external)){
  154. if(window.external.msIsSiteMode()){
  155. window.external.msSiteModeActivate();
  156. if(this.icon){
  157. window.external.msSiteModeSetIconOverlay(this.icon, message);
  158. }
  159. }
  160. }
  161. else if("webkitNotifications" in window){
  162. if(window.webkitNotifications.checkPermission() === 0){
  163. n = window.webkitNotifications.createNotification(this.icon, message, this.body );
  164. n.show();
  165. n.onclick = function(){
  166.  
  167. // Fire any user bound events to the onclick function
  168. self.onclick();
  169.  
  170. // redirect the user back to the page
  171. window.focus();
  172. setTimeout( function(){ n.cancel(); }, 1000);
  173. };
  174. }
  175. }
  176. else if( "mozNotification" in window.navigator ){
  177. var m = window.navigator.mozNotification.createNotification( message, this.body, this.icon );
  178. m.show();
  179. }
  180. };
  181.  
  182. window.Notification.requestPermission = function(cb){
  183. // Setup
  184. // triggers the authentication to create a notification
  185. cb = cb || function(){};
  186. // IE9
  187. if(("external" in window) && ("msIsSiteMode" in window.external)){
  188. try{
  189. if( !window.external.msIsSiteMode() ){
  190. window.external.msAddSiteMode();
  191. cb( PERMISSION_UNKNOWN );
  192. }
  193. }
  194. catch(e){}
  195. cb( update_permission() );
  196. }
  197. else if("webkitNotifications" in window){
  198. window.webkitNotifications.requestPermission(function(){
  199. cb( update_permission() );
  200. });
  201. }
  202. else {
  203. cb( update_permission() );
  204. }
  205. };
  206.  
  207. // Get the current permission
  208. update_permission();
  209. }
  210. })(window, document);