stasuss / tale-monitor

// ==UserScript==
// @name        tale-monitor
// @namespace   stasuss
// @include     http://the-tale.org/game/
// @version     1
// @grant       GM_getValue
// @grant       GM_setValue
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// ==/UserScript==

setTimeout(function () {
  checkChoices();
}, 4000);

setInterval(function () {
  checkChoices();
}, 60000);

function checkChoices() {
  if ($('.pgf-quest-description:first').text() == 'безделие') return;
  
  var links = $('.pgf-quest-choices > .pgf-choice-link.choice-link');
  
  if (links.length < 1) return;
  
  var exp = $('.pgf-quest-rewards > .pgf-experience:first').text();
  var pow = $('.pgf-quest-rewards > .pgf-power:first').text();
  var lastExp = GM_getValue('last_exp', 0);
  var lastPow = GM_getValue('last_pow', 0);
  
  if (exp != lastExp && pow != lastPow) {
    showPopup('help your hero! (click to skip)');
  }
}

function showPopup(txt) {
  var opts = {icon: "http://www.divokrim.com/images/security_question.png"};
  
  if (!('Notification' in window)) {
    alert('This browser does not support desktop notification');
  } else if (Notification.permission === 'granted') {
    var notification = new Notification(txt, opts);
  } else if (Notification.permission !== 'denied') {
    Notification.requestPermission(function (permission) {
      if (permission === 'granted') {
        var notification = new Notification(txt, opts);
      }
    });
  }
  
  if (notification) {
    notification.onclick = function () {
      var exp = $('.pgf-quest-rewards > .pgf-experience:first').text();
      var pow = $('.pgf-quest-rewards > .pgf-power:first').text();
      GM_setValue('last_exp', exp);
      GM_setValue('last_pow', pow);
    };
  }
}