Skip to content

Commit c978131

Browse files
committed
fix(chrome): bugfix for chrome in full site editor
Complex bugfix involving javascript loading new CSS inside an iframe, as the last element
1 parent 33fd7a2 commit c978131

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

plugins/hc-styles/hc-styles.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
wp_register_style( 'hc-styles-fix-highlight', plugins_url( '/hc-styles/css/fix-for-highlight-bug.css' ) );
3333
wp_enqueue_style( 'hc-styles-fix-highlight' );
3434

35+
wp_enqueue_script("hc-append", plugins_url( '/hc-styles/js/append.js' ));
36+
3537
/* This is not in prod, let's not break anything by deploying this
3638
add_filter( 'comment_form_defaults', function ( $args ) {
3739
// i.e. different themes may have different form structures.

plugins/hc-styles/js/append.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)