Gab.ai Mentions Notifications

Additional notifications count just for mentions.

目前为 2017-01-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Gab.ai Mentions Notifications
  3. // @namespace https://www.ekkooff.com/
  4. // @version 1.1
  5. // @description Additional notifications count just for mentions.
  6. // @author Kevin Roberts (@echo)
  7. // @match https://gab.ai/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14.  
  15. var init = function () {
  16.  
  17. if(!window.hasOwnProperty('$')||!window.hasOwnProperty('Pusher')) {
  18. setTimeout(init,500);
  19. return;
  20. }
  21.  
  22. var mentions = $('<span id="notificationmentions" style="background:#55aa22;left:120px;" class="hidden">0</span>');
  23.  
  24. var reset = function() {
  25. mentions.text('0');
  26. mentions.addClass('hidden');
  27. };
  28.  
  29. var oldXHR = window.XMLHttpRequest;
  30. function newXHR() {
  31. var realXHR = new oldXHR();
  32. realXHR.addEventListener("readystatechange", function() {
  33. if(realXHR.readyState==4 && realXHR.status==200){
  34. if(realXHR.responseURL.startsWith('https://gab.ai/api/notifications')) {
  35. reset();
  36. }
  37. }
  38. }, false);
  39. return realXHR;
  40. }
  41. window.XMLHttpRequest = newXHR;
  42.  
  43. var timeout;
  44. var times = 0;
  45.  
  46. var bind = function() {
  47. if(Pusher.instances[0]===undefined) {
  48. times++;
  49. if(times <= 3) {
  50. timeout = setTimeout(bind, 500);
  51. } else {
  52. console.log('Could not bind to notifications, giving up.');
  53. }
  54. }
  55.  
  56. Pusher.instances[0].bind_all(function(eventName,data) {
  57. console.log(data.message);
  58. if(data.message && data.message.endsWith("mentioned you in a post.")) {
  59. if($('#notificationmentions').length===0) {
  60. $('.notification-count').append(mentions);
  61. }
  62. var count = parseInt(mentions.text());
  63. count++;
  64. mentions.text(count);
  65. mentions.removeClass('hidden');
  66. }
  67. });
  68. };
  69.  
  70. timeout = setTimeout(bind, 500);
  71. };
  72.  
  73. window.addEventListener('load',init,false);
  74. })();