|
| 1 | +document.addEventListener("DOMContentLoaded", function() { |
| 2 | + setTimeout(function() { document.body.insertAdjacentHTML('beforeend', '<link rel="stylesheet" id="hc-styles-fix-highlight-css" href="https://hcommons.org/app/plugins/hc-styles/css/fix-for-highlight-bug.css" media="all" />'); }, 3000) }); |
| 3 | + |
| 4 | +(function () { |
| 5 | + console.log("Injecting CSS"); |
| 6 | + const cssHref = 'https://hcommons.org/app/plugins/hc-styles/css/fix-for-highlight-bug.css'; |
| 7 | + const selector = 'iframe[name="editor-canvas"]'; |
| 8 | + |
| 9 | + function injectStylesheet(iframe) { |
| 10 | + try { |
| 11 | + const doc = iframe.contentDocument || iframe.contentWindow.document; |
| 12 | + |
| 13 | + const link = doc.createElement('link'); |
| 14 | + link.rel = 'stylesheet'; |
| 15 | + link.href = cssHref; |
| 16 | + link.type = 'text/css'; |
| 17 | + link.media = 'all'; |
| 18 | + |
| 19 | + doc.body.appendChild(link); |
| 20 | + console.log(doc); |
| 21 | + |
| 22 | + } catch (e) { |
| 23 | + console.error('Failed to inject CSS into iframe:', e); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + function waitForIframeAndInject() { |
| 28 | + const iframe = document.querySelector(selector); |
| 29 | + if (iframe && iframe.contentDocument && iframe.contentDocument.readyState === 'complete') { |
| 30 | + classes = iframe.contentDocument.body.classList; |
| 31 | + for (cls of classes) { |
| 32 | + console.log("Testing for: " + cls); |
| 33 | + if (cls == "block-editor-iframe__body") { |
| 34 | + console.log("Found block-editor-iframe__body. Injecting."); |
| 35 | + setTimeout(injectStylesheet(iframe), 500); |
| 36 | + return |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + console.log("Waiting..."); |
| 41 | + |
| 42 | + setTimeout(waitForIframeAndInject, 1000); // try again shortly |
| 43 | + |
| 44 | + |
| 45 | + } else { |
| 46 | + console.log("Waiting..."); |
| 47 | + setTimeout(waitForIframeAndInject, 1000); // try again shortly |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + window.addEventListener('load', waitForIframeAndInject); |
| 52 | + |
| 53 | +})(); |
0 commit comments