diff --git a/dynamix.unraid.net.plg b/dynamix.unraid.net.plg index 83d70f076..a42954ef3 100644 --- a/dynamix.unraid.net.plg +++ b/dynamix.unraid.net.plg @@ -176,7 +176,7 @@ This is work in progress. Use this for testing purposes only! Shadowbox.open({ - content:'https://dev.d1eohvtyc6gnee.amplifyapp.com/?ts=&guid=&state=&keyfile=®gen=&flashproduct=&flashvendor=®istered=&servername=&serverip=&internalport=&protocol=&site='+location.origin+'&email=', + content:'https://registration-dev.unraid.net/?ts=&guid=&state=&keyfile=®gen=&flashproduct=&flashvendor=®istered=&servername=&serverip=&internalport=&protocol=&site='+location.origin+'&email=', player: 'iframe', options: {modal: true, animate: false, displayNav: false, overlayOpacity: 0.8, viewportPadding: 0, showMovieControls: false} }); @@ -833,8 +833,41 @@ function handleMessage(e) { hideRegWizard(); return; } + + const SAFE_JSON_PARSE = (str) => { + try { + return [null, JSON.parse(str)]; + } catch (err) { + return [err]; + } + }; - var data = JSON.parse(e.data); + const [err, data] = SAFE_JSON_PARSE(e.data); + if (err) return false; // swallow json parse error + + const HANDLE_LICENSES = (data, e) => { + if (data.license) { + $.get('/webGui/include/InstallKey.php', {url: data.license}, function() { + console.log('New license key installed: ' + data.license); + const payload = { + event: 'LICENSE_SUCCESS', + message: 'New license key installed', + license: data.license, + }; + e.source.postMessage(JSON.stringify(payload), e.origin); + }).fail(function() { + console.error('Failed to license new key: ' + data.license); + const payload = { + event: 'LICENSE_ERROR', + message: 'Failed to license new key', + license: data.license, + }; + e.source.postMessage(JSON.stringify(payload), e.origin); + }); + } else { + console.error('KEY_PURCHASE event fired! but missing license data:', data); + } + }; switch (data.event) { case "CLOSE_SHADOWBOX": @@ -851,38 +884,15 @@ function handleMessage(e) { } $.post('/update.php', postargs, function() { console.log('dynamix/dynamix.cfg: Updated apikey under [remote] section'); - if (!data.license) { - setTimeout(function(){ window.location.href = '/Main'; }, 500); - } }).fail(function() { console.error('Failed to update apikey under [remote] section'); }); } - if (data.license) { - hideRegWizard(function (){ - $.get('/webGui/include/InstallKey.php', {url: data.license}, function() { - console.log('New license key installed: ' + data.license); - setTimeout(function(){ window.location.href = '/Main'; }, 500); - }).fail(function() { - console.error('Failed to license new key: ' + data.license); - }); - }); - } + // duplicate conditional so we don't get the error from HANDLE_LICENSES() + if (data.license) HANDLE_LICENSES(data, e); break; - case "INSTALL_KEY": case "KEY_PURCHASE": - if (data.license) { - hideRegWizard(function () { - $.get('/webGui/include/InstallKey.php', {url: data.license}, function() { - console.log('New license key installed: ' + data.license); - setTimeout(function(){ window.location.href = '/Main'; }, 500); - }).fail(function() { - console.error('Failed to license new key: ' + data.license); - }); - }); - } else { - console.error('KEY_PURCHASE event fired! but missing license data:', data); - } + HANDLE_LICENSES(data, e); break; case "GET_STATE": $.get('/plugins/dynamix.unraid.net/include/state.php', function(newstate) { @@ -898,6 +908,10 @@ function handleMessage(e) { console.error('Failed to unregister'); }); break; + case "RELOAD": + return window.location.reload(); + case "REDIRECT_MAIN": + return window.location.href = '/Main'; default: console.error('Unhandled event \'' + data.event + '\' fired. data:', data); break; @@ -992,16 +1006,16 @@ echo json_encode($arr);