Saucsse / Respawn Notif

// ==UserScript==
// @name         Respawn Notif
// @namespace    http://www.jeuxvideo.com/
// @version      2
// @description  Get notification in-live from JVC
// @author       Craftbukkit
// @match        http://www.jeuxvideo.com/*
// @match        https://www.jeuxvideo.com/*
// @require      https://code.jquery.com/jquery-1.11.3.min.js
// @grant      GM_setValue
// @grant      GM_getValue
// ==/UserScript==

(function() {
    'use strict';
    var hashJVC;
	var iAmTheMaster = false;
	var idMaster = Math.random();
	var alreadySeen = new Array();
    
    var parseNotification = function(data) {
        var leData = data.html.split('//').join();
        if(leData.length >= 10) {
            var nbLength = leData.split('<a href="').length;
            for(var i = 1; i < nbLength; i++) {
                var urlData = leData.split('<a href="')[i].split('"')[0].split(",").join("");
				var idNotif = leData.split('data-id="')[i].split('"')[0];
                var titreData = leData.split('modal-notif-item-label">')[i].split('</a>')[0].split('<!--').join("").split('-->').join('').split('Forums -                     ').join('').trim();
				if(alreadySeen[urlData] != true) {
					alreadySeen[urlData] = true;
					if(urlData.indexOf("messages") != "-1") {
						var authorMP = leData.split('data-id="')[i].split('modal-notif-author">')[1].split('<')[0].trim();
						var notif = new Notification("MP de "+authorMP, {body:titreData,icon:'http://megaicons.net/static/img/icons_sizes/8/178/512/buzz-message-outline-icon.png'});
						urlData = "www.jeuxvideo.com/"+urlData;
						urlData = urlData.split("&amp;").join("&");
					} else {
						var notif = new Notification("TOPIC | "+titreData, {body:'Nouveau(x) message(s)',icon:'http://www.jeuxvideo.com/img/forums/topic-dossier1.png'});
					}
                    notif.hashJVC = hashJVC;
                    notif.idNotif = idNotif;
                    notif.urlData = urlData;
					notif.onclick = function(){$.get("http://www.jeuxvideo.com/abonnements/ajax/set_notification_lu.php?ajax_hash="+this.hashJVC+"&id_notification="+this.idNotif,function(data){});window.open("http://"+this.urlData);};
				}
            }
        }
    };
	
	(function checkIfMaster(){
		var date = new Date();
		var timestamp = date.getTime();
		if(GM_getValue("master") == undefined) {
			GM_setValue("master", idMaster);
			GM_setValue("mastertime", timestamp);
			iAmTheMaster = true;
		} else if(GM_getValue("master") == idMaster) {
			iAmTheMaster = true;
			GM_setValue("mastertime", timestamp);
		} else {
			if(Math.floor((timestamp-GM_getValue("mastertime"))/1000) >= 7) {
				iAmTheMaster = true;
				GM_setValue("master", idMaster);
				GM_setValue("mastertime", timestamp);
			} else {
				iAmTheMaster = false;
			}
		}
		setTimeout(checkIfMaster, 5000);
	})();
    
    // Cimer Mozilla pour la doc sur les notif
    (function askPourLaNotif() {
        if (Notification.permission !== 'denied') {
            Notification.requestPermission(function (permission) {
              if(!('permission' in Notification)) {
                Notification.permission = permission;
              }
            });
          }
      })();
      
    (function getHash() {
        hashJVC = document.body.dataset.aboSession;
    })();
    
    (function checkIfNotif() {
        if(hashJVC != undefined && iAmTheMaster) {
            $.get("http://www.jeuxvideo.com/abonnements/ajax/list_notification_header.php?ajax_hash="+hashJVC, function(data){ parseNotification(data); });
        }
        setTimeout(checkIfNotif, 5000);
    })();
    
})();