Skip to content

Instantly share code, notes, and snippets.

@ManotLuijiu
Last active August 8, 2024 06:02
Show Gist options
  • Save ManotLuijiu/0df73c81d9bf0bfed0178586eb2fe554 to your computer and use it in GitHub Desktop.
Save ManotLuijiu/0df73c81d9bf0bfed0178586eb2fe554 to your computer and use it in GitHub Desktop.
Disable Dark Mode in <iframe> Tag
useEffect(() => {
window.addEventListener('message', (e) => {
const { disableDarkMode, id } = e.data;
const botId = id;
if (disableDarkMode) {
document.body.style.backgroundColor = 'white';
document.body.style.color = 'black';
document.body.classList.remove('dark-mode');
}
// Logic of botId here
});
}, []);
<!DOCTYPE html>
<html lang="en">
<head>
<title>Moo AI Testing Bot</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<h1>Test AI ChatBot</h1>
<script>
/* Create DOM */
const iframe = document.createElement("iframe");
const iframeStyles = (styleString) => {
const style = document.createElement("style");
style.textContent = styleString;
document.head.append(style);
};
iframeStyles(`
.chat-iframe {
position: fixed;
bottom: 50px;
right: 50px;
border: none;
}
`);
iframe.src = "http://localhost:3000/chatbot";
iframe.classList.add("chat-frame");
document.body.appendChild(iframe);
/* END Create DOM */
/* Create Logic */
window.addEventListener("message", (e) => {
if (e.origin !== "http://localhost:3000") return null;
let dimensions = JSON.parse(e.data);
iframe.width = dimensions.width;
iframe.height = dimensions.height;
iframe.contentWindow.postMessage(
{
id: `06e0b2c2-xxxx-xxxx-bb14-6c47dfexxxxx`,
disableDarkMode: true,
},
"http://localhost:3000/"
);
});
// Send a message to disable dark mode
iframe.onload = () => {
iframe.contentWindow.postMessage(
{
disableDarkMode: true,
},
"http://localhost:3000/"
);
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment