diff --git a/doc/changelog.dox b/doc/changelog.dox index 5d7ee1b60..c7b8e56da 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -260,6 +260,9 @@ namespace Magnum { - Added @ref Platform::Sdl2Application::KeyEvent::keyName() "Platform::*Application::KeyEvent::keyName()" - It's now possible to iterate main loop manually using @ref Platform::Sdl2Application::mainLoopIteration() +- Ability to pass command-line arguments to Emscripten browser apps, see + @ref Platform::Sdl2Application and @ref Platform::WindowlessEglApplication + for more information ### Primitives library diff --git a/src/Magnum/Platform/EmscriptenApplication.js b/src/Magnum/Platform/EmscriptenApplication.js index 96717b5f8..fa467e1da 100644 --- a/src/Magnum/Platform/EmscriptenApplication.js +++ b/src/Magnum/Platform/EmscriptenApplication.js @@ -2,6 +2,8 @@ var Module = { preRun: [], postRun: [], + arguments: [], + printErr: function(message) { console.error(Array.prototype.slice.call(arguments).join(' ')); }, @@ -37,6 +39,20 @@ var Module = { } }; +/* Parse arguments, e.g. /app/?foo=bar&fizz&buzz=3 goes to the app as + ['--foo', 'bar', '--fizz', '--buzz', '3'] */ +var args = decodeURIComponent(window.location.search.substr(1)).trim().split('&'); +for(var i = 0; i != args.length; ++i) { + var j = args[i].indexOf('='); + /* Key + value */ + if(j != -1) { + Module.arguments.push('--' + args[i].substring(0, j)); + Module.arguments.push(args[i].substring(j + 1)); + + /* Just key */ + } else Module.arguments.push('--' + args[i]); +} + Module.setStatus('Downloading...'); Module.canvas.addEventListener('contextmenu', function(event) { diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index 97a468dbc..ea6b04444 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -229,7 +229,9 @@ The CSS file contains rudimentary style to avoid eye bleeding. The application redirects all output (thus also @ref Corrade::Utility::Debug "Debug", @ref Corrade::Utility::Warning "Warning" and @ref Corrade::Utility::Error "Error") -to JavaScript console. +to JavaScript console. It's possible to pass command-line arguments to `main()` +using GET URL parameters. For example, `/app/?foo=bar&fizz&buzz=3` will go to +the app as `['--foo', 'bar', '--fizz', '--buzz', '3']`. ## Usage with iOS diff --git a/src/Magnum/Platform/WindowlessEglApplication.h b/src/Magnum/Platform/WindowlessEglApplication.h index f3a661ee0..841885761 100644 --- a/src/Magnum/Platform/WindowlessEglApplication.h +++ b/src/Magnum/Platform/WindowlessEglApplication.h @@ -298,7 +298,9 @@ The CSS file contains rudimentary style to avoid eye bleeding. The application prints all output (thus also @ref Corrade::Utility::Debug "Debug", @ref Corrade::Utility::Warning "Warning" and @ref Corrade::Utility::Error "Error") -to the `<pre>` on the page. +to the `<pre>` on the page. It's possible to pass command-line arguments +to `main()` using GET URL parameters. For example, `/app/?foo=bar&fizz&buzz=3` +will go to the app as `['--foo', 'bar', '--fizz', '--buzz', '3']`. */ class WindowlessEglApplication { public: diff --git a/src/Magnum/Platform/WindowlessEmscriptenApplication.js b/src/Magnum/Platform/WindowlessEmscriptenApplication.js index adbd56a1f..f8a53fa65 100644 --- a/src/Magnum/Platform/WindowlessEmscriptenApplication.js +++ b/src/Magnum/Platform/WindowlessEmscriptenApplication.js @@ -2,6 +2,8 @@ var Module = { preRun: [], postRun: [], + arguments: [], + printErr: function(message) { var log = document.getElementById('log'); log.innerHTML += Array.prototype.slice.call(arguments).join(' ') + '\n'; @@ -39,6 +41,20 @@ var Module = { } }; +/* Parse arguments, e.g. /app/?foo=bar&fizz&buzz=3 goes to the app as + ['--foo', 'bar', '--fizz', '--buzz', '3'] */ +var args = decodeURIComponent(window.location.search.substr(1)).trim().split('&'); +for(var i = 0; i != args.length; ++i) { + var j = args[i].indexOf('='); + /* Key + value */ + if(j != -1) { + Module.arguments.push('--' + args[i].substring(0, j)); + Module.arguments.push(args[i].substring(j + 1)); + + /* Just key */ + } else Module.arguments.push('--' + args[i]); +} + Module.setStatus('Downloading...'); Module.canvas.addEventListener('contextmenu', function(event) {