Skip to content

Commit

Permalink
PWA-3260 :Unable to execute javascript tags which added in block in P…
Browse files Browse the repository at this point in the history
…WA Venia
  • Loading branch information
rudraswamy.c authored and rudraswamy.c committed May 8, 2024
1 parent b38b527 commit 1466903
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packages/venia-ui/lib/RootComponents/CMS/cms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react';
import React, { Fragment, useEffect } from 'react';
import { shape, string } from 'prop-types';

import CMSPageShimmer from './cms.shimmer';
Expand All @@ -12,11 +12,37 @@ import defaultClasses from './cms.module.css';

const CMSPage = props => {
const { identifier } = props;

const talonProps = useCmsPage({ identifier });
const { cmsPage, shouldShowLoadingIndicator } = talonProps;
const classes = useStyle(defaultClasses, props.classes);

useEffect(() => {
// Function to execute inline scripts safely
const executeInlineScripts = document => {
const scripts = document.getElementsByTagName('script');
for (let i = 0; i < scripts.length; i++) {
const scriptContent =
scripts[i].textContent || scripts[i].innerText;
if (scriptContent) {
try {
// Execute script in a sandboxed environment
const sandbox = {};
const codeToExecute = `(function() { ${scriptContent} })();`;
const executedScript = new Function(
'sandbox',
codeToExecute
);
executedScript(sandbox);
} catch (error) {
console.error('Error executing inline script:', error);
}
}
}
};

executeInlineScripts(document); // Pass the document or a specific element containing the content
}, [shouldShowLoadingIndicator]);

if (shouldShowLoadingIndicator) {
return <CMSPageShimmer classes={classes} />;
}
Expand Down

0 comments on commit 1466903

Please sign in to comment.