כיצד לעבור בין מצב כהה למצב בהיר ב-Discourse משובץ

סיכום מבוסס בינה מלאכותית

Users mikeguo and kirupa are seeking a way to dynamically switch the theme (e.g., dark mode) of embedded Discourse forums to match their host site’s theme. They encounter cross-origin restrictions when trying to manipulate the iframe’s DOM or inject CSS, as iframe.contentDocument is null and direct access is blocked for security reasons. mikeguo notes no official reply yet, while kirupa gives a friendly bump for guidance. satonotdead points to an official solution limited to official themes, referencing embed.scss and a meta.discourse.org thread. Eventually, mikeguo confirms success using the latest official feature and directs others to a detailed solution for automatic dark mode in embedded comments. The key takeaway is that while direct DOM manipulation is blocked, Discourse provides official theming hooks — especially for embedded contexts — that can be leveraged via documented methods or community examples.

My website provides the dark mode button, so when button was click, I need to change the color theme of Dicourse Embeds to keep the same with my website.

But when I try to add class for body of embedded iframe, I found iframe.contentDocument is always null, and if use document.getElementById("discourse-embed-frame").contentWindow.document , the error as below:

I think there should be a way to config the embedded frame to allow cross-origin, but I still not found how to config after trying some config.

The other way maybe to use postMessage, like document.getElementById("discourse-embed-frame").contentWindow.postMessage("darkMode"), but there needs to be an handler to handle the darkMode message in script, I haven’t found where to add it.

I’m having a similar challenge. Were you able to find a workaround?

Because of the cross-origin issue, I can’t add any kind of marker on the iframe that my embedded CSS can use to figure out what colors to use:

Not resolved yet. I haven’t received an official reply either, so I don’t know if all the official friends are on vacation.

(Friendly bump to see if we have any official guidance :slight_smile: )

There is an official way but only for official themes: Styling of Embedded Discourse

That could help:

Using offical latest feature, I have implemented it.
If you still have requirements, you can refer to