1
0

iNotify.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ;(function (root, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (root.iNotify = factory());
  5. }(this, function (root, undefined) {
  6. var repeatableEffects = ['flash', 'scroll'],
  7. defaultNotification = {
  8. title: "通知!",
  9. body: '您来了一条新消息',
  10. openurl: ''
  11. },
  12. iconURL = "";
  13. function iNotify(config) {
  14. if (config) {
  15. this.init(config);
  16. }
  17. }
  18. iNotify.prototype = {
  19. init: function (config) {
  20. if (!config) {
  21. config = {};
  22. }
  23. this.interval = config.interval || 200; //响应时长
  24. this.effect = config.effect || 'flash'; //效果
  25. this.title = config.title || document.title; //标题
  26. this.message = config.message || this.title; //原来的标题
  27. this.onclick = config.onclick || this.onclick; //点击事件
  28. this.openurl = config.openurl || this.openurl; //点击事件
  29. this.updateFavicon = config.updateFavicon || {
  30. id: "favicon",
  31. textColor: "#fff",
  32. backgroundColor: "#2F9A00"
  33. }
  34. this.audio = config.audio || '';
  35. this.favicon = getFavicon(this.updateFavicon);
  36. this.favicon = this.favicon;
  37. this.cloneFavicon = this.favicon.cloneNode(true);
  38. defaultNotification.icon = iconURL = (config.notification && config.notification.icon) ? config.notification.icon : (config.icon ? config.icon : this.favicon.href);
  39. this.notification = config.notification || defaultNotification;
  40. //初始化生成声音文件节点
  41. if (this.audio && this.audio.file) {
  42. this.setURL(this.audio.file)
  43. }
  44. return this;
  45. },
  46. render: function () {
  47. switch (this.effect) {
  48. case 'flash':
  49. document.title = (this.title === document.title) ? this.message : this.title;
  50. break;
  51. case 'scroll':
  52. document.title = document.title.slice(1);
  53. if (0 === document.title.length) {
  54. document.title = this.message;
  55. }
  56. break;
  57. }
  58. },
  59. setURL: function (url) {
  60. if (url) {
  61. if (this.audioElm) {
  62. this.audioElm.remove();
  63. }
  64. this.audioElm = createAudio(url);
  65. document.body.appendChild(this.audioElm);
  66. }
  67. return this;
  68. },
  69. loopPlay: function () {
  70. this.setURL();
  71. this.audioElm.loop = true;
  72. this.player();
  73. return this;
  74. },
  75. stopPlay: function () {
  76. this.audioElm && (this.audioElm.loop = false, this.audioElm.pause());
  77. return this;
  78. },
  79. //播放声音
  80. player: function () {
  81. var adi = this.audio.file,
  82. source = null;
  83. if (!this.audio || !this.audio.file) {
  84. return;
  85. }
  86. if (!this.audioElm) {
  87. this.audioElm = createAudio(this.audio.file);
  88. document.body.appendChild(this.audioElm)
  89. }
  90. this.audioElm.play();
  91. return this;
  92. },
  93. notify: function (json) {
  94. var nt = this.notification;
  95. var url = json.openurl ? json.openurl : this.openurl;
  96. var onclick = json.onclick ? json.onclick : this.onclick;
  97. if (window.Notification) {
  98. if (json) {
  99. nt = jsonArguments(json, nt);
  100. } else {
  101. nt = defaultNotification;
  102. }
  103. var n = new Notification(nt.title, {
  104. icon: json.icon ? json.icon : iconURL,
  105. body: nt.body
  106. });
  107. n.onclick = function () {
  108. (onclick && typeof(onclick) === "function") && onclick(n);
  109. url && window.open(url);
  110. }
  111. }
  112. return this;
  113. },
  114. //是否许可弹框通知
  115. isPermission: function () {
  116. return window.Notification && Notification.permission === "granted" ? true : false;
  117. },
  118. //设置标题
  119. setTitle: function (str) {
  120. if (str === true) {
  121. if (0 <= repeatableEffects.indexOf(this.effect)) {
  122. return this.addTimer();
  123. }
  124. } else if (str) {
  125. this.message = str,
  126. this.addTimer();
  127. } else {
  128. this.clearTimer(),
  129. this.title = this.title;
  130. }
  131. return this;
  132. },
  133. //设置时间间隔
  134. setInterval: function (num) {
  135. if (num) {
  136. this.interval = num,
  137. this.addTimer();
  138. }
  139. return this;
  140. },
  141. //设置网页Icon
  142. setFavicon: function (num) {
  143. if (!num && num !== 0) {
  144. return this.faviconClear();
  145. }
  146. var oldicon = document.getElementById('new' + this.updateFavicon.id)
  147. if (this.favicon) {
  148. this.favicon.remove();
  149. }
  150. if (oldicon) {
  151. oldicon.remove();
  152. }
  153. changeFavicon(num, this.updateFavicon)
  154. return this;
  155. },
  156. //添加计数器
  157. addTimer: function () {
  158. this.clearTimer();
  159. if (0 <= repeatableEffects.indexOf(this.effect)) {
  160. this.timer = setInterval(this.render.bind(this), this.interval);
  161. }
  162. return this;
  163. },
  164. //清除Icon
  165. faviconClear: function () {
  166. var newicon = document.getElementById('new' + this.updateFavicon.id),
  167. head = document.getElementsByTagName('head')[0],
  168. ficon = document.querySelectorAll('link[rel~=shortcut]');
  169. newicon && newicon.remove();
  170. if (ficon.length > 0) {
  171. for (var i = 0; i < ficon.length; i++) {
  172. ficon[i].remove()
  173. }
  174. }
  175. head.appendChild(this.cloneFavicon);
  176. iconURL = this.cloneFavicon.href;
  177. this.favicon = this.cloneFavicon;
  178. return this;
  179. },
  180. //清除计数器
  181. clearTimer: function () {
  182. clearInterval(this.timer);
  183. document.title = this.title;
  184. return this;
  185. }
  186. };
  187. // 获取 favicon
  188. function getFavicon(setting) {
  189. var ic = document.querySelectorAll('link[rel~=shortcut]')[0];
  190. if (!ic) {
  191. ic = changeFavicon('O', setting);
  192. }
  193. return ic;
  194. }
  195. function createAudio(url) {
  196. var audioElm = document.createElement('audio'),
  197. source;
  198. if (isArray(url) && url.length > 0) {
  199. for (var i = 0; i < url.length; i++) {
  200. source = document.createElement('source');
  201. source.src = url[i];
  202. source.type = 'audio/' + getExtension(url[i]);
  203. audioElm.appendChild(source);
  204. }
  205. } else {
  206. audioElm.src = url;
  207. }
  208. return audioElm;
  209. }
  210. function isArray(value) {
  211. return Object.prototype.toString.call(value) === '[object Array]';
  212. }
  213. function changeFavicon(num, settings) {
  214. var canvas = document.createElement('canvas'),
  215. img = document.createElement('img'),
  216. head = document.getElementsByTagName('head')[0],
  217. linkTag = document.createElement('link'),
  218. ctx;
  219. canvas.height = canvas.width = 32;
  220. ctx = canvas.getContext('2d');
  221. ctx.fillStyle = settings.backgroundColor;
  222. ctx.fillRect(0, 0, 32, 32);
  223. ctx.textAlign = "center";
  224. ctx.font = '22px "helvetica", sans-serif';
  225. ctx.fillStyle = settings.textColor;
  226. ctx.fillText(num, 16, 24);
  227. //生成到
  228. linkTag.setAttribute('rel', 'shortcut icon');
  229. linkTag.setAttribute('type', 'image/x-icon');
  230. linkTag.setAttribute('id', 'new' + settings.id);
  231. linkTag.setAttribute('href', canvas.toDataURL('image/png'));
  232. iconURL = canvas.toDataURL('image/png');
  233. return head.appendChild(linkTag);
  234. };
  235. //获取文件后缀
  236. function getExtension(file_name) {
  237. return file_name.match(/\.([^\.]+)$/)[1];
  238. }
  239. function jsonArguments(news, olds) {
  240. for (var a in olds) {
  241. if (news[a]) {
  242. olds[a] = news[a]
  243. }
  244. }
  245. return olds
  246. }
  247. //提醒是否添加chrome通知
  248. if (window.Notification && window.Notification.permission !== "granted") {
  249. window.Notification.requestPermission();
  250. }
  251. return iNotify;
  252. }));