Noti test

test

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

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

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