2018-05-26 05:48:16 +05:30

1 line
15 KiB
JavaScript

var shareswitch=0,navState=[0,0],views=[],extraswitch=0,myLineChart2={},myLineChart3={},myLineChart4={},vids=5;function idClickListener(e,t){document.getElementById(e).addEventListener("click",t)}function queryClickListener(e,t){document.querySelectorAll(e).forEach(function(e){e.addEventListener("click",t)})}function fx(e){var t=document.getElementById(e),n=0,a=1,i=1;return fx.transition=function(e,t,a,o){for(var s=e,l=0;l<i;l++)setTimeout(function(){s+=Number((t-n)/i),o(s)},50*(l+1))},fx.fadeIn=function(e){e=def(e,400),i=e/50,"none"===window.getComputedStyle(t).getPropertyValue("display")&&(t.dataset.fxDisplay?t.style.display=t.dataset.fxDisplay:t.style.display="block",a=t.dataset.fxOpacity||Number(t.style.opacity)||1,t.style.opacity=0,fx.transition(0,a,1,function(e){t.style.opacity=e},e))},fx.fadeOut=function(e){e=def(e,400),i=e/50,"none"!==window.getComputedStyle(t).getPropertyValue("display")&&(t.dataset.fxDisplay=window.getComputedStyle(t).getPropertyValue("display"),a=t.style.opacity?Number(t.style.opacity):1,t.dataset.fxOpacity=a,n=a,fx.transition(a,0,-1,function(e){t.style.opacity=e},e),setTimeout(function(){t.style.display="none",t.style.opacity=t.dataset.fxOpacity},e))},fx}developmentMode||(window.top!==window.self||window.top.location!==window.self.location?window.top.location=window.self.location:"youcount.github.io"===window.location.hostname&&"youcount.github.io"===window.top.location.hostname||(window.location.hostname="youcount.github.io"));var emailParts=["manas.khurana20","gmail","com","&#46;","&#64;"];document.getElementById("email").innerHTML=emailParts[0]+emailParts[4]+emailParts[1]+emailParts[3]+emailParts[2],document.getElementById("email").href="mailto:"+getText("email");var clickList=[["inputButton",function(){getValue()}],["fb",function(){window.open("https://www.facebook.com/sharer/sharer.php?u="+encodeURIComponent(getText(document.querySelector("#pageUrl input"))),"_blank")}],["tw",function(){window.open("https://twitter.com/share?text="+getText("username")+" now has "+actualCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")+" subscribers!&url= "+encodeURIComponent(getText(document.querySelector("#pageUrl input")))+"&hashtags=YouCount","_blank")}],["lnkdIn",function(){window.open("https://www.linkedin.com/shareArticle?mini=true&url="+encodeURIComponent(getText(document.querySelector("#pageUrl input")))+"&title="+encodeURIComponent(channelname)+"'s%20Live%20Subscriber%20Count&source=YouCount","_blank")}],["tb",function(){window.open("http://www.tumblr.com/share/link?url="+encodeURIComponent(getText(document.querySelector("#pageUrl input"))),"_blank")}],["rdit",function(){window.open("http://www.reddit.com/submit?url="+encodeURIComponent(getText(document.querySelector("#pageUrl input")))+"&title="+encodeURIComponent(channelname)+"s%20Live%20Subscriber%20Count","_blank")}],["link",linkshare],["bg2",function(){isTutorialOn[0]?tutorial(3):(fx("pageUrl").fadeOut(250),fx("bg2").fadeOut(500))}]];function tutorial(e){navState[0]&&handleNavButtons(navState[0]),isTutorialOn[0]=1,isTutorialOn[1]=e+1,window.scrollTo(0,0);var t=0;switch(e){case 0:changeText("username",""),document.getElementById("input").style.zIndex="1004",document.getElementById("tutorial").style.display="block",t=document.getElementById("username").getBoundingClientRect().bottom,document.getElementById("tutorial").style.top=t+35+"px",document.getElementById("bg2").style.display="block",document.getElementById("tutStep1").style.display="block",document.getElementById("tutStep2").style.display="none",isLive||changeText("actualCount","Tutorial"),document.getElementById("tutorial").addEventListener("click",function(){1===isTutorialOn[1]&&document.getElementById("username").focus()});break;case 1:document.getElementById("tutStep1").style.display="none",document.getElementById("tutStep2").style.display="block",t=document.getElementById("username").getBoundingClientRect().bottom,document.getElementById("tutorial").style.top=t+35+"px";break;case 2:document.getElementById("tutStep1").style.display="none",document.getElementById("tutStep2").style.display="block",document.getElementById("suggest").style.zIndex="1004",t=document.getElementById("suggest").getBoundingClientRect().bottom,document.getElementById("tutorial").style.top=t+35+"px",document.getElementById("bg2").style.display="block";break;case 3:document.getElementById("tutorial").style.display="none",document.getElementById("bg2").style.display="none",document.getElementById("input").style.zIndex="50",document.getElementById("suggest").style.zIndex="51",changeText("username",channelname),isTutorialOn[0]=0,isTutorialOn[1]=0;break;default:isTutorialOn[0]=0,isTutorialOn[1]=0}}function shareFunc(e){var t=200;if(!(!0!==e&&shareswitch<2)){var n=document.querySelectorAll(".share");switch(shareswitch){case 0:shareswitch=1,n.forEach(function(e,n){setTimeout(function(){fx(e.id).fadeIn(t)},40*(n-1))}),setTimeout(function(){shareswitch=2},t+40*n.length);break;case 1:break;case 2:shareswitch=1,navState[0]=0,n.forEach(function(e){fx(e.id).fadeOut(t)}),setTimeout(function(){shareswitch=0},t+40*n.length)}}}function handleNavButtons(e){if(!navState[1])if(0!==navState[0])switch(navState[1]=1,document.getElementById("bg1").style.height="100%",document.getElementById("bg1").classList.add("ball"),document.getElementById("mainPage").style.display="block",navState[0]===e?setTimeout(function(){navState=[0,0]},500):setTimeout(function(){navState=[0,0],handleNavButtons(e)},500),navState[0]){case 2:document.getElementById("helpArt").style.display="none",document.getElementById("helpArt").style.opacity="0",document.querySelector('.navButtonsCover[data-child="helpButton"]').style.backgroundColor="transparent";break;case 3:document.getElementById("codeArt").style.display="none",document.getElementById("codeArt").style.opacity="0",document.querySelector('.navButtonsCover[data-child="code"]').style.backgroundColor="transparent"}else switch(e){case 1:location.hash&&(location.href=location.href.split(location.hash)[0]),navState[0]=1;break;case 2:document.getElementById("bg1").classList.remove("ball"),document.querySelector('.navButtonsCover[data-child="helpButton"]').style.backgroundColor="rgba(0,0,0,0.5)",setTimeout(function(){fx("helpArt").fadeIn(200),document.getElementById("bg1").style.height="auto",document.getElementById("mainPage").style.display="none"},500),setTimeout(function(){navState[1]=0},1e3),navState=[2,1];break;case 3:document.getElementById("bg1").classList.remove("ball"),document.querySelector('.navButtonsCover[data-child="code"]').style.backgroundColor="rgba(0,0,0,0.5)",setTimeout(function(){fx("codeArt").fadeIn(200),document.getElementById("bg1").style.height="auto",document.getElementById("mainPage").style.display="none"},500),setTimeout(function(){navState[1]=0},1e3),navState=[3,1];break;case 4:shareFunc(!0),navState[0]=4}}clickList.forEach(function(e){idClickListener(e[0],e[1])}),queryClickListener(".suggest",function(e){if(e.target.dataset.id&&-1===["Loading.","Loading..","Loading..."].indexOf(e.target.innerHTML)){username=e.target.dataset.id;var t="https://www.googleapis.com/youtube/v3/channels?part=snippet&id="+e.target.dataset.id+"&fields=items/snippet&type=channel&maxResults=1&key=";isTutorialOn[0]&&tutorial(3),queryName(t)}}),["showextra","hideextra"].forEach(function(e){idClickListener(e,extrabutton)}),isTutorialOn[0]&&tutorial(0),queryClickListener("body",shareFunc);var usernameKeyUp=[!1,!1],usernameKeyUpInter=null;function usernameKeyUpFunc(){var e=getText("username");if(internet)return!e||["Not Found!","Loading.","Loading..","Loading...","Refresh the page"].indexOf(e)>-1?(document.getElementById("suggest").style.display="none",clearInterval(usernameKeyUpInter),usernameKeyUp=[!1,!1],void(isTutorialOn[0]&&tutorial(1))):void ajx("https://www.googleapis.com/youtube/v3/search?part=snippet&q="+encodeURIComponent(e)+"&type=channel&maxResults=5&relevanceLanguage=en&key="+getKey(),function(e){try{if(!e.items)return void noConnection("undef e.items in username keyup (script.js)",!0);if(e.pageInfo.totalResults<1)return;var t=document.querySelectorAll(".suggest"),n=document.querySelectorAll(".suggest div"),a=document.querySelectorAll(".suggestImg");t.forEach(function(t,n){t.style.display="block",t.dataset.id=e.items[n].snippet.channelId.trim()}),isTutorialOn[0]&&tutorial(2),n.forEach(function(n,i){try{n.dataset.id=e.items[i].snippet.channelId.trim(),changeText(n,e.items[i].snippet.title),a[i].style.visibility="hidden"}catch(e){noConnection("texs.forEach: "+e+" (script.js)",!0),t[i].style.display="none"}}),a.forEach(function(n,a){try{n.dataset.id=e.items[a].snippet.channelId.trim(),n.src=e.items[a].snippet.thumbnails.default.url,whenImageLoaded(n).then(function(){n.style.visibility="visible"})}catch(e){noConnection("imas.forEach: "+e+" (script.js)",!0),t[a].style.display="none"}})}catch(e){noConnection("suggest ajx: "+e+" (script.js)",!0)}},function(){noConnection("username keyup no ajx response (script.js)",!0)});changeText("username","Refresh the page")}function whenImageLoaded(e){return new Promise(function(t){e.addEventListener("load",t)})}function loading(e){e&&-1===loadingList.indexOf(e)?(loadingList.length||(loadingInterval=setInterval(function(){loading()},500)),changeText(e,"Loading."),loadingList.push(e)):loadingList.length?loadingList=loadingList.filter(function(e,t){var n=["Loading.","Loading..","Loading..."],a=n.indexOf(getText(e));return 0===a||1===a?(changeText(e,n[a+1]),!0):2===a&&(changeText(e,n[0]),!0)}):clearInterval(loadingInterval)}function linkshare(){fx("pageUrl").fadeIn(250),fx("bg2").fadeIn(500),username&&location.hash.split("#!/")[1]||changeText(document.querySelector("#pageUrl input"),"https://youcount.github.io/")}document.getElementById("username").addEventListener("keyup",function(){usernameKeyUp[0]?usernameKeyUp[1]=!0:(usernameKeyUp[0]=!0,usernameKeyUp[1]=!0,document.getElementById("suggest").style.display="block",document.querySelectorAll(".suggest").forEach(function(e,t){0===t?(e.style.display="block",e.childNodes[0].style.visibility="hidden",loading(e.childNodes[1])):e.style.display="none"}),isTutorialOn[0]&&tutorial(2),usernameKeyUpInter=setInterval(function(){usernameKeyUp[1]&&(usernameKeyUpFunc(),usernameKeyUp[1]=!1)},1e3))}),loadingList=[],loadingInterval=null;for(var l=50;l>0;l--)views.push(l);function pushViews(e,t){ajx(e,function(e){views[t]=e.items[0].statistics.viewCount,t===2*vids-1&&upCharts()})}function extrabutton(){if(username)if(0===firstload){if(!internet||notFound||isTutorialOn[0])return;loading("showextra");var e="https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id="+username+"&fields=items/contentDetails/relatedPlaylists/uploads&key="+getKey();try{ajx(e,function(e){if(e.items[0].contentDetails.relatedPlaylists.uploads){var t="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId="+e.items[0].contentDetails.relatedPlaylists.uploads+"&maxResults=50&fields=items/snippet/resourceId/videoId&key="+getKey();ajx(t,function(e){if(e.items){for(var t=0;e.items[t];t++){pushViews("https://www.googleapis.com/youtube/v3/videos?part=statistics&id="+e.items[t].snippet.resourceId.videoId+"&fields=items/statistics/viewCount&key="+getKey(),t)}getScript("https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js",function(){isChart=1,fx("showextra").fadeOut(),document.getElementById("hideextra").style.display="block",document.getElementById("extraContent").style.display="block",extraswitch=1;for(var e=[],n=[],a=0;t<vids;a++)e[a]=views[a],n[a]="";var i={labels:n,datasets:[{label:"Views of last "+vids+" videos",fill:!1,borderColor:"rgba(255,50,50,0.5)",pointBorderColor:"rgba(255,50,50,0.5)",pointBackgroundColor:"rgba(255,50,50,1)",data:e}]};myLineChart2=new Chart(document.getElementById("myChart2").getContext("2d"),{type:"line",data:i,gridLines:{display:!1},responsive:!0,maintainAspectRatio:!1});var o=[function(){for(var e=0,t=0;t<vids;t++)e+=Number(views[t]);return e}(),function(){for(var e=0,t=vids;t<2*vids;t++)e+=Number(views[t]);return e}()],s=[Math.floor(o[0]/vids),Math.floor(o[1]/vids)],l={labels:["last "+vids+" videos","last to last "+vids+" videos"],datasets:[{label:"Average Views",borderColor:["rgba(0,0,255,1)","rgba(0,255,0,1)"],backgroundColor:["rgba(50,50,255,0.2)","rgba(50,255,50,0.2)"],hoverBackgroundColor:["rgba(0,0,255,1)","rgba(0,255,0,1)"],data:s}]};myLineChart3=new Chart(document.getElementById("myChart3").getContext("2d"),{type:"bar",data:l,gridLines:{display:!1},responsive:!0,maintainAspectRatio:!1});var r={labels:["last "+vids+" videos (total views)","last to last "+vids+" videos (total views)"],datasets:[{label:"Total Views",borderColor:["rgba(0,0,255,1)","rgba(0,255,0,1)"],backgroundColor:["rgba(50,50,255,0.2)","rgba(50,255,50,0.2)"],hoverBackgroundColor:["rgba(0,0,255,1)","rgba(0,255,0,1)"],data:[o[0],o[1]]}]};myLineChart4=new Chart(document.getElementById("myChart4").getContext("2d"),{type:"doughnut",data:r,gridLines:{display:!1},responsive:!0,maintainAspectRatio:!1});var d="https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+username+"&fields=items/statistics(videoCount,viewCount)&key="+getKey();ajx(d,function(e){e.items[0].statistics.videoCount&&e.items[0].statistics.viewCount?(changeText(document.getElementById("totalVideos"),e.items[0].statistics.videoCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")),changeText(document.getElementById("totalViews"),e.items[0].statistics.viewCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g,","))):noConnection("4. undef b.items[0].statistics.videoCount or b.items[0].statistics.viewCount in extrabutton(script.js)")}),firstload=1,upCharts()})}else noConnection("3. undef e.items in extrabutton(script.js)")})}else noConnection("2. undef e.items[0].contentDetails.relatedPlaylists.uploads in extrabutton(script.js)")},function(){noConnection("1. extrabutton no response from ajx (script.js)")})}catch(e){noConnection(e)}}else changeText("showextra","Show Stats"),0===extraswitch?(isChart=1,fx("showextra").fadeOut(),fx("hideextra").fadeIn(),document.getElementById("extraContent").style.display="block",extraswitch=1):(myLineChart1.destroy(),fx("showextra").fadeIn(),fx("hideextra").fadeOut(100),document.getElementById("extraContent").style.display="none",extraswitch=0,isChart=0);else tutorial(0)}function upCharts(){if(firstload){(vids=Number(getText("vids")))>25&&changeText("vids",vids=25);for(var e=0,t=0,n=0;n<vids;n++)myLineChart2.data.labels[n]="",myLineChart2.data.datasets[0].data[n]=views[n],e+=Number(views[n]);for(n=vids;n<2*vids;n++)t+=Number(views[n]);myLineChart2.data.labels.splice(vids),myLineChart2.data.datasets[0].data.splice(vids),myLineChart2.data.datasets[0].label="Views of last "+vids+" videos",myLineChart3.data.labels=["last "+vids+" videos","last to last "+vids+" videos"],myLineChart3.data.datasets[0].data[0]=Math.floor(e/vids),myLineChart3.data.datasets[0].data[1]=Math.floor(t/vids),myLineChart4.data.labels=["last "+vids+" videos (total views)","last to last "+vids+" videos (total views)"],myLineChart4.data.datasets[0].data[0]=e,myLineChart4.data.datasets[0].data[1]=t,myLineChart2.update(),myLineChart3.update(),myLineChart4.update()}else noConnection("upCharts was called before firstload==1",!0)}internet&&document.querySelectorAll(".share").forEach(function(e){e.style.backgroundImage="url(/images/social.png)"});