MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / injectConfigIntoHtml

Function injectConfigIntoHtml

server/src/utils/html-config.ts:107–124  ·  view source on GitHub ↗
(html: string, user?: AppUser | null)

Source from the content-addressed store, hash-verified

105 * Also injects PostHog script if configured.
106 */
107export function injectConfigIntoHtml(html: string, user?: AppUser | null): string {
108 const configScript = getAppConfigScript(user);
109
110 // Add early error buffer (sync) and PostHog script (deferred) if API key is configured
111 const posthogScripts = POSTHOG_API_KEY
112 ? `${EARLY_ERROR_BUFFER_SCRIPT}\n<script src="/posthog-init.js" defer></script>`
113 : '';
114
115 // csrf.js patches fetch() to include X-CSRF-Token on state-changing requests
116 const csrfScript = `<script src="/csrf.js"></script>`;
117
118 const injectedScripts = `${configScript}\n${csrfScript}\n${posthogScripts}`;
119
120 if (html.includes("</head>")) {
121 return html.replace("</head>", `${injectedScripts}\n</head>`);
122 }
123 return html.replace("<body", `${injectedScripts}\n<body`);
124}
125
126/**
127 * Resolve path to a public HTML file.

Callers 2

serveHtmlWithConfigFunction · 0.85
serveHtmlWithMetaTagsFunction · 0.85

Calls 1

getAppConfigScriptFunction · 0.70

Tested by

no test coverage detected