knochenhans / xHamster Notification Popup

// ==UserScript==
// @name        xHamster Notification Popup
// @version     0.0.2
// @namespace   @XOR
// @include     *xhamster.com*
// @description Simple script to show popup notifications for new messages or notifications and change the title to reflect new messages/notfications
// @grant       GM.notification
// @license     MIT
// ==/UserScript==

var targetMsg = document.querySelector('.counter-messages');
var targetNotification = document.querySelector('.counter-notifications');

var currentMsgCount = targetMsg.innerHTML;
var currentNotificationCount = targetNotification.innerHTML;

function setTitle() {
  var pre = "";

  if (parseInt(currentMsgCount, 10) > 0)
    pre = "[" + currentMsgCount + "] ";

  if (parseInt(currentNotificationCount, 10) > 0)
    pre += "(" + currentNotificationCount + ") ";

  document.title = pre + document.title;
}

var observerMsg = new MutationObserver(function (mutations) {
  mutations.forEach(function (mutation) {
    if (parseInt(targetMsg.innerHTML, 10) > parseInt(currentMsgCount, 10)) {
      GM.notification({
        text: 'New message',
        title: 'Message',
        onclick: () => window.focus()
      });

      currentMsgCount = targetMsg.innerHTML;
      setTitle();
    }
  });
});

var observerNotification = new MutationObserver(function (mutations) {
  mutations.forEach(function (mutation) {
    if (parseInt(targetNotification.innerHTML, 10) > parseInt(currentNotificationCount, 10)) {
      GM.notification({
        text: 'New notification',
        title: 'Notification',
        onclick: () => window.focus()
      });

      currentNotificationCount = targetNotification.innerHTML;
      setTitle();
    }
  });
});

var config = {
  characterData: true,
  childList: true
};

observerMsg.observe(targetMsg, config);
observerNotification.observe(targetNotification, config);

setTitle();