Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWA-3260 :Unable to execute javascript tags which added in block in P… #4264

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 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,33 @@ 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);

const executeInlineScript = () => {
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);
}
}
}
};
useEffect(() => {
executeInlineScript();
});
if (shouldShowLoadingIndicator) {
return <CMSPageShimmer classes={classes} />;
}
Expand Down