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 currLink;
- var currImgSrc;
- var currHostName;
- var currGameId;
- 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 = "gamedgfdgdhbsghhg,zd"+currGameId;
- //console.log("1");
- 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;
- //console.log("2");
- if(!currImgSrc || currImgSrc !== "https://epicmafia.com/images/goldlives.png") /// It isn't a gold heart
- continue; // skip it
- //console.log("3");
- // Fetch the Game's link
- currLink = "https://epicmafia.com/game/"+currGame.getAttribute("data-gid");
- // Fetch the Host's name
- var hostProfileLink = "https://epicmafia.com" + $(currGame).find(".tt.userinfo")[0].getAttribute("href");
- console.log(hostProfileLink);
- var ajaxCall = $.ajax({currLink: currLink, currGameId: currGameId, currHostName: currHostName, url: hostProfileLink, type:"GET"});
- $.when(ajaxCall).done(function(ajaxCallParam){
- //console.log("4");
- console.log(ajaxCallParam);
- this.currHostName = $(ajaxCallParam).find("#usertitle")[0].innerText;
- console.log("Host name = "+currHostName);
- sendNotification(this.currLink, this.currGameId, this.currHostName);
- });
- }
- }
- function sendNotification(currLink, 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(currLink, 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(currLink, currGameId, currHostName);
- }
- });
- }
- }
- function createNotification(currLink, currGameId, currHostName) // Creates notification with information from given parameters
- {
- var notification = new Notification("Competitive game "+currGameId+" open.\nHost: "+currHostName);
- notification.onclick = function(){
- window.open(currLink);
- };
- }
- var refreshDelay = 5000;
- 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