From 63b010991a3b606c4b0311e85f0672eeac8451e6 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 1 Aug 2017 22:45:25 +0530 Subject: Simplify code. * viewtube.js: Remove unnecessary parantheses for single statements. Combine statements with OR operator. Use partial application of functions. --- viewtube.js | 98 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 41 insertions(+), 57 deletions(-) diff --git a/viewtube.js b/viewtube.js index 681fabd..c18a548 100644 --- a/viewtube.js +++ b/viewtube.js @@ -103,6 +103,11 @@ } function playDASHwithHTML5() { + function playAudio (play) { + if (play) player["contentAudio"].play(); + else player["contentAudio"].pause(); + } + if (player['videoPlay'].indexOf('MP4') != -1) { player["contentVideo"] = createVideoElement({src: player["videoList"][player["videoPlay"].replace(/MP4/, "Video MP4")]}); if (player['videoList']['High Bitrate Audio Opus']) @@ -120,23 +125,17 @@ else player["contentAudio"] = createVideoElement({src: player["videoList"]["Medium Bitrate Audio WebM"]}); } player['contentAudio'].pause(); - player['contentVideo'].addEventListener('play', function() { - player['contentAudio'].play(); - }, false); - player['contentVideo'].addEventListener('pause', function() { - player['contentAudio'].pause(); - }, false); + player['contentVideo'].addEventListener('play', playAudio.bind(null, true), false); + player['contentVideo'].addEventListener('pause', playAudio.bind(null, false), false); player['contentVideo'].addEventListener('ended', function() { player['contentVideo'].pause(); player['contentAudio'].pause(); }, false); player['contentVideo'].addEventListener('timeupdate', function() { - if (player['contentAudio'].paused && !player['contentVideo'].paused) { + if (player['contentAudio'].paused && !player['contentVideo'].paused) player['contentAudio'].play(); - } - if (Math.abs(player['contentVideo'].currentTime - player['contentAudio'].currentTime) >= 0.30) { + if (Math.abs(player['contentVideo'].currentTime - player['contentAudio'].currentTime) >= 0.30) player['contentAudio'].currentTime = player['contentVideo'].currentTime; - } }, false); player["contentAudio"].classList.add("hide"); player['contentVideo'].appendChild(player['contentAudio']); @@ -223,9 +222,7 @@ if (ytVideoUnavailable.className.indexOf('hid') == -1) { var ytAgeGateContent = document.querySelector("#watch7-player-age-gate-content"); if (!ytAgeGateContent) return; - else { - if(ytAgeGateContent.indexOf('feature=private_video') != -1) return; - } + else if (ytAgeGateContent.indexOf('feature=private_video') != -1) return; } } @@ -261,47 +258,39 @@ if (!ytPlayerWindow) console.log("Couldn't get the player element."); else { /* Get Video Thumbnail */ - var ytVideoThumb = getMyContent(page.url, 'link\\s+itemprop="thumbnailUrl"\\s+href="(.*?)"', false); - if (!ytVideoThumb) ytVideoThumb = getMyContent(page.url, 'meta\\s+property="og:image"\\s+content="(.*?)"', false); - if (!ytVideoThumb) { - var ytVideoID = page.url.match(/(\?|&)v=(.*?)(&|$)/); - if (ytVideoID) ytVideoThumb = 'https://img.youtube.com/vi/' + ytVideoID[2] + '/0.jpg'; - } + var ytVideoThumb = getMyContent(page.url, 'link\\s+itemprop="thumbnailUrl"\\s+href="(.*?)"', false) + || getMyContent(page.url, 'meta\\s+property="og:image"\\s+content="(.*?)"', false) + || ('https://img.youtube.com/vi/' + page.url.match(/(\?|&)v=(.*?)(&|$)/)[2] + '/0.jpg'); /* Get Videos Content */ var ytVideosEncodedFmts, ytVideosAdaptiveFmts, ytVideosContent, ytHLSVideos, ytHLSContent; - ytVideosEncodedFmts = getMyContent(page.url, '"url_encoded_fmt_stream_map":\\s*"(.*?)"', false); - if (!ytVideosEncodedFmts) ytVideosEncodedFmts = getMyContent(page.url, '\\\\"url_encoded_fmt_stream_map\\\\":\\s*\\\\"(.*?)\\\\"', false); - ytVideosAdaptiveFmts = getMyContent(page.url, '"adaptive_fmts":\\s*"(.*?)"', false); - if (!ytVideosAdaptiveFmts) ytVideosAdaptiveFmts = getMyContent(page.url, '\\\\"adaptive_fmts\\\\":\\s*\\\\"(.*?)\\\\"', false); - if (ytVideosEncodedFmts) { - ytVideosContent = ytVideosEncodedFmts; - } + ytVideosEncodedFmts = getMyContent(page.url, '"url_encoded_fmt_stream_map":\\s*"(.*?)"', false) + || getMyContent(page.url, '\\\\"url_encoded_fmt_stream_map\\\\":\\s*\\\\"(.*?)\\\\"', false); + ytVideosAdaptiveFmts = getMyContent(page.url, '"adaptive_fmts":\\s*"(.*?)"', false) + || getMyContent(page.url, '\\\\"adaptive_fmts\\\\":\\s*\\\\"(.*?)\\\\"', false); + if (ytVideosEncodedFmts) ytVideosContent = ytVideosEncodedFmts; else { - ytHLSVideos = getMyContent(page.url, '"hlsvp":\\s*"(.*?)"', false); - if (!ytHLSVideos) ytHLSVideos = getMyContent(page.url, '\\\\"hlsvp\\\\":\\s*\\\\"(.*?)\\\\"', false); + ytHLSVideos = getMyContent(page.url, '"hlsvp":\\s*"(.*?)"', false) + || getMyContent(page.url, '\\\\"hlsvp\\\\":\\s*\\\\"(.*?)\\\\"', false); if (ytHLSVideos) { ytHLSVideos = cleanMyContent(ytHLSVideos, false); - if (ytHLSVideos.indexOf('keepalive/yes/') != -1) ytHLSVideos = ytHLSVideos.replace('keepalive/yes/', ''); + if (ytHLSVideos.indexOf('keepalive/yes/') != -1) + ytHLSVideos = ytHLSVideos.replace('keepalive/yes/', ''); } else { - var ytVideoID = page.url.match(/(\?|&)v=(.*?)(&|$)/); - ytVideoID = (ytVideoID) ? ytVideoID[2] : null; + var ytVideoID = page.url.match(/(\?|&)v=(.*?)(&|$)/)[2]; if (ytVideoID) { var ytVideoSts = getMyContent(page.url.replace(/watch.*?v=/, 'embed/').replace(/&.*$/, ''), '"sts"\\s*:\\s*(\\d+)', false); var ytVideosInfoURL = page.win.location.protocol + '//' + page.win.location.hostname + '/get_video_info?video_id=' + ytVideoID + '&eurl=https://youtube.googleapis.com/v/' + ytVideoID + '&sts=' + ytVideoSts; var ytVideosInfo = getMyContent(ytVideosInfoURL, 'TEXT', false); if (ytVideosInfo) { - ytVideosEncodedFmts = ytVideosInfo.match(/url_encoded_fmt_stream_map=(.*?)&/); - ytVideosEncodedFmts = (ytVideosEncodedFmts) ? ytVideosEncodedFmts[1] : null; - if (ytVideosEncodedFmts) { - ytVideosEncodedFmts = cleanMyContent(ytVideosEncodedFmts, true); - ytVideosContent = ytVideosEncodedFmts; - } + ytVideosEncodedFmts = ytVideosInfo.match(/url_encoded_fmt_stream_map=(.*?)&/)[1]; + if (ytVideosEncodedFmts) + ytVideosContent = cleanMyContent(ytVideosEncodedFmts, true); if (!ytVideosAdaptiveFmts) { - ytVideosAdaptiveFmts = ytVideosInfo.match(/adaptive_fmts=(.*?)&/); - ytVideosAdaptiveFmts = (ytVideosAdaptiveFmts) ? ytVideosAdaptiveFmts[1] : null; - if (ytVideosAdaptiveFmts) ytVideosAdaptiveFmts = cleanMyContent(ytVideosAdaptiveFmts, true); + ytVideosAdaptiveFmts = ytVideosInfo.match(/adaptive_fmts=(.*?)&/)[1]; + if (ytVideosAdaptiveFmts) + ytVideosAdaptiveFmts = cleanMyContent(ytVideosAdaptiveFmts, true); } } } @@ -396,17 +385,17 @@ var ytVideoFound = false; var ytVideos = ytVideosContent.split(','); var ytVideoParse, ytVideoCodeParse, ytVideoCode, myVideoCode, ytVideo; - for (var i = 0; i < ytVideos.length; i++) { - if (!ytVideos[i].match(/^url/)) { - ytVideoParse = ytVideos[i].match(/(.*)(url=.*$)/); - if (ytVideoParse) ytVideos[i] = ytVideoParse[2] + '&' + ytVideoParse[1]; + for (var ytVideo of ytVideos) { + if (!ytVideo.match(/^url/)) { + ytVideoParse = ytVideo.match(/(.*)(url=.*$)/); + if (ytVideoParse) ytVideo = ytVideoParse[2] + '&' + ytVideoParse[1]; } - ytVideoCodeParse = ytVideos[i].match(/itag=(\d{1,3})/); + ytVideoCodeParse = ytVideo.match(/itag=(\d{1,3})/); ytVideoCode = (ytVideoCodeParse) ? ytVideoCodeParse[1] : null; if (ytVideoCode) { myVideoCode = ytVideoFormats[ytVideoCode]; if (myVideoCode) { - ytVideo = cleanMyContent(ytVideos[i], true); + ytVideo = cleanMyContent(ytVideo, true); ytVideo = ytVideo.replace(/url=/, '').replace(/&$/, ''); if (ytVideo.match(/itag=/) && ytVideo.match(/itag=/g).length > 1) { if (ytVideo.match(/itag=\d{1,3}&/)) ytVideo = ytVideo.replace(/itag=\d{1,3}&/, ''); @@ -438,7 +427,7 @@ ytVideo = cleanMyContent(ytVideo, true); if (ytVideo.indexOf('ratebypass') == -1) ytVideo += '&ratebypass=yes'; if (ytVideo && ytVideo.indexOf('http') == 0) { - if (!ytVideoFound) ytVideoFound = true; + ytVideoFound = true; ytVideoList[myVideoCode] = ytVideo; } } @@ -483,8 +472,7 @@ var ytHLSMatcher = new RegExp('(http.*?m3u8)', 'g'); ytHLSVideos = ytHLSContent.match(ytHLSMatcher); if (ytHLSVideos) { - for (var i = 0; i < ytHLSVideos.length; i++) { - ytHLSVideo = ytHLSVideos[i]; + for (var ytHLSVideo in ytHLSVideos) { ytVideoCodeParse = ytHLSVideo.match(/\/itag\/(\d{1,3})\//); ytVideoCode = (ytVideoCodeParse) ? ytVideoCodeParse[1] : null; if (ytVideoCode) { @@ -508,8 +496,8 @@ var ytVideoList = {}; if (ytVideosContent) { if (ytVideosContent.match(/&s=/) || ytVideosContent.match(/,s=/) || ytVideosContent.match(/u0026s=/)) { - var ytScriptURL = getMyContent(page.url, '"js":\\s*"(.*?)"', true); - if (!ytScriptURL) ytScriptURL = getMyContent(page.url.replace(/watch.*?v=/, 'embed/').replace(/&.*$/, ''), '"js":\\s*"(.*?)"', true); + var ytScriptURL = getMyContent(page.url, '"js":\\s*"(.*?)"', true) + || getMyContent(page.url.replace(/watch.*?v=/, 'embed/').replace(/&.*$/, ''), '"js":\\s*"(.*?)"', true); if (ytScriptURL) { ytScriptURL = page.win.location.protocol + ytScriptURL; try { @@ -529,9 +517,7 @@ } else console.log("Couldn't get the signature content."); }; - oReq.onerror = function() { - console.log("Couldn't make the request. Make sure your browser user scripts extension supports cross-domain requests."); - }; + oReq.onerror = console.log.bind(null, "Couldn't make the request. Make sure your browser user scripts extension supports cross-domain requests."); oReq.send(); } catch(e) { @@ -541,9 +527,7 @@ } else console.log("Couldn't get the signature link."); } - else { - ytVideos(); - } + else ytVideos(); } else { if (ytHLSVideos) { -- cgit v1.2.3