|
| 1 | +<!-- Definitive version lives at https://github.com/scientific-python/blog.scientific-python.org --> |
| 2 | +{{ $page := . }} |
| 3 | +<hr/> |
| 4 | +<div id='discourse-comments'></div> |
| 5 | +<script type="text/javascript"> |
| 6 | + // For an explanation of the flow of information here, see: |
| 7 | + // https://meta.discourse.org/t/automatic-dark-mode-for-embedded-comments-as-well/184347/19?u=stéfan_van_der_walt |
| 8 | + |
| 9 | + const discourse_url = "https://discuss.scientific-python.org"; |
| 10 | + |
| 11 | + // Send a message to the iframe to let it know the dark/light mode choice |
| 12 | + const notifyIFrameOfTheme = () => { |
| 13 | + const iframe = document.getElementById("discourse-embed-frame"); |
| 14 | + if (iframe && iframe.contentWindow) { |
| 15 | + iframe.contentWindow.postMessage( |
| 16 | + { |
| 17 | + theme: document.documentElement.getAttribute("data-theme") |
| 18 | + }, |
| 19 | + discourse_url |
| 20 | + ); |
| 21 | + } |
| 22 | + }; |
| 23 | + |
| 24 | + const observer = new MutationObserver((mutations) => { |
| 25 | + mutations.forEach((mutation) => { |
| 26 | + if (mutation.attributeName == "data-theme") { |
| 27 | + notifyIFrameOfTheme(); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + }); |
| 32 | + observer.observe(document.querySelector("html"), { |
| 33 | + subtree: false, |
| 34 | + attributes: true, |
| 35 | + }); |
| 36 | + |
| 37 | + // Call notifyIFrameOfTheme once we receive a message from Discourse |
| 38 | + // that the comments iframe has loaded |
| 39 | + const handleMessageListener = (event) => { |
| 40 | + var origin = event.origin; |
| 41 | + if ((origin === discourse_url) && (event.data == "iframe loaded")) { |
| 42 | + notifyIFrameOfTheme(); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + // Call on Discourse to create a comment iframe |
| 47 | + // See https://meta.discourse.org/t/embed-discourse-comments-on-another-website-via-javascript/31963 |
| 48 | + DiscourseEmbed = { |
| 49 | + discourseUrl: `${discourse_url}/`, |
| 50 | + {{ printf "discourseEmbedUrl: '%s'" $page.Permalink | safeJS }}, |
| 51 | + // className: 'CLASS_NAME', |
| 52 | + }; |
| 53 | + |
| 54 | + (function () { |
| 55 | + var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true; |
| 56 | + d.src = `${DiscourseEmbed.discourseUrl}javascripts/embed.js`; |
| 57 | + (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d); |
| 58 | + })(); |
| 59 | + |
| 60 | + window.addEventListener("message", handleMessageListener); |
| 61 | +</script> |
0 commit comments