Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 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 currLink;
  22. var currImgSrc;
  23. var currHostName;
  24. var currGameId;
  25.  
  26. for(index=0; index<gameList.length; ++index) { // Loop through available games
  27. currGame = gameList[index];
  28. currGameId = $(currGame).attr("data-gid");
  29.  
  30. // Make sure the notification appears no more than once
  31. var cookieName = "gamedgfdgdhbsghhg,zd"+currGameId;
  32.  
  33. //console.log("1");
  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. //console.log("2");
  44. if(!currImgSrc || currImgSrc !== "https://epicmafia.com/images/goldlives.png") /// It isn't a gold heart
  45. continue; // skip it
  46.  
  47. //console.log("3");
  48. // Fetch the Game's link
  49. currLink = "https://epicmafia.com/game/"+currGame.getAttribute("data-gid");
  50.  
  51. // Fetch the Host's name
  52. var hostProfileLink = "https://epicmafia.com" + $(currGame).find(".tt.userinfo")[0].getAttribute("href");
  53. console.log(hostProfileLink);
  54. var ajaxCall = $.ajax({currLink: currLink, currGameId: currGameId, currHostName: currHostName, url: hostProfileLink, type:"GET"});
  55. $.when(ajaxCall).done(function(ajaxCallParam){
  56. //console.log("4");
  57. console.log(ajaxCallParam);
  58. this.currHostName = $(ajaxCallParam).find("#usertitle")[0].innerText;
  59. console.log("Host name = "+currHostName);
  60.  
  61. sendNotification(this.currLink, this.currGameId, this.currHostName);
  62. });
  63. }
  64. }
  65. function sendNotification(currLink, currGameId, currHostName) { // Pushes notification, if possible
  66. // Let's check if the browser supports notifications
  67. if (!("Notification" in window)) {
  68. alert("This browser does not support desktop notification");
  69. }
  70.  
  71. // Let's check whether notification permissions have already been granted
  72. else if (Notification.permission === "granted")
  73. createNotification(currLink, currGameId, currHostName);
  74.  
  75. // Otherwise, we need to ask the user for permission
  76. else if (Notification.permission !== "denied") {
  77. Notification.requestPermission(function (permission) {
  78. // If the user accepts, let's create a notification
  79. if (permission === "granted") {
  80. createNotification(currLink, currGameId, currHostName);
  81. }
  82. });
  83. }
  84. }
  85.  
  86. function createNotification(currLink, currGameId, currHostName) // Creates notification with information from given parameters
  87. {
  88. var notification = new Notification("Competitive game "+currGameId+" open.\nHost: "+currHostName);
  89. notification.onclick = function(){
  90. window.open(currLink);
  91. };
  92. }
  93.  
  94. var refreshDelay = 5000;
  95. function refreshPage() { // Refreshes the page if inactive
  96. if(document.hidden)
  97. location.reload();
  98. }
  99.  
  100. setInterval(refreshPage, refreshDelay);
  101.  
  102. //notifyMe();
  103. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement