Gab.ai Mentions Notifications

Additional notifications count just for mentions.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Gab.ai Mentions Notifications
// @namespace    https://www.ekkooff.com/
// @version      1.1
// @description  Additional notifications count just for mentions.
// @author       Kevin Roberts (@echo)
// @match        https://gab.ai/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';


      var init = function () {

      if(!window.hasOwnProperty('$')||!window.hasOwnProperty('Pusher')) {
          setTimeout(init,500);
          return;
      }

      var mentions = $('<span id="notificationmentions" style="background:#55aa22;left:120px;" class="hidden">0</span>');

      var reset = function() {
          mentions.text('0');
          mentions.addClass('hidden');
      };

      var oldXHR = window.XMLHttpRequest;
      function newXHR() {
          var realXHR = new oldXHR();
          realXHR.addEventListener("readystatechange", function() {
              if(realXHR.readyState==4 && realXHR.status==200){
                  if(realXHR.responseURL.startsWith('https://gab.ai/api/notifications')) {
                      reset();
                  }
              }
          }, false);
          return realXHR;
      }
      window.XMLHttpRequest = newXHR;

      var timeout;
      var times = 0;

      var bind = function() {
        if(Pusher.instances[0]===undefined) {
          times++;
          if(times <= 3) {
            timeout = setTimeout(bind, 500);
          } else {
            console.log('Could not bind to notifications, giving up.');
          }
        }

        Pusher.instances[0].bind_all(function(eventName,data) {
          console.log(data.message);
          if(data.message && data.message.endsWith("mentioned you in a post.")) {
            if($('#notificationmentions').length===0) {
              $('.notification-count').append(mentions);
            }
            var count = parseInt(mentions.text());
            count++;
            mentions.text(count);
            mentions.removeClass('hidden');
          }
        });
      };

      timeout = setTimeout(bind, 500);
    };

     window.addEventListener('load',init,false);
})();