osc_tweets_never_miss

osc首页动弹提醒

  1. // ==UserScript==
  2. // @name osc_tweets_never_miss
  3. // @namespace http://www.oschina.net/
  4. // @description osc首页动弹提醒
  5. // @include http://www.oschina.net/
  6. // @version 1.3
  7. // @grant none
  8. // ==/UserScript==
  9. function notifyTweets(title, body, icon)
  10. {
  11. // Let's check if the browser supports notifications
  12. var options =
  13. {
  14. body: body,
  15. icon: icon
  16. };
  17. if (!('Notification' in window))
  18. {
  19. alert('当前浏览器不支持桌面通知!!');
  20. }
  21. else if (Notification.permission === 'granted')
  22. {
  23. // If it's okay let's create a notification
  24. createNotify(title, options);
  25. }
  26. else if (Notification.permission !== 'denied')
  27. {
  28. Notification.requestPermission(function (permission)
  29. {
  30. // If the user is okay, let's create a notification
  31. if (permission === 'granted')
  32. {
  33. createNotify(title, options);
  34. }
  35. });
  36. }
  37. }
  38. function createNotify(title, options)
  39. {
  40. var notification = new Notification(title, options);
  41. notification.onclick = function (event)
  42. {
  43. event.preventDefault();
  44. notification.close();
  45. };
  46. }
  47. $(document).on('DOMNodeInserted', '.TopTweets li', function (e)
  48. {
  49. var user = $(e.target).find('.user a').text();
  50. var body = $(e.target).find('.log').text();
  51. var icon = $(e.target).find('.SmallPortrait').attr('src');
  52. notifyTweets(user, body, icon);
  53. });