Inject CSS into Blogger Editor Iframe with Tampermonkey

Preface

Previously improved the Blogger editor and added pre and code function buttons

Also improved the blog template to better display blockquote, pre, and code

Then I felt that the display effect inside the editor was a bit ugly. Mainly because the editor originally did not have pre and code functions, so there was no suitable display effect.

Blogger editor showing code-prettify highlighting and key functions


Approach

I thought, if I extract the CSS snippets from the template and apply them to the editor page, wouldn't that work?

First, I had the Agent help me extract the CSS for h2, p, blockquote, code, and pre from the template I made earlier, and got

h2 {
  border-bottom: 1px solid #e8eaed;
  padding-bottom: 6px;
}
p {    
  margin: 0.85em 0;
}
blockquote {
  margin: 1em 0;
  padding: 8px 16px;
  border-left: 4px solid #dadce0;
  color: #5f6368;
  background: #fafafa;
  line-height: 1.55;
}
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas,
    "Liberation Mono", monospace;
  background: #f1f3f4;
  border-radius: 4px;
  padding: 2px 5px;
}
pre {
  background: #f8f9fa;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 12px 14px;
  overflow-x: auto;
  line-height: 1.55;
}

I enabled the Stylebot plugin and prepared to insert CSS, but then found that the WYSIWYG area in the middle of the editor was an iframe

Blogger editor inner iframe applying CSS with 717px×2089px size shown

GPT-Oriented Development

I had originally planned to use Antigravity like last time, but was worried about not having enough quota.

I asked the free web account Claude

Is there a way to inject CSS into an iframe on a page?

Code example of injecting CSS styles into a same-origin iframe on a webpage

This is obviously a job for a Tampermonkey script.

Open the F12 developer tools and get this iframe's class="ZW3ZFc"

DevTools highlighting the iframe element with class ZW3ZFc in the Blogger editor

Then:

Write a Tampermonkey script,
Select the iframe element with class=ZW3ZFc
Insert CSS
...omitted


Result

Blogger editor showing hasCustomColor function adding prettyprint highlighting to pre


Github

https://github.com/crazypeace/blogger-editor-css/raw/refs/heads/main/blogger-editor-iframe-css.user.js


Comments