From 2e8da967061d6294ff69b1a623c70b809790fa94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Sun, 7 Oct 2018 12:48:41 +0200 Subject: [PATCH] Moved oauth callback to /api/oauth and embed to /api/embed, with utilizing the search in url --- server/public/assets/js/callback.js | 2 +- server/public/assets/js/embed.js | 42 ++++++++++++++++-------- server/public/assets/js/functions.js | 2 +- server/public/assets/js/listeners.js | 49 +++++++++++++++++++--------- server/public/assets/js/search.js | 4 +-- server/routing/client/router.js | 34 +++++++++++-------- 6 files changed, 85 insertions(+), 48 deletions(-) diff --git a/server/public/assets/js/callback.js b/server/public/assets/js/callback.js index 432a17ba..53b47925 100755 --- a/server/public/assets/js/callback.js +++ b/server/public/assets/js/callback.js @@ -1,6 +1,6 @@ window.addEventListener("load", function() { var query = getQueryHash(window.location.hash); - var redirect = window.location.protocol + "//" + window.location.hostname + "/o_callback"; + var redirect = window.location.protocol + "//" + window.location.hostname + "/api/oauth"; var client_id; var response; var scope; diff --git a/server/public/assets/js/embed.js b/server/public/assets/js/embed.js index b473a776..eb4a7f20 100755 --- a/server/public/assets/js/embed.js +++ b/server/public/assets/js/embed.js @@ -52,10 +52,12 @@ var began = false; var seekTo; var socket; var video_id; -var hash = window.location.hash.substring(1).split("&"); -var chan = Helper.decodeChannelName(hash[0]); -var autoplay = false; -var color = "#808080"; +var embedOptions = getSearch(window.location.search); +var chan = Helper.decodeChannelName(embedOptions["channel"]); +var autoplay = embedOptions["autoplay"]; +var videoonly = embedOptions["videoonly"]; +var color = "#" + embedOptions["color"]; +var localmode = embedOptions["localmode"]; var dragging = false; var user_auth_started = false; var user_auth_avoid = false; @@ -98,14 +100,26 @@ window.addEventListener("message", receiveMessage, false); window.addEventListener("DOMContentLoaded", function() { }); -window.addEventListener("load", function() { - if(hash.length >= 2 && hash.indexOf("autoplay") > 0){ - autoplay = true; - Helper.css("#player", "visibility", "hidden"); - } else { - //paused = true; + +function getSearch(elem) { + var result = {}; + var search = window.location.search.split("&"); + for(var i = 0; i < search.length; i++) { + var currElement = search[i].split("="); + var key = currElement[0].replace("?", ""); + var value = currElement[1]; + if(value == "true") value = true; + else if(value == "false") value = false; + result[key] = value; } - if(hash.indexOf("videoonly") > -1) { + return result; +} + +window.addEventListener("load", function() { + if(autoplay){ + Helper.css("#player", "visibility", "hidden"); + } + if(videoonly) { Helper.addClass("#wrapper", "hide"); Helper.addClass("#controls", "hide"); Helper.addClass("#player", "video_only"); @@ -116,13 +130,13 @@ window.addEventListener("load", function() { M.Modal.init(document.getElementById("locked_channel"), { dismissible: false }); - color = "#" + hash[1]; + add = "https://zoff.me"; //if(window.location.hostname == "localhost") add = "localhost"; //add = "localhost"; socket = io.connect(''+add+':8080', connection_options); - if(hash.indexOf("localmode") > 1) { + if(localmode) { change_offline(true, false); } @@ -170,7 +184,7 @@ window.addEventListener("load", function() { //Helper.css("#controls", "background-color", color); document.querySelector("body").style.backgroundColor = color; - if(hash.indexOf("controll") > -1) { + if(embedOptions.hasOwnProperty("control") && embedOptions.control) { Hostcontroller.change_enabled(true); } else { Hostcontroller.change_enabled(false); diff --git a/server/public/assets/js/functions.js b/server/public/assets/js/functions.js index d627f160..111434a7 100644 --- a/server/public/assets/js/functions.js +++ b/server/public/assets/js/functions.js @@ -797,7 +797,7 @@ function disable_debug(){ function embed_code(autoplay, width, height, color, embed_videoonly, embed_localmode){ var autoplay_add = ""; if(autoplay == "&autoplay") autoplay_add = 'allow="autoplay"'; - return ''; + return ''; } function change_offline(enabled, already_offline){ diff --git a/server/public/assets/js/listeners.js b/server/public/assets/js/listeners.js index b10fb128..0259e71b 100755 --- a/server/public/assets/js/listeners.js +++ b/server/public/assets/js/listeners.js @@ -88,14 +88,14 @@ var not_import_html = ""; var not_export_html = ""; var embed_height = 300; var embed_width = 600; -var embed_videoonly = ""; -var embed_localmode = ""; -var embed_autoplay = "&autoplay"; +var embed_videoonly = "&videoonly=false"; +var embed_localmode = "&localmode=false"; +var embed_autoplay = "&autoplay=true"; var connect_error = false; var access_token_data_youtube = {}; var youtube_authenticated = false; var chromecastAvailable = false; -var color = "2d2d2d"; +var color = "&color=2d2d2d"; var find_start = false; var durationTimeout; var find_started = false; @@ -859,7 +859,7 @@ function addDynamicListeners() { addListener("input", '#color_embed', function(e){ var that = e; - color = that.value.substring(1); + color = "&color=" + that.value.substring(1); document.getElementById("embed-area").value = embed_code(embed_autoplay, embed_width, embed_height, color, embed_videoonly, embed_localmode); }); @@ -944,7 +944,7 @@ function addDynamicListeners() { youtube_window.close(); window.callback = ""; }; - youtube_window = window.open("/o_callback#youtube=true&nonce=" + nonce, "", "width=600, height=600"); + youtube_window = window.open("/api/oauth#youtube=true&nonce=" + nonce, "", "width=600, height=600"); } else { List.exportToYoutube(); } @@ -972,7 +972,7 @@ function addDynamicListeners() { spotify_window.close(); window.callback = ""; }; - spotify_window = window.open("/o_callback#spotify=true&nonce=" + nonce, "", "width=600, height=600"); + spotify_window = window.open("/api/oauth#spotify=true&nonce=" + nonce, "", "width=600, height=600"); }); addListener("submit", "#listImport", function(event){ @@ -1103,28 +1103,45 @@ function addDynamicListeners() { Search.importSpotifyPlaylist('https://api.spotify.com/v1/users/' + user + '/playlists/' + playlist_id + '/tracks'); } else { - before_toast(); - M.toast({html: "It seems you've entered a invalid url.", displayLength: 4000}); + var url = document.getElementById("import_spotify").value.split("https://open.spotify.com/playlist/"); + + if(url.length == 2) { + /*url = url[1].split("/"); + var user = url[0];*/ + var playlist_id = url[1]; + playlist_id = playlist_id.replace("?", ""); + + document.getElementById("import_spotify").disabled = true; + Helper.addClass("#import_spotify", "hide"); + Helper.removeClass("#playlist_loader_spotify", "hide"); + + ga('send', 'event', "import", "spotify"); + + Search.importSpotifyPlaylist('https://api.spotify.com/v1/playlists/' + playlist_id + '/tracks'); + } else { + before_toast(); + M.toast({html: "It seems you've entered a invalid url.", displayLength: 4000}); + } } } document.getElementById("import_spotify").value = ""; }); addListener("change", "#autoplay", function(e) { - if(e.checked) embed_autoplay = "&autoplay"; - else embed_autoplay = ""; + if(e.checked) embed_autoplay = "&autoplay=true"; + else embed_autoplay = "&autoplay=false"; document.getElementById("embed-area").value = embed_code(embed_autoplay, embed_width, embed_height, color, embed_videoonly, embed_localmode); }); addListener("change", "#videoonly", function(e) { - if(e.checked) embed_videoonly = "&videoonly"; - else embed_videoonly = ""; + if(e.checked) embed_videoonly = "&videoonly=true"; + else embed_videoonly = "&videoonly=false"; document.getElementById("embed-area").value = embed_code(embed_autoplay, embed_width, embed_height, color, embed_videoonly, embed_localmode); }); addListener("change", "#localmode", function(e) { - if(e.checked) embed_localmode = "&localmode"; - else embed_localmode = ""; + if(e.checked) embed_localmode = "&localmode=true"; + else embed_localmode = "&localmode=false"; document.getElementById("embed-area").value = embed_code(embed_autoplay, embed_width, embed_height, color, embed_videoonly, embed_localmode); }); @@ -1262,7 +1279,7 @@ function addDynamicListeners() { spotify_window.close(); window.callback = ""; }; - spotify_window = window.open("/o_callback#spotify=true&nonce=" + nonce, "", "width=600, height=600"); + spotify_window = window.open("/api/oauth#spotify=true&nonce=" + nonce, "", "width=600, height=600"); }); addListener("click", ".import-youtube", function(event){ diff --git a/server/public/assets/js/search.js b/server/public/assets/js/search.js index 652f945a..465c0cc2 100755 --- a/server/public/assets/js/search.js +++ b/server/public/assets/js/search.js @@ -498,7 +498,7 @@ importPlaylist: function(pId,pageToken){ youtube_window.close(); window.callback = ""; }; - youtube_window = window.open("/o_callback#youtube=true&nonce=" + nonce, "", "width=600, height=600"); + youtube_window = window.open("/api/oauth#youtube=true&nonce=" + nonce, "", "width=600, height=600"); } else { Helper.log([ "import list error: ", @@ -555,7 +555,7 @@ importPlaylist: function(pId,pageToken){ youtube_window.close(); window.callback = ""; }; - youtube_window = window.open("/o_callback#youtube=true&nonce=" + nonce, "", "width=600, height=600"); + youtube_window = window.open("/api/oauth#youtube=true&nonce=" + nonce, "", "width=600, height=600"); } else { Helper.log([ "import list error: ", diff --git a/server/routing/client/router.js b/server/routing/client/router.js index 6db27d49..dcce3345 100644 --- a/server/routing/client/router.js +++ b/server/routing/client/router.js @@ -57,6 +57,23 @@ router.route('/').post(function(req, res, next){ root(req, res, next); }); +router.route('/api/embed').get(function(req, res, next) { + var data = { + year: year, + javascript_file: "embed.min.js", + captcha: res.recaptcha, + analytics: analytics, + stylesheet: "embed.css", + embed: true, + og_image: "https://zoff.me/assets/images/small-square.jpg", + } + res.render('layouts/client/embed', data); +}); + +router.route('/api/oauth').get(function(req, res, next) { + res.sendFile(path.join(pathThumbnails, '/public/assets/html/callback.html')); +}); + router.route('/api/apply').get(function(req, res, next) { var data = { year: year, @@ -196,20 +213,9 @@ function channel(req, res, next) { } else if(subdomain.length >= 2 && subdomain[0] == "www") { res.redirect("https://zoff.me"); } else { - if(req.params.channel_name == "_embed") { - //res.sendFile(path.join(pathThumbnails, '/public/assets/html/embed.html')); - var data = { - year: year, - javascript_file: "embed.min.js", - captcha: res.recaptcha, - analytics: analytics, - stylesheet: "embed.css", - embed: true, - og_image: "https://zoff.me/assets/images/small-square.jpg", - } - res.render('layouts/client/embed', data); - } else if(req.params.channel_name == "o_callback") { - res.sendFile(path.join(pathThumbnails, '/public/assets/html/callback.html')); + if(req.params.channel_name == "o_callback") { + res.redirect("/api/oauth"); + //res.sendFile(path.join(pathThumbnails, '/public/assets/html/callback.html')); } else { /*db.collection("frontpage_lists").find({"_id": Functions.encodeChannelName(req.params.channel_name)}, function(err, docs) { console.log(docs);