Last active
August 8, 2024 06:02
-
-
Save ManotLuijiu/0df73c81d9bf0bfed0178586eb2fe554 to your computer and use it in GitHub Desktop.
Disable Dark Mode in <iframe> Tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
}, []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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