Compare commits

...

1 Commits

Author SHA1 Message Date
Cursor Agent
207ad3c7bd fix: wrap custom script execution in try-catch to prevent ReferenceError
Fixes FORMBRICKS-TK

Custom scripts injected via CustomScriptsInjector can attempt to access
undefined global variables (like zp_token) before they're defined, causing
a ReferenceError that breaks the survey page.

This fix wraps inline script content in an IIFE with try-catch to gracefully
handle errors from undefined variables. Also adds error handlers for external
scripts that fail to load.

The survey page will now continue to function even when custom scripts
encounter runtime errors, with warnings logged to the console for debugging.
2026-03-17 18:30:40 +00:00

View File

@@ -56,11 +56,26 @@ export const CustomScriptsInjector = ({
newScript.setAttribute(attr.name, attr.value);
});
// Copy inline script content
// Copy inline script content with error handling
if (script.textContent) {
newScript.textContent = script.textContent;
// Wrap inline scripts in try-catch to prevent undefined variable errors
const wrappedContent = `
(function() {
try {
${script.textContent}
} catch (error) {
console.warn("[Formbricks] Custom script error:", error);
}
})();
`;
newScript.textContent = wrappedContent;
}
// Add error handler for external scripts
newScript.onerror = (error) => {
console.warn("[Formbricks] Custom script failed to load:", error);
};
document.head.appendChild(newScript);
});