Skip to content

Instantly share code, notes, and snippets.

@SirM2z
Last active November 24, 2017 07:29
Show Gist options
  • Save SirM2z/877a2b16e963d686fba20dea8806001d to your computer and use it in GitHub Desktop.
Save SirM2z/877a2b16e963d686fba20dea8806001d to your computer and use it in GitHub Desktop.
chrome console snippets
(function() {
const NotificationInstance = Notification || window.Notification;
if (!!NotificationInstance) {
const permissionNow = NotificationInstance.permission;
// 'default' 用户还未被询问是否授权,所以通知不会被显示
// 'granted' 表示之前已经询问过用户,并且用户已经授予了显示通知的权限
// 'denied' 用户已经明确的拒绝了显示通知的权限
if (permissionNow === 'granted') {
// 允许通知
CreatTask();
} else if (permissionNow === 'denied') {
console.log('用户拒绝了你!!!');
setPermission();
} else {
setPermission();
}
}
function setPermission() {
// 请求获取通知权限
NotificationInstance.requestPermission(function(PERMISSION) {
if (PERMISSION === 'granted') {
CreatTask();
} else {
console.log('用户无情残忍的拒绝了你!!!');
}
});
}
function CreatTask() {
var num = 1;
var t = setInterval(()=>{
console.log(`休息任务运行第: ${num++} 次`)
var now = new Date()
if (now.getHours() === 15 && now.getMinutes() === 25) {
clearInterval(t);
CreatNotification();
}
}, 10000)
}
function CreatNotification() {
const n = new NotificationInstance('go go go',{
body: '休息时间到了!',
tag: 'z',
icon: 'https://avatars2.githubusercontent.com/u/12137804?s=460&v=4',
data: {
url: 'https://github.com/SirM2z'
},
// 新通知出现是否覆盖旧的通知,覆盖(true)则永远只显示一条通知,不覆盖(false)则会多条通知重叠。非必须,默认为true
renotify: true,
// 通知是否静音。非必须,默认为false,表示无声
silent: true,
// 通知声源文件地址。非必须,默认为空
sound: 'mp3',
// 指定通知是否保持活性,知道用户点击或关闭。非必须,默认为false
requireInteraction: true
});
n.onshow = function() {
new Audio('http://www.runoob.com/try/demo_source/horse.ogg').play()
console.log('成功显示!');
}
n.onclick = function(e) {
// 可以直接通过实例的方式获取data内自定义的数据
// 也可以通过访问回调参数e来获取data的数据
console.log(e);
window.open(n.data.url, '_blank');
n.close();
}
n.onclose = function() {
console.log('你墙壁了我!!!');
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment