/*! * @wcjiang/notify v2.0.11 * JS achieve the browser title flashing , scrolling, voice prompts , chrome notice. * * Copyright (c) 2018 kenny wang * http://jaywcjlove.github.io/iNotify * * Licensed under the MIT license. */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Notify = factory()); }(this, (function () { 'use strict'; // 提醒是否添加chrome通知 if (window.Notification && window.Notification.permission !== 'granted') { window.Notification.requestPermission(); } var iconURL = ''; var repeatableEffects = ['flash', 'scroll']; var defaultNotification = { title: 'iNotify !', body: 'You have a new message.', openurl: '' }; function jsonArguments(news, olds) { for (var a in olds) { if (news[a]) { olds[a] = news[a]; } } return olds; } function isArray(value) { return Object.prototype.toString.call(value) === '[object Array]'; } function createAudio(url) { var audioElm = document.createElement('audio'); var source = void 0; if (isArray(url) && url.length > 0) { for (var i = 0; i < url.length; i++) { source = document.createElement('source'); source.src = url[i]; source.type = 'audio/' + getExtension(url[i]); audioElm.appendChild(source); } } else { audioElm.src = url; } return audioElm; } function getFavicon(setting) { var ic = document.querySelectorAll('link[rel~=shortcut]')[0]; if (!ic) { ic = changeFavicon('O', setting); } return ic; } function getExtension(fileName) { return fileName.match(/\.([^\\.]+)$/)[1]; } function changeFavicon(num, settings) { var canvas = document.createElement('canvas'); var head = document.getElementsByTagName('head')[0]; var linkTag = document.createElement('link'); var ctx = null; canvas.height = 32; canvas.width = 32; ctx = canvas.getContext('2d'); ctx.fillStyle = settings.backgroundColor; ctx.fillRect(0, 0, 32, 32); ctx.textAlign = 'center'; ctx.font = '22px "helvetica", sans-serif'; ctx.fillStyle = settings.textColor; num && ctx.fillText(num, 16, 24); // 生成到 linkTag.setAttribute('rel', 'shortcut icon'); linkTag.setAttribute('type', 'image/x-icon'); linkTag.setAttribute('id', 'new' + settings.id); linkTag.setAttribute('href', canvas.toDataURL('image/png')); iconURL = canvas.toDataURL('image/png'); return head.appendChild(linkTag); } function Notify(config) { if (config) { this.init(config); } } Notify.prototype = { init: function init(config) { if (!config) { config = {}; } this.interval = config.interval || 100; // 响应时长 this.effect = config.effect || 'flash'; // 效果 this.title = config.title || document.title; // 标题 this.message = config.message || this.title; // 原来的标题 this.onclick = config.onclick || this.onclick; // 点击事件 this.openurl = config.openurl || this.openurl; // 点击事件 this.updateFavicon = config.updateFavicon || { id: 'favicon', textColor: '#fff', backgroundColor: '#2F9A00' }; this.audio = config.audio || ''; this.favicon = getFavicon(this.updateFavicon); this.cloneFavicon = this.favicon.cloneNode(true); iconURL = config.notification && config.notification.icon ? config.notification.icon : config.icon ? config.icon : this.favicon.href; defaultNotification.icon = iconURL; this.notification = config.notification || defaultNotification; // 初始化生成声音文件节点 if (this.audio && this.audio.file) { this.setURL(this.audio.file); } return this; }, render: function render() { if (this.effect === 'flash') { document.title = this.title === document.title ? this.message : this.title; } else if (this.effect === 'scroll') { var title = this.message || document.title; if (!this.scrollTitle || !this.scrollTitle.slice(1)) { document.title = title; this.scrollTitle = title; } else { this.scrollTitle = this.scrollTitle.slice(1); document.title = this.scrollTitle; } } return this; }, // 设置标题 setTitle: function setTitle(str) { if (str === true) { if (repeatableEffects.indexOf(this.effect) >= 0) { return this.addTimer(); } } else if (str) { this.message = str; this.scrollTitle = ''; this.addTimer(); } else { this.clearTimer(); } return this; }, setURL: function setURL(url) { if (url) { if (this.audioElm) { this.audioElm.remove(); } this.audioElm = createAudio(url); document.body.appendChild(this.audioElm); } return this; }, loopPlay: function loopPlay() { this.setURL(); this.audioElm.loop = true; this.player(); return this; }, stopPlay: function stopPlay() { this.audioElm && (this.audioElm.loop = false, this.audioElm.pause()); return this; }, // 播放声音 player: function player() { if (!this.audio || !this.audio.file) { return; } if (!this.audioElm) { this.audioElm = createAudio(this.audio.file); document.body.appendChild(this.audioElm); } this.audioElm.play(); return this; }, notify: function notify(json) { var nt = this.notification; var url = json.openurl ? json.openurl : this.openurl; var onclick = json.onclick ? json.onclick : this.onclick; if (window.Notification) { if (json) { nt = jsonArguments(json, nt); } else { nt = defaultNotification; } var option = {}; option.icon = json.icon ? json.icon : iconURL; option.body = nt.body; if (json.dir) option.dir = json.dir; var n = new Notification(nt.title, option); n.onclick = function () { onclick && typeof onclick === 'function' && onclick(n); url && window.open(url); }; n.onshow = function () { json.onshow && typeof json.onshow === 'function' && json.onshow(n); }; n.onclose = function () { json.onclose && typeof json.onclose === 'function' && json.onclose(n); }; n.onerror = function () { json.onerror && typeof json.onerror === 'function' && json.onerror(n); }; this.Notifiy = n; } return this; }, // 是否许可弹框通知 isPermission: function isPermission() { return window.Notification && Notification.permission === 'granted'; }, // 设置时间间隔 setInterval: function setInterval(num) { if (num) { this.interval = num; this.addTimer(); } return this; }, // 设置网页Icon setFavicon: function setFavicon(num) { if (!num && num !== 0) { return this.faviconClear(); } var oldicon = document.getElementById('new' + this.updateFavicon.id); if (this.favicon) { this.favicon.remove(); } if (oldicon) { oldicon.remove(); } this.updateFavicon.num = num; changeFavicon(num, this.updateFavicon); return this; }, // 设置 Favicon 文字颜色 setFaviconColor: function setFaviconColor(color) { if (color) { this.faviconRemove(); this.updateFavicon.textColor = color; changeFavicon(this.updateFavicon.num, this.updateFavicon); } return this; }, // 设置 Favicon 背景颜色 setFaviconBackgroundColor: function setFaviconBackgroundColor(color) { if (color) { this.faviconRemove(); this.updateFavicon.backgroundColor = color; changeFavicon(this.updateFavicon.num, this.updateFavicon); } return this; }, faviconRemove: function faviconRemove() { this.faviconClear(); var oldicon = document.getElementById('new' + this.updateFavicon.id); if (this.favicon) { this.favicon.remove(); } if (oldicon) { oldicon.remove(); } }, // 添加计数器 addTimer: function addTimer() { this.clearTimer(); if (repeatableEffects.indexOf(this.effect) >= 0) { this.timer = setInterval(this.render.bind(this), this.interval); } return this; }, close: function close() { if (this.Notifiy) this.Notifiy.close(); }, // 清除Icon faviconClear: function faviconClear() { var newicon = document.getElementById('new' + this.updateFavicon.id); var head = document.getElementsByTagName('head')[0]; var ficon = document.querySelectorAll('link[rel~=shortcut]'); newicon && newicon.remove(); if (ficon.length > 0) { for (var i = 0; i < ficon.length; i++) { ficon[i].remove(); } } head.appendChild(this.cloneFavicon); iconURL = this.cloneFavicon.href; this.favicon = this.cloneFavicon; return this; }, // 清除计数器 clearTimer: function clearTimer() { this.timer && clearInterval(this.timer); document.title = this.title; return this; } }; return Notify; })));

notify.min.js的更多相关文章

  1. jquery.qrcode.min.js生成二维码 通过前端实现二维码生成

    主体代码: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <tit ...

  2. JavaScript--jquery.min.js文件

    /*! jQuery v1.12.3 | (c) jQuery Foundation | jquery.org/license */ !function(a,b){"object" ...

  3. Mvc 之System.Web.Optimization 压缩合并如何让*.min.js 脚本不再压缩

    最近项目中用到了easy ui ,但是在配置BundleConfig 的时候出现了问题,easy ui的脚本jquery.easyui.min.js 压缩后出现各种脚本错误,总是莫名其妙的 i标量错误 ...

  4. html5shiv.js and respond.min.js

    因为用到这两个插件,所以记录下来.. html5shiv:解决ie9以下浏览器对html5新增标签的不识别,并导致CSS不起作用的问题. respond.min:让不支持Css3 Media Quer ...

  5. min.js反压缩

    给个网址自己体会.. http://jsbeautifier.org/ 当需要修改min.js中的代码时,把min.js文件ctrl+c   ctrl+v扔到上面的网页里,点击beautify 即可

  6. 让IE系列支持HTML5的html5shiv.js和respond.min.js

    HTML5越来越成为主流,被广大搜索引擎所使用,但IE对HTML5的支持却常被人唾弃. 解决方案有两种: 1.为网站创建多套模板,通过程序对User-Agent的判断给不同的浏览器用户显示不同的页面, ...

  7. MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案

    1.选中报错的jquery文件例如"jquery-1.8.0.min.js". 2.右键选择 MyEclipse-->Exclude From Validation . 3. ...

  8. 项目里的jquery.min.js错误

    项目里的jquery.min.js报一系列 - Missing semicolon - Missing semicolon - Missing semicolon - Missing semicolo ...

  9. spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...

    问题:spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js... web.x ...

随机推荐

  1. fft,ntt

    在被两题卡了常数之后,花了很久优化了自己的模板 现在的一般来说任意模数求逆1s跑3e5,exp跑1e5是没啥问题的(自己电脑,可能比luogu慢一倍) 当模数是$998244353,100453580 ...

  2. Integer Replacement

    https://leetcode.com/problems/integer-replacement/#/solutions 这题是一道典型的搜索问题,我采用广度搜索,可以直接输出最短路径.这题的tes ...

  3. java自动化-junit框架简述

    本人使用的是java的junit框架来组织的自动化测试,故我这边需要简单介绍一下junit框架 首先,建议自行百度一下junit框架,先有一个大概的了解 所谓的接口自动化测试,会对多个接口中每一个接口 ...

  4. 跑的飞快的dinic

    orz kczno1 目前还是不知道怎么卡,也不会证明复杂度是正确的 其实我感觉卡不了

  5. Sting、StringBuffer、StringBuilder

    (1)String是字符串常量,一旦创建之后不可更改:StringBuffer和StringBuilder是字符串变量,可以更改.String的不可变,所以适合作为Map的键. (2)StringBu ...

  6. 【安全性测试】一个简单地绕前端暴XSS漏洞

    在appscan暴出一个关于跨站点脚本编制的漏洞,但是appscan并不能完整地显示该漏洞.于是,工具是否出现误报,需要通过自己手工验证. 然后,我们需要找到目标参数的包并分析是从哪个步骤提交给服务器 ...

  7. 3.2station

    题意火车进入,然后要求以最大字典序输出 解题思路:先用一个数组储存当前位置之后最大的数字是什么,然后把已经进站的最后一节火车和还没进站的车的最大值,哪个更大,如果已经进站的更大,那么就先出站,否则就先 ...

  8. [LeetCode] Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  9. [EOJ Monthly 2018.10][C. 痛苦的 01 矩阵]

    题目链接:C. 痛苦的 01 矩阵 题目大意:原题说的很清楚了,不需要简化_(:з」∠)_ 题解:设\(r_i\)为第\(i\)行中0的个数,\(c_j\)为第\(j\)列中0的个数,\(f_{i,j ...

  10. JBPM工作流(八)——流程实例(PI)Process Instance

    /** * 流程实例 *    *  启动流程实例 *    *  完成任务 *    *  查询 *      *  查询流程实例 *      *  查询任务 *          *  查询正在 ...