Browse Source

implement query param passing between browser window and app iframe

Nariman Jelveh 1 năm trước cách đây
mục cha
commit
8100edbef9
3 tập tin đã thay đổi với 17 bổ sung0 xóa
  1. 1 0
      src/UI/UIDesktop.js
  2. 9 0
      src/helpers.js
  3. 7 0
      src/initgui.js

+ 1 - 0
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,

+ 9 - 0
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", {

+ 7 - 0
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;
+        }
     }
 
     //--------------------------------------------------------------------------------------