Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Epicmafia New Game Notifications
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description try to take over the world!
- // @author You
- // @match https://epicmafia.com/lobby
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- checkGames();
- function checkGames() // Loads all open games, pushes notifications for Competitive games
- {
- var gameList = $(".gamerow.vv.mafia_gamerow.join");
- var index;
- var currGame;
- var currGameLink;
- var currImgSrc;
- var currHostName;
- var currGameId;
- var currSetupName;
- for(index=0; index<gameList.length; ++index) { // Loop through available games
- currGame = gameList[index];
- currGameId = $(currGame).attr("data-gid");
- // Make sure the notification appears no more than once
- var cookieName = "game"+currGameId;
- if(document.cookie.search(cookieName) != -1) // Notification for this setup has already appeared before
- continue; // skip it
- document.cookie = cookieName+"=1; ";
- // Fetch the Image's source
- if($(currGame).find("img")[0])
- currImgSrc = $(currGame).find("img")[0].currentSrc;
- if(!currImgSrc) // Unranked game
- continue;
- else if(currImgSrc === "https://epicmafia.com/images/lock.png") /// Private game
- continue;
- else if(currImgSrc === "https://epicmafia.com/images/lives.png") /// Red heart (Ranked)
- continue;
- else if(currImgSrc === "https://epicmafia.com/images/goldlives.png") // Gold heart (Competitive)
- ;
- // Fetch the Game's link
- currGameLink = "https://epicmafia.com/game/"+currGame.getAttribute("data-gid");
- // Fetch the Host's username
- var hostProfileLink = "https://epicmafia.com" + $(currGame).find(".tt.userinfo")[0].getAttribute("href");
- var hostedSetupLink = hostProfileLink;
- var f = function(p_currGameLink, p_currGameId, p_currHostName){
- var ajaxCall1 = $.ajax({url: hostProfileLink, type:"GET"}); // Request the host's profile HTML
- var ajaxCall2 = $.ajax({url: hostedSetupLink, type:"GET"}); // Request the hosted setup's page's HTML
- $.when(ajaxCall1, ajaxCall2).done(
- function(promise1, promise2){
- //console.log(p_currGameLink);
- p_currHostName = $(promise1[0]).find("#usertitle")[0].innerText;
- sendNotification(p_currGameLink, p_currGameId, p_currHostName);
- });
- };
- f(currGameLink, currGameId, currHostName);
- }
- }
- function sendNotification(currGameLink, currGameId, currHostName) { // Pushes notification, if possible
- // Let's check if the browser supports notifications
- if (!("Notification" in window)) {
- alert("This browser does not support desktop notification");
- }
- // Let's check whether notification permissions have already been granted
- else if (Notification.permission === "granted")
- createNotification(currGameLink, currGameId, currHostName);
- // Otherwise, we need to ask the user for permission
- else if (Notification.permission !== "denied") {
- Notification.requestPermission(function (permission) {
- // If the user accepts, let's create a notification
- if (permission === "granted") {
- createNotification(currGameLink, currGameId, currHostName);
- }
- });
- }
- }
- function createNotification(currGameLink, currGameId, currHostName) // Creates notification with information from given parameters
- {
- var notification = new Notification("Competitive game "+currGameId+" open.\nHost: "+currHostName);
- notification.onclick = function(){
- window.open(currGameLink);
- };
- }
- var refreshDelay = 4000;
- function refreshPage() { // Refreshes the page if inactive
- if(document.hidden)
- location.reload();
- }
- setInterval(refreshPage, refreshDelay);
- //notifyMe();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement