Advertisement
Guest User

background.js

a guest
May 31st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function(){
  2.  
  3.     var keys = ["AIzaSyAobtIM9MXaMsxwYIl3UWppbDU9lN9i0SY", "AIzaSyB8Ye39ZELzbEtOesRAOWPMS3Ya9jRSyh4", "AIzaSyDC1Gs3Y6XLo4TSH2I2SYFimPctgtJNLYw"];
  4.         ytID = '';
  5.         twitch_data = '';
  6.         titleForNtf = '';
  7.         twitchID = '';
  8.         nowOnline = '';
  9.  
  10.     ChannelType = {
  11.         YOUTUBE = 0,
  12.         TWITCH = 1
  13.     };
  14.  
  15.     var liveStatus = new Array();
  16.         liveStatus[ChannelType.TWITCH] = 'offline';
  17.         liveStatus[ChannelType.YOUTUBE] = 'offline';
  18.  
  19.     var NilfgornChannelId = "UCEdCg-dup5LY-v5Y5F7IeTQ";
  20.  
  21.     updateData();
  22.  
  23.  
  24.  
  25.     function getTwitch(){
  26.         $.ajax({
  27.             url: 'https://api.twitch.tv/kraken/streams/nilfgorn',
  28.             type: 'GET',
  29.             async: false,
  30.             complete: function(senddata){
  31.                 var data=JSON.stringify(senddata);
  32.                     data=JSON.parse(data);
  33.                     twitch_data=JSON.parse(data.responseText);
  34.                 if (twitch_data.stream == null){
  35.                     liveStatus['tw'] = 'offline';
  36.                 } else {
  37.                     if (twitchID == twitch_data.stream._id) {
  38.                         liveStatus[ChannelType.TWITCH] = 'offline';
  39.                     } else {
  40.                         liveStatus[ChannelType.TWITCH] = 'online';
  41.                         twitchID = twitch_data.stream._id;
  42.                         titleForNtf = twitch_data.stream.channel.status + ' (' + twitch_data.stream.channel.game + ')';
  43.                     }
  44.                 };
  45.             }
  46.         });
  47.     };
  48.  
  49.  
  50.  
  51.  
  52.     function getYoutubeLive(cid){
  53.         $.ajax({
  54.             url: 'https://www.googleapis.com/youtube/v3/search?key='+keys[Math.floor((Math.random() * 3))]+'&part=snippet&type=video&channelId='+cid+'&order=date&eventType=live',
  55.             type: 'GET',
  56.             async: false,
  57.             complete: function(senddata){
  58.                 var data=JSON.stringify(senddata);
  59.                     data=JSON.parse(data);
  60.                     yt_result=JSON.parse(data.responseText);
  61.  
  62.                 if (yt_result.pageInfo.totalResults == 0 || nowOnline == yt_result.items[0].id.videoId)
  63.                 {
  64.                     if (cid == NilfgornChannelId)
  65.                     {
  66.                         liveStatus[ChannelType.YOUTUBE] = 'offline';
  67.                     }
  68.                     else
  69.                     {
  70.                         liveStatus[ChannelType.TWITCH] = 'offline';
  71.                     }
  72.                 }
  73.                 else
  74.                 {
  75.                     if (cid == NilfgornChannelId)
  76.                     {
  77.                             liveStatus[ChannelType.YOUTUBE] = 'online';
  78.                             nowOnline =  yt_result.items[0].id.videoId;
  79.                             titleForNtf = yt_result.items[0].snippet.title;
  80.                     }
  81.                     else
  82.                     {
  83.                         liveStatus[ChannelType.TWITCH] = 'online';
  84.                         nowOnline =  yt_result.items[0].id.videoId;
  85.                         titleForNtf = yt_result.items[0].snippet.title;
  86.                     };
  87.  
  88.                     ytID = yt_result.items[0].id.videoId;
  89.  
  90.                         $.ajax({
  91.                             url: 'https://www.googleapis.com/youtube/v3/search?key='+keys[Math.floor((Math.random() * 3))]+'&part=snippet&type=video&channelId='+cid+'&order=date&eventType=completed',
  92.                             type: 'GET',
  93.                             async: false,
  94.                             complete: function(senddata){
  95.                                 var data=JSON.stringify(senddata);
  96.                                     data=JSON.parse(data);
  97.                                     check_yt_result=JSON.parse(data.responseText);
  98.                                 if (check_yt_result.items[0].id.videoId == ytID) {
  99.                                     if (cid == NilfgornChannelId) {
  100.                                         liveStatus[ChannelType.YOUTUBE] = 'offline';
  101.                                     } else {
  102.                                         liveStatus[ChannelType.TWITCH] = 'offline';
  103.                                     }
  104.                                 } else {
  105.                                     if (cid == NilfgornChannelId) {
  106.                                         liveStatus[ChannelType.YOUTUBE] = 'online';
  107.                                     } else {
  108.                                         liveStatus[ChannelType.TWITCH] = 'online';
  109.                                     }
  110.                                 }
  111.                             }
  112.                         });
  113.                 }
  114.             }
  115.         });
  116.     };
  117.  
  118.  
  119.  
  120.  
  121.  
  122.     function updateData() {
  123.        
  124.         getTwitch();
  125.  
  126.         if (liveStatus[ChannelType.TWITCH]=='online') {
  127.  
  128.             var mailNotification = new Notification(titleForNtf, {
  129.                 tag: "tw",
  130.                 body: "Прямо сейчас на Twitch'e",
  131.                 icon: "src/img/livetwitch.png"
  132.             });
  133.             mailNotification.onclick = function(){
  134.                 window.open("http://twitch.tv/nilfgorn");
  135.                 mailNotification.close();
  136.             };
  137.  
  138.         } else {
  139.  
  140.             getYoutubeLive(NilfgornChannelId); // UCEdCg-dup5LY-v5Y5F7IeTQ  UCCCPCZNChQdGa9EkATeye4g
  141.  
  142.             if (liveStatus['yt']=='online') {
  143.  
  144.                 var mailNotification = new Notification(titleForNtf, {
  145.                     tag: "yt",
  146.                     body: "Прямо сейчас на YouTube'e",
  147.                     icon: "src/img/liveyoutube.png"
  148.                 });
  149.                 mailNotification.onclick = function(){
  150.                     window.open("https://youtu.be/" + ytID);
  151.                     mailNotification.close();
  152.                 };
  153.  
  154.             } else {
  155.  
  156.                 getYoutubeLive(NilfgornChannelId);
  157.  
  158.                 if (liveStatus[ChannelType.TWITCH] == 'online') {
  159.  
  160.                     var mailNotification = new Notification(titleForNtf, {
  161.                         tag: "aw",
  162.                         body: "Прямо сейчас на YouTube'e",
  163.                         icon: "src/img/liveyoutube.png"
  164.                     });
  165.                     mailNotification.onclick = function(){
  166.                         window.open("https://youtu.be/" + ytID);
  167.                         mailNotification.close();
  168.                     };
  169.  
  170.                 }
  171.             }
  172.         };
  173.  
  174.         setTimeout(updateData, 1000*60*2); // 2 minutes
  175.     }
  176. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement