Advertisement
Guest User

Competitive Lobby Notifications

a guest
Sep 21st, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Epicmafia New Game Notifications
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://epicmafia.com/lobby
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. checkGames();
  15.  
  16. function checkGames() // Loads all open games, pushes notifications for Competitive games
  17. {
  18. var gameList = $(".gamerow.vv.mafia_gamerow.join");
  19. var index;
  20. var currGame;
  21. var currGameLink;
  22. var currImgSrc;
  23. var currHostName;
  24. var currGameId;
  25. var currSetupName;
  26.  
  27. for(index=0; index<gameList.length; ++index) { // Loop through available games
  28. currGame = gameList[index];
  29. currGameId = $(currGame).attr("data-gid");
  30.  
  31. // Make sure the notification appears no more than once
  32. var cookieName = "game"+currGameId;
  33.  
  34. if(document.cookie.search(cookieName) != -1) // Notification for this setup has already appeared before
  35. continue; // skip it
  36.  
  37. document.cookie = cookieName+"=1; ";
  38.  
  39. // Fetch the Image's source
  40. if($(currGame).find("img")[0])
  41. currImgSrc = $(currGame).find("img")[0].currentSrc;
  42.  
  43. if(!currImgSrc) // Unranked game
  44. continue;
  45. else if(currImgSrc === "https://epicmafia.com/images/lock.png") /// Private game
  46. continue;
  47. else if(currImgSrc === "https://epicmafia.com/images/lives.png") /// Red heart (Ranked)
  48. continue;
  49. else if(currImgSrc === "https://epicmafia.com/images/goldlives.png") // Gold heart (Competitive)
  50. ;
  51.  
  52.  
  53. // Fetch the Game's link
  54. currGameLink = "https://epicmafia.com/game/"+currGame.getAttribute("data-gid");
  55.  
  56. // Fetch the Host's username
  57. var hostProfileLink = "https://epicmafia.com" + $(currGame).find(".tt.userinfo")[0].getAttribute("href");
  58. var hostedSetupLink = hostProfileLink;
  59.  
  60. var f = function(p_currGameLink, p_currGameId, p_currHostName){
  61. var ajaxCall1 = $.ajax({url: hostProfileLink, type:"GET"}); // Request the host's profile HTML
  62. var ajaxCall2 = $.ajax({url: hostedSetupLink, type:"GET"}); // Request the hosted setup's page's HTML
  63.  
  64. $.when(ajaxCall1, ajaxCall2).done(
  65. function(promise1, promise2){
  66. //console.log(p_currGameLink);
  67. p_currHostName = $(promise1[0]).find("#usertitle")[0].innerText;
  68.  
  69. sendNotification(p_currGameLink, p_currGameId, p_currHostName);
  70. });
  71. };
  72. f(currGameLink, currGameId, currHostName);
  73.  
  74.  
  75. }
  76. }
  77. function sendNotification(currGameLink, currGameId, currHostName) { // Pushes notification, if possible
  78. // Let's check if the browser supports notifications
  79. if (!("Notification" in window)) {
  80. alert("This browser does not support desktop notification");
  81. }
  82.  
  83. // Let's check whether notification permissions have already been granted
  84. else if (Notification.permission === "granted")
  85. createNotification(currGameLink, currGameId, currHostName);
  86.  
  87. // Otherwise, we need to ask the user for permission
  88. else if (Notification.permission !== "denied") {
  89. Notification.requestPermission(function (permission) {
  90. // If the user accepts, let's create a notification
  91. if (permission === "granted") {
  92. createNotification(currGameLink, currGameId, currHostName);
  93. }
  94. });
  95. }
  96. }
  97.  
  98. function createNotification(currGameLink, currGameId, currHostName) // Creates notification with information from given parameters
  99. {
  100. var notification = new Notification("Competitive game "+currGameId+" open.\nHost: "+currHostName);
  101. notification.onclick = function(){
  102. window.open(currGameLink);
  103. };
  104. }
  105.  
  106. var refreshDelay = 4000;
  107. function refreshPage() { // Refreshes the page if inactive
  108. if(document.hidden)
  109. location.reload();
  110. }
  111.  
  112. setInterval(refreshPage, refreshDelay);
  113.  
  114. //notifyMe();
  115. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement