diff --git a/src/lib/components/workspace/Functions.svelte b/src/lib/components/workspace/Functions.svelte index 153f031a3..8dba99d09 100644 --- a/src/lib/components/workspace/Functions.svelte +++ b/src/lib/components/workspace/Functions.svelte @@ -43,8 +43,26 @@ let showDeleteConfirm = false; - const shareHandler = async (tool) => { - console.log(tool); + const shareHandler = async (item) => { + toast.success($i18n.t('Redirecting you to OpenWebUI Community')); + + const url = 'https://openwebui.com'; + + const tab = await window.open(`${url}/tools/create`, '_blank'); + + // Define the event handler function + const messageHandler = (event) => { + if (event.origin !== url) return; + if (event.data === 'loaded') { + tab.postMessage(JSON.stringify(item), '*'); + + // Remove the event listener after handling the message + window.removeEventListener('message', messageHandler); + } + }; + + window.addEventListener('message', messageHandler, false); + console.log(item); }; const cloneHandler = async (func) => { diff --git a/src/lib/components/workspace/Tools.svelte b/src/lib/components/workspace/Tools.svelte index bdf84e3b2..1aaec2629 100644 --- a/src/lib/components/workspace/Tools.svelte +++ b/src/lib/components/workspace/Tools.svelte @@ -39,8 +39,26 @@ let showDeleteConfirm = false; - const shareHandler = async (tool) => { - console.log(tool); + const shareHandler = async (item) => { + toast.success($i18n.t('Redirecting you to OpenWebUI Community')); + + const url = 'https://openwebui.com'; + + const tab = await window.open(`${url}/tools/create`, '_blank'); + + // Define the event handler function + const messageHandler = (event) => { + if (event.origin !== url) return; + if (event.data === 'loaded') { + tab.postMessage(JSON.stringify(item), '*'); + + // Remove the event listener after handling the message + window.removeEventListener('message', messageHandler); + } + }; + + window.addEventListener('message', messageHandler, false); + console.log(item); }; const cloneHandler = async (tool) => { diff --git a/src/routes/(app)/workspace/functions/create/+page.svelte b/src/routes/(app)/workspace/functions/create/+page.svelte index 033bdc22f..118668c83 100644 --- a/src/routes/(app)/workspace/functions/create/+page.svelte +++ b/src/routes/(app)/workspace/functions/create/+page.svelte @@ -36,6 +36,18 @@ }; onMount(() => { + window.addEventListener('message', async (event) => { + if ( + !['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes( + event.origin + ) + ) + return; + + func = JSON.parse(event.data); + console.log(func); + }); + if (sessionStorage.function) { func = JSON.parse(sessionStorage.function); sessionStorage.removeItem('function'); diff --git a/src/routes/(app)/workspace/tools/create/+page.svelte b/src/routes/(app)/workspace/tools/create/+page.svelte index f5617c8cf..daa6b7acd 100644 --- a/src/routes/(app)/workspace/tools/create/+page.svelte +++ b/src/routes/(app)/workspace/tools/create/+page.svelte @@ -33,6 +33,18 @@ }; onMount(() => { + window.addEventListener('message', async (event) => { + if ( + !['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes( + event.origin + ) + ) + return; + + tool = JSON.parse(event.data); + console.log(tool); + }); + if (sessionStorage.tool) { tool = JSON.parse(sessionStorage.tool); sessionStorage.removeItem('tool');