0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

カスタムスクリプトをはじめてみる。

Last updated at Posted at 2023-04-05

MTブロックエディタを1から作ってみたいと思います。
練習練習。

カスタムスクリプト

カスタムスクリプト内に、<style>や<script>を書くことができるので、
ブロックエディタ上でスタイルやDOMを出力状態にして表示することができます。

スタイルを指定する

<style>
p { color:#cc0000;}
</style>

JavaScriptの基本形

以下が基本形です。

<script>
document.addEventListener("DOMContentLoaded", async () => {
    if (document.body.dataset.hasCompiledHtml) {
        return;
    }
    // ここに処理したいスクリプトを。
});
</script>

DOM構築完了後に実行

addEventListener を DOMContentLoadedイベント(DOMツリーの構築が完了したとき)を取得。

// アロー
document.addEventListener("DOMContentLoaded", async () => {

// function
document.addEventListener('DOMContentLoaded', async function() {

DOM構築完了後に実行

「出力されるHTML」で表示された場合には「document.body.dataset.hasCompiledHtml」の値が真になる。

if (document.body.dataset.hasCompiledHtml) {
    return;
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?