JS 實現瀏覽器通知

jaywcjlove 9年前發布 | 38K 次閱讀 開源 JavaScript開發 JS

JS 實現瀏覽器的 title 閃爍、滾動、聲音提示、chrome、Firefox、Safari等系統通知。

這是重復造輪子...,標題閃爍、或者滾動提示,favicon數字顯示。打開chrome瀏覽器調試工具,按照下面截圖的方式放到調試里面調用一下,你就可以看到效果了。




下載


npm


<br /> $ npm install title-notify<br />


bower


<br /> $ bower install inotify<br />

init


effect: flash | scroll | favicon  

js&nbsp;<br /> iNotify.init({<br /> &nbsp; &nbsp; message: '有消息了。',//標題<br /> &nbsp; &nbsp; effect: 'flash', // flash | scroll 閃爍還是滾動<br /> &nbsp; &nbsp; //可選播放聲音<br /> &nbsp; &nbsp; audio:{<br /> &nbsp; &nbsp; &nbsp; &nbsp; file: 'msg.mp4'<br /> &nbsp; &nbsp; },<br /> &nbsp; &nbsp; //標題閃爍,或者滾動速度<br /> &nbsp; &nbsp; interval: 1000,<br /> &nbsp; &nbsp; //可選,默認綠底白字的 &nbsp;Favicon<br /> &nbsp; &nbsp; updateFavicon:{<br /> &nbsp; &nbsp; &nbsp; &nbsp; // favicon 字體顏色<br /> &nbsp; &nbsp; &nbsp; &nbsp; textColor: "#fff",<br /> &nbsp; &nbsp; &nbsp; &nbsp; //背景顏色,設置背景顏色透明,將值設置為“transparent”<br /> &nbsp; &nbsp; &nbsp; &nbsp; backgroundColor: "#2F9A00"&nbsp;<br /> &nbsp; &nbsp; },<br /> &nbsp; &nbsp; //可選chrome瀏覽器通知,默認不填寫就是下面的內容<br /> &nbsp; &nbsp; notification:{<br /> &nbsp; &nbsp; &nbsp; &nbsp; title:"通知!",//設置標題<br /> &nbsp; &nbsp; &nbsp; &nbsp; icon:"",//設置圖標 icon 默認為 Favicon<br /> &nbsp; &nbsp; &nbsp; &nbsp; body:'您來了一條新消息'//設置消息內容<br /> &nbsp; &nbsp; }<br /> })<br />

聲音設置


player

播放聲音

js<br /> iNotify.player()<br />

loopPlay

自動播放聲音

js<br /> iNotify.loopPlay()<br />

stopPlay

停止播放聲音

js<br /> iNotify.stopPlay()<br />

setURL

設置播放聲音URL

js<br /> iNotify.setURL()<br />

title通知


setTitle

設置標題  

js<br /> iNotify.setTitle('新標題')<br />


setInterval

設置時間間隔  

js<br /> iNotify.setInterval(2000)<br />

addTimer

添加計數器

js<br /> iNotify.addTimer()<br />

clearTimer

清除計數器  

js<br /> iNotify.clearTimer()<br />

favicon通知


setFavicon

設置icon 顯示數字

js<br /> iNotify.setFavicon(10)<br />

faviconClear

清除數字顯示原來的icon

js<br /> iNotify.faviconClear()<br />

chrome通知


notify

彈出chrome通知,不傳參數為預設值...

js<br /> iNotify.notify();&nbsp;<br /> iNotify.notify({<br /> &nbsp; &nbsp; title:"新通知"<br /> &nbsp; &nbsp; body:"打雷啦,下雨啦..."<br /> });<br />

其它


iNotify.init().title; 獲取標題


例子



實例一


js<br /> function iconNotify(num){<br /> &nbsp; &nbsp; if(!notify) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; var notify = iNotify.init({<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; effect: 'flash',<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interval: 500<br /> &nbsp; &nbsp; &nbsp; &nbsp; });<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp; if(num===0){<br /> &nbsp; &nbsp; &nbsp; &nbsp; notify.faviconClear()<br /> &nbsp; &nbsp; &nbsp; &nbsp; notify.setTitle();<br /> &nbsp; &nbsp; }else if(num&lt;100){<br /> &nbsp; &nbsp; &nbsp; &nbsp; notify.setFavicon(num)<br /> &nbsp; &nbsp; &nbsp; &nbsp; notify.setTitle("有新消息!");<br /> &nbsp; &nbsp; }else if(num&gt;99){<br /> &nbsp; &nbsp; &nbsp; &nbsp; notify.setFavicon('..')<br /> &nbsp; &nbsp; &nbsp; &nbsp; notify.setTitle("有新消息!");<br /> &nbsp; &nbsp; }<br /> }<br />

實例二


js<br /> var notify = iNotify.init({<br /> &nbsp; &nbsp; effect: 'flash',<br /> &nbsp; &nbsp; interval: 500<br /> });<br /> notify.setFavicon("1")<br />

實例三


js<br /> var iN = iNotify.init({<br /> &nbsp; &nbsp; effect: 'flash',<br /> &nbsp; &nbsp; interval: 500,<br /> &nbsp; &nbsp; message:"有消息拉!",<br /> &nbsp; &nbsp; updateFavicon:{//可選,默認綠底白字<br /> &nbsp; &nbsp; &nbsp; &nbsp; textColor: "#fff",// favicon 字體顏色<br /> &nbsp; &nbsp; &nbsp; &nbsp; backgroundColor: "#2F9A00" //背景顏色<br /> &nbsp; &nbsp; }<br /> }).setFavicon(10);<br />

實例四


js<br /> var iN = iNotify.init().setFavicon(5);<br />


實例五


js<br /> var iN = iNotify.init({<br /> &nbsp; &nbsp; effect: 'flash',<br /> &nbsp; &nbsp; interval: 500,<br /> &nbsp; &nbsp; message:"有消息拉!",<br /> &nbsp; &nbsp; audio:{<br /> &nbsp; &nbsp; &nbsp; &nbsp; file: 'msg.mp4'<br /> &nbsp; &nbsp; }<br /> }).setFavicon(10).player();<br />


實例五


js<br /> var iN = iNotify.init({<br /> &nbsp; &nbsp; effect: 'flash',<br /> &nbsp; &nbsp; interval: 500,<br /> &nbsp; &nbsp; message:"有消息拉!",<br /> &nbsp; &nbsp; audio:{<br /> &nbsp; &nbsp; &nbsp; &nbsp; file: 'msg.mp4'<br /> &nbsp; &nbsp; },<br /> &nbsp; &nbsp; notification:{<br /> &nbsp; &nbsp; &nbsp; &nbsp; title:"通知!",<br /> &nbsp; &nbsp; &nbsp; &nbsp; icon:"",<br /> &nbsp; &nbsp; &nbsp; &nbsp; body:'您來了一條新消息'<br /> &nbsp; &nbsp; }<br /> }).setFavicon(10).player();<br /> <br /> //彈出chrome通知,不傳參數為預設值...<br /> iN.notify();&nbsp;<br /> <br /> iN.notify({<br /> &nbsp; &nbsp; title:"新通知"<br /> &nbsp; &nbsp; body:"打雷啦,下雨啦..."<br /> });&nbsp;<br />


 本文由用戶 jaywcjlove 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!