From 043904f88820656690bfb5783f33d54ccf30f5f9 Mon Sep 17 00:00:00 2001 From: jcorporation Date: Wed, 26 Sep 2018 22:25:51 +0100 Subject: [PATCH] Fix: base64 encode decode unicode string for popup menu #60 --- dist/htdocs/js/mympd.min.js | 4 ++-- htdocs/js/mympd.js | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/dist/htdocs/js/mympd.min.js b/dist/htdocs/js/mympd.min.js index 7f2383d..cea53af 100644 --- a/dist/htdocs/js/mympd.min.js +++ b/dist/htdocs/js/mympd.min.js @@ -110,7 +110,7 @@ function addToPlaylist(){var a=document.getElementById("addToPlaylistUri").value function addStream(){var a=document.getElementById("streamUrl").value;""!=a&&0==a.indexOf("http")?(sendAPI({cmd:"MPD_API_QUEUE_ADD_TRACK",data:{uri:a}}),modalAddToPlaylist.hide(),showNotification("Added stream "+a+"to queue","","","success")):(document.getElementById("streamUrl").classList.add("is-invalid"),document.getElementById("addStreamFrm").classList.add("was-validated"))} function showRenamePlaylist(a){document.getElementById("renamePlaylistFrm").classList.remove("was-validated");document.getElementById("renamePlaylistTo").classList.remove("is-invalid");modalRenamePlaylist.show();document.getElementById("renamePlaylistFrom").value=a;document.getElementById("renamePlaylistTo").value=""} function renamePlaylist(){var a=document.getElementById("renamePlaylistFrom").value,b=document.getElementById("renamePlaylistTo").value,c=b.replace(/\w\-/g,"");""!=b&&b!=a&&""==c?(sendAPI({cmd:"MPD_API_PLAYLIST_RENAME",data:{from:a,to:b}}),modalRenamePlaylist.hide(),sendAPI({cmd:"MPD_API_PLAYLIST_LIST",data:{offset:app.current.page,filter:app.current.filter}},parsePlaylists)):(document.getElementById("renamePlaylistTo").classList.add("is-invalid"),document.getElementById("renamePlaylistFrm").classList.add("was-validated"))} -function dirname(a){return a.replace(/\/[^\/]*$/,"")}function addMenuItem(a,b){return'"+b+""} +function dirname(a){return a.replace(/\/[^\/]*$/,"")}function b64EncodeUnicode(a){return btoa(encodeURIComponent(a).replace(/%([0-9A-F]{2})/g,function(a,c){return String.fromCharCode("0x"+c)}))}function b64DecodeUnicode(a){return decodeURIComponent(atob(a).split("").map(function(a){return"%"+("00"+a.charCodeAt(0).toString(16)).slice(-2)}).join(""))}function addMenuItem(a,b){return'"+b+""} function showMenu(a,b){b.preventDefault();b.stopPropagation();b=document.getElementsByClassName("popover");for(var c=0;ckeyboard_arrow_rightAlbum actions
'+addMenuItem({cmd:"appendQueue",options:[b,d,c]},"Append to queue")+addMenuItem({cmd:"appendAfterQueue", @@ -118,7 +118,7 @@ options:[b,d,e,c]},"Add after current playing song")+addMenuItem({cmd:"replaceQu addMenuItem({cmd:"playlistDetails",options:[d]},"Edit playlist"))+addMenuItem({cmd:"showRenamePlaylist",options:[d]},"Rename playlist")+addMenuItem({cmd:"delPlaylist",options:[d]},"Delete playlist"):"Browse"==app.current.app&&"Playlists"==app.current.tab&&"Detail"==app.current.view?(e=document.getElementById("BrowsePlaylistsDetailList"),f+=addMenuItem({cmd:"appendQueue",options:[b,d,c]},"Append to queue")+addMenuItem({cmd:"replaceQueue",options:[b,d,c]},"Replace queue")+("false"==e.getAttribute("data-ro")? addMenuItem({cmd:"removeFromPlaylist",options:[e.getAttribute("data-uri"),a.parentNode.parentNode.getAttribute("data-songpos")]},"Remove"):"")+("plist"!=b?addMenuItem({cmd:"showAddToPlaylist",options:[d]},"Add to playlist"):"")):"Queue"==app.current.app&&(f+=addMenuItem({cmd:"delQueueSong",options:["single",a.parentNode.parentNode.getAttribute("data-trackid")]},"Remove")+addMenuItem({cmd:"delQueueSong",options:["range",0,a.parentNode.parentNode.getAttribute("data-songpos")]},"Remove all upwards")+ addMenuItem({cmd:"delQueueSong",options:["range",parseInt(a.parentNode.parentNode.getAttribute("data-songpos"))-1,-1]},"Remove all downwards")+(-1==d.indexOf("http")?addMenuItem({cmd:"songDetails",options:[d]},"Songdetails"):""));new Popover(a,{trigger:"click",delay:0,dismissible:!0,template:'"});var g=a.Popover;a.getAttribute("data-init")||(a.setAttribute("data-init","true"),a.addEventListener("shown.bs.popover", -function(a){document.getElementsByClassName("popover-content")[0].addEventListener("click",function(a){a.preventDefault();a.stopPropagation();if("A"==a.target.nodeName&&(a=a.target.getAttribute("data-href"))){a=JSON.parse(atob(a));if("function"===typeof window[a.cmd])switch(a.cmd){case "sendAPI":sendAPI.apply(null,$jscomp.arrayFromIterable(a.options));break;default:window[a.cmd].apply(null,$jscomp.arrayFromIterable(a.options))}g.hide()}},!1);if(a=document.getElementById("advancedMenuLink"))a.addEventListener("click", +function(a){document.getElementsByClassName("popover-content")[0].addEventListener("click",function(a){a.preventDefault();a.stopPropagation();if("A"==a.target.nodeName&&(a=a.target.getAttribute("data-href"))){a=JSON.parse(b64DecodeUnicode(a));if("function"===typeof window[a.cmd])switch(a.cmd){case "sendAPI":sendAPI.apply(null,$jscomp.arrayFromIterable(a.options));break;default:window[a.cmd].apply(null,$jscomp.arrayFromIterable(a.options))}g.hide()}},!1);if(a=document.getElementById("advancedMenuLink"))a.addEventListener("click", function(a){a=this.getElementsByTagName("span")[0];a.innerText="keyboard_arrow_right"==a.innerText?"keyboard_arrow_down":"keyboard_arrow_right"},!1),new Collapse(a)},!1));g.show()} function sendAPI(a,b){var c=new XMLHttpRequest;c.open("POST","/api",!0);c.setRequestHeader("Content-type","application/json");c.onreadystatechange=function(){if(4==c.readyState)if(""!=c.responseText){var d=JSON.parse(c.responseText);"error"==d.type?(showNotification("Error",d.data,d.data,"danger"),console.log("Error: "+d.data)):"result"==d.type&&"ok"!=d.data?showNotification(d.data,"","","success"):void 0!=b&&"function"==typeof b&&b(d)}else console.log("Empty response for request: "+JSON.stringify(a))}; c.send(JSON.stringify(a))}function openLocalPlayer(){window.open("/player.html#"+settings.mpdstream,"LocalPlayer")}function updateDB(){sendAPI({cmd:"MPD_API_DATABASE_UPDATE"});updateDBstarted(!0)}function rescanDB(){sendAPI({cmd:"MPD_API_DATABASE_RESCAN"});updateDBstarted(!0)} diff --git a/htdocs/js/mympd.js b/htdocs/js/mympd.js index 954e265..0d8d8c5 100644 --- a/htdocs/js/mympd.js +++ b/htdocs/js/mympd.js @@ -1712,8 +1712,21 @@ function dirname(uri) { return uri.replace(/\/[^\/]*$/, ''); } +function b64EncodeUnicode(str) { + return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, + function toSolidBytes(match, p1) { + return String.fromCharCode('0x' + p1); + })); +} + +function b64DecodeUnicode(str) { + return decodeURIComponent(atob(str).split('').map(function(c) { + return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); + }).join('')); +} + function addMenuItem(href, text) { - return '' + text +''; + return '' + text +''; } function showMenu(el, event) { @@ -1799,7 +1812,7 @@ function showMenu(el, event) { if (event.target.nodeName == 'A') { var dh = event.target.getAttribute('data-href'); if (dh) { - var cmd = JSON.parse(atob(dh)); + var cmd = JSON.parse(b64DecodeUnicode(dh)); if (typeof window[cmd.cmd] === 'function') { switch(cmd.cmd) { case 'sendAPI':