diff --git a/src/generated_scene/generator.rs b/src/generated_scene/generator.rs index 0285e97..d8a9282 100644 --- a/src/generated_scene/generator.rs +++ b/src/generated_scene/generator.rs @@ -347,18 +347,51 @@ function buildArtifact(args, rows) {{ }} const defaultDeps = {{ - validatePageContext: async () => true, - queryData: async (args) => {{ + validatePageContext(args) {{ + const host = (globalThis.location?.hostname || '').trim(); + const expected = (args.expected_domain || '').trim(); + if (!host) return {{ ok: false, reason: 'page_context_unavailable' }}; + if (host !== expected) return {{ ok: false, reason: 'page_context_mismatch' }}; + return {{ ok: true }}; + }}, + + async queryData(args) {{ const endpoint = API_ENDPOINTS[0]; if (!endpoint) throw new Error('No API endpoint configured'); const request = buildRequest(args, endpoint); - const response = await fetch(request.url, {{ - method: request.method, - headers: request.headers, - body: request.body - }}); - if (!response.ok) throw new Error(`HTTP ${{response.status}}: ${{response.statusText}}`); - return response.json(); + + // Prefer jQuery (internal pages typically have it) + if (typeof $ !== 'undefined' && typeof $.ajax === 'function') {{ + return new Promise((resolve, reject) => {{ + $.ajax({{ + url: request.url, + type: request.method, + data: request.body, + contentType: 'application/json', + dataType: 'json', + success: resolve, + error: (xhr, status, err) => reject(new Error( + `API failed (${{xhr.status}}): ${{err}} | body=${{(xhr.responseText || '').substring(0, 200)}}` + )) + }}); + }}); + }} + + // Fallback: fetch API + if (typeof fetch === 'function') {{ + const response = await fetch(request.url, {{ + method: request.method, + headers: request.headers, + body: request.method !== 'GET' ? request.body : undefined + }}); + if (!response.ok) {{ + const text = await response.text().catch(() => ''); + throw new Error(`HTTP ${{response.status}}: ${{text.substring(0, 200)}}`); + }} + return response.json(); + }} + + throw new Error('No HTTP client available (need jQuery or fetch)'); }} }};