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

inline_script: inline script code to excute in PWA APP #4306

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions packages/pagebuilder/lib/ContentTypes/Html/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useStyle } from '@magento/venia-ui/lib/classify';
import { arrayOf, shape, string } from 'prop-types';
import { useHistory } from 'react-router-dom';
import handleHtmlContentClick from '../../handleHtmlContentClick';
import {useExecuteScripts} from '@magento/peregrine/lib/hooks/useExecuteScripts'

const toHTML = str => ({ __html: str });

Expand Down Expand Up @@ -60,12 +61,13 @@ const Html = props => {
const clickHandler = event => {
handleHtmlContentClick(history, event);
};

const setRef = useExecuteScripts(html);
return (
<div
ref={setRef}
style={dynamicStyles}
className={[classes.root, ...cssClasses].join(' ')}
dangerouslySetInnerHTML={toHTML(html)}
// dangerouslySetInnerHTML={toHTML(html)}
onClick={clickHandler}
onKeyDown={clickHandler}
role="presentation"
Expand Down
65 changes: 65 additions & 0 deletions packages/peregrine/lib/hooks/useExecuteScripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useRef, useEffect } from 'react';
import { nonceGenerator } from '../../lib/util/nonceGenerator';

const removeHtmlComments = html => {
return html.replace(/<!--[\s\S]*?-->/g, '');
};

export const useExecuteScripts = htmlContent => {
const containerRef = useRef(null);
const lastScriptTagRef = useRef(null);
const originalDocumentWrite = document.write;

useEffect(() => {
if (containerRef.current) {
/**
* Overriding the document.write function to make sure
* Javascript is writing the content in a place they intent to
* write instead of replacing the whole document.
*
*/

document.write = function(content) {
console.log(lastScriptTagRef.current);
if (lastScriptTagRef.current) {
const span = document.createElement('span');
const scriptNode = lastScriptTagRef.current;
span.innerHTML = content;
console.log(scriptNode);
if (scriptNode) scriptNode.after(span);
} else {
originalDocumentWrite.call(document, content);
}
};

// Remove HTML comments
const contentWithoutComments = removeHtmlComments(htmlContent);
containerRef.current.innerHTML = contentWithoutComments;

const scriptTags = containerRef.current.querySelectorAll('script');

scriptTags.forEach(scriptTag => {
const newScript = document.createElement('script');
const nonce = nonceGenerator(24);
newScript.type = scriptTag.type || 'text/javascript';
newScript.setAttribute('nonce', nonce);

if (scriptTag.src) {
newScript.src = scriptTag.src;
} else {
newScript.textContent = scriptTag.innerHTML;
}

lastScriptTagRef.current = newScript;

scriptTag.replaceWith(newScript);
});
}

return () => {
document.write = originalDocumentWrite;
};
}, [htmlContent]);

return containerRef;
};
3 changes: 2 additions & 1 deletion packages/peregrine/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { from } from '@apollo/client';
import * as RestApi from './RestApi';
import * as Util from './util';

Expand All @@ -12,7 +13,7 @@ export { useScrollLock } from './hooks/useScrollLock';
export { useSearchParam } from './hooks/useSearchParam';
export { useSort } from './hooks/useSort';
export { useTypePolicies } from './hooks/useTypePolicies';

export { useExecuteScripts } from './hooks/useExecuteScripts'
export {
WindowSizeContextProvider,
useWindowSize
Expand Down
6 changes: 6 additions & 0 deletions packages/peregrine/lib/util/nonceGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Function to generate a random nonce
export const nonceGenerator = length => {
const array = new Uint8Array(length);
window.crypto.getRandomValues(array);
return btoa(String.fromCharCode.apply(null, array)).slice(0, length);
};
Loading