1
0
Fork 0
mirror of synced 2024-09-20 03:08:18 +12:00

use postMessage instead of window object

This commit is contained in:
Martin McKeaveney 2024-08-19 18:01:56 +01:00
parent 74efb0bce2
commit 89051da102
5 changed files with 267 additions and 420 deletions

View file

@ -1,5 +1,4 @@
#!/bin/bash
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null && pwd 2> /dev/null; )"
if [[ $TARGETARCH == arm* ]] ;
then

View file

@ -825,11 +825,11 @@ const getEmbedBindings = () => {
bindings = [
{
type: "context",
runtimeBinding: `${safeEmbed}.`,
readableBinding: `ParentData`,
runtimeBinding: `${safeEmbed}`,
readableBinding: `ParentWindow`,
category: "Embed",
icon: "DevicePhone",
display: { type: "object", name: "Parent Data" },
icon: "DistributeVertically",
display: { type: "object", name: "Parent Window" },
},
]
return bindings

View file

@ -1,11 +1,41 @@
<script>
import Provider from "./Provider.svelte"
import { onMount } from "svelte"
let data = {}
export function extractDomainFromUrl(url) {
const { hostname } = new URL(url)
const parts = hostname.split('.');
const tld = parts.slice(-2).join(".")
return tld
}
export function handleMessage(event) {
// Validate the event origin to ensure it's coming from a trusted source
// Allow different subdomains but must match TLD
const appOrigin = extractDomainFromUrl(window.location.origin)
const eventOrigin = extractDomainFromUrl(event.origin)
if (appOrigin !== eventOrigin) {
data = event.data
} else {
console.error(
`Embedded budibase app domain ${appOrigin} does not match origin of event ${eventOrigin}.
Top level domains must match`
)
}
}
onMount(() => {
window.addEventListener("message", handleMessage)
return () => window.removeEventListener("message", handleMessage)
})
$: data = window.parent?.data
$: console.log("parentWindow", data)
</script>
<Provider key="embed" {data}>
<slot />
</Provider>
</Provider>

@ -1 +1 @@
Subproject commit 7dbe323aec724ae6336b13c06aaefa4a89837edf
Subproject commit 14c89a5b20ee4de07723063458a2437b0dd4f2c9

640
yarn.lock

File diff suppressed because it is too large Load diff