Skip to content

Instantly share code, notes, and snippets.

Created March 8, 2016 03:15
Show Gist options
  • Save anonymous/c1ce9f3ee8ab0672a44f to your computer and use it in GitHub Desktop.
Save anonymous/c1ce9f3ee8ab0672a44f to your computer and use it in GitHub Desktop.
关注 V2EX 回帖并调用 Chrome 通知
/**
* 使用方法:
*
* 1、打开 https://www.v2ex.com/
* 2、右键检查(审查元素)
* 3、点击 console
* 4、复制并粘贴本代码
* 5、回车运行
*/
(function getLastReply(topic_id) {
Notification.requestPermission(); // 请求通知
console.log('关注中...');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.v2ex.com/api/replies/show.json?topic_id=' + topic_id, true);
xhr.send();
xhr.onload = function() {
var last_reply = JSON.parse(xhr.responseText).pop();
console.log(Date.parse(new Date())/1000 - last_reply.created);
if (Date.parse(new Date())/1000 - last_reply.created < 60) {
return new Notification('您关注的主题有了新的评论', {
body: last_reply.content,
icon: 'https://www.v2ex.com/static/img/icon_rayps_64.png',
}); // 发送通知
} else {
setTimeout(function (){
getLastReply(topic_id);
}, 30000); // V2EX 限制,API 每小时只能请求 120 次
}
};
})('261843'); // 帖子 ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment