From 8100edbef9367bbb33a0ba6e5847dec731f43744 Mon Sep 17 00:00:00 2001 From: Nariman Jelveh Date: Sat, 16 Mar 2024 19:57:33 -0700 Subject: [PATCH] implement query param passing between browser window and app iframe --- src/UI/UIDesktop.js | 1 + src/helpers.js | 9 +++++++++ src/initgui.js | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/src/UI/UIDesktop.js b/src/UI/UIDesktop.js index 2926f433..f16dbb2f 100644 --- a/src/UI/UIDesktop.js +++ b/src/UI/UIDesktop.js @@ -930,6 +930,7 @@ async function UIDesktop(options){ name: app_launched_from_url, readURL: qparams.get('readURL'), maximized: qparams.get('maximized'), + params: app_query_params ?? [], is_fullpage: window.is_fullpage_mode, window_options: { stay_on_top: false, diff --git a/src/helpers.js b/src/helpers.js index a12fb405..753f0620 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1904,6 +1904,7 @@ window.launch_app = async (options)=>{ // add app_instance_id to URL iframe_url.searchParams.append('puter.app_instance_id', uuid); + // add app_id to URL iframe_url.searchParams.append('puter.app.id', app_info.uuid); @@ -1939,6 +1940,14 @@ window.launch_app = async (options)=>{ else if(options.token){ iframe_url.searchParams.append('puter.auth.token', options.token); } + + // if options.params is set, add them to the URL as query params + if(options.params && options.params.length > 0){ + for (const property in options.params) { + iframe_url.searchParams.append(property, options.params[property]); + } + } + // Try to acquire app token from the server else{ let response = await fetch(window.api_origin + "/auth/get-user-app-token", { diff --git a/src/initgui.js b/src/initgui.js index 121594f4..2b939244 100644 --- a/src/initgui.js +++ b/src/initgui.js @@ -76,6 +76,13 @@ window.initgui = async function(){ const url_paths = window.location.pathname.split('/').filter(element => element); if(url_paths[0]?.toLocaleLowerCase() === 'app' && url_paths[1]){ window.app_launched_from_url = url_paths[1]; + + // get query params, any param that doesn't start with 'puter.' will be passed to the app + window.app_query_params = {}; + for (let [key, value] of url_query_params) { + if(!key.startsWith('puter.')) + app_query_params[key] = value; + } } //--------------------------------------------------------------------------------------