HWM_Def_Pre_Notifications

Уведомления о начинающемся вскоре наборе в защиты (уведомляет когда вкладка битв становится оранжевой)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        HWM_Def_Pre_Notifications
// @namespace   Рианти
// @description Уведомления о начинающемся вскоре наборе в защиты (уведомляет когда вкладка битв становится оранжевой)
// @include     *heroeswm.ru*
// @version     1
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_xmlhttpRequest
// ==/UserScript==

var sound = 'http://www.freesfx.co.uk/rx2/mp3s/4/16584_1460661075.mp3';

if(Notification.permission !== 'denied' && Notification.permission !== "granted") Notification.requestPermission(function (permission) {
  if (permission === "granted") {
    var notification = new Notification("Теперь вы будете получать уведомления о предствоящих защитах.");
  } else {
    alert ('Без разрешения на уведомления вам будет доступно только уведомление звуковым сигналом.');
  }
});

var lastCheckResult, lastNotifyTime, now, isActive, lastCheckTime;

check(document);

function check(dom){
    lastCheckResult = parseInt(GM_getValue('lastCheckResult', '1'));
    lastNotifyTime = parseInt(GM_getValue('lastNotifyTime', '0'));
    now = Date.now();
    isActive = dom.querySelector('body').innerHTML.indexOf('<font color="#ff9c00">Битвы</font>') > -1 ? 1 : 0;

    if(isActive && !lastCheckResult && (now > 60 * 1000 + lastNotifyTime)) notify();

    GM_setValue('lastCheckTime', now);
    GM_setValue('lastCheckResult', isActive);
    setTimeout(intervalCheck, 60 * 1000);
}

function intervalCheck(){
  now = Date.now();
  lastCheckTime = parseInt(GM_getValue('lastCheckTime', '0'));
  console.log(60 * 1000 + lastCheckTime - now);
  if (now >= 60 * 1000 + lastCheckTime){
    GM_setValue('lastCheckTime', now);
    requestPage(location.protocol + '//' + location.hostname + '/home.php', check);
  } else {
    setTimeout(intervalCheck, 60 * 1000 + lastCheckTime - now + Math.random() * 500); // Случайная прибавка позволяет избежать риска загрузки на лишних вкладках.
  }
}

function notify(){
  GM_setValue('lastNotifyTime', now);
  new Audio(sound).play();

  if (Notification.permission === "granted") {
    var notification = new Notification("Силы тьмы на подходе!");
  } else if (Notification.permission !== 'denied') {
    Notification.requestPermission(function (permission) {
      if (permission === "granted") {
        var notification = new Notification("Силы тьмы на подходе!");
      }
    });
  }
}

function requestPage (url, onloadHandler){
  console.log('[HWM_Def_Pre_Notifications] loading: ', url);
  GM_xmlhttpRequest({
    overrideMimeType: 'text/plain; charset=windows-1251',
    synchronous: false,
    url: url,
    method: "GET",
    onload: function(response){
      onloadHandler(new DOMParser().parseFromString(response.responseText, 'text/html').documentElement);
    },
    onerror: function(){ requestPage (url, onloadHandler) },
    ontimeout: function(){ requestPage (url, onloadHandler) },
    timeout: 5000
  });
}