WebflowのCMSで記事の読了時間を自動計算して表示する方法

Written by
John Doe
Published on
2025-02-16

table of contents

WebflowのCMSを使ってブログ記事を管理している方の中には、「この記事は◯分で読めます」と自動表示したい と思う方も多いのではないでしょうか?

今回は JavaScriptを使って記事の読了時間を自動計算し、Webflowのページ上に動的に表示する方法 を解説します。

実際に試行錯誤しながら実装した過程も含めて、詳しく解説していきますので、ぜひ参考にしてください!

1.ステップ 1: 記事の構造を確認する

まず、WebflowのCMSにおいて記事の本文がどのように配置されているか確認します。

必要な要素:

  • 記事本文のテキストが含まれる .article-content クラスのリッチテキストブロックを確認しましょう。これは記事のメインコンテンツを表示する部分です。

1-1.Webflowでの設定手順

  1. WebflowのDesigner(デザインエディター)を開く
  2. CMSコレクションページに移動(ブログ記事のテンプレートページなど)
  3. 記事本文を表示するリッチテキストブロックを選択
  4. 「クラス」を .article-content に設定する(既にある場合は確認のみ)

1-2.記事本文の要素を確認する方法

  1. ページを公開する or プレビューを開く
  2. ブラウザの開発ツール(F12キー)を開く
  3. 「Elements」タブで .article-content を検索
  4. 記事のテキストが正しく入っているか確認

また、読了時間を表示する .reading-time-container という div 要素をページ内に設置します。この要素は、JavaScript を使って計算した読了時間を動的に挿入するためのものです。

まず、Webflow のデザインモードで .reading-time-container を追加し、適切な位置に配置してください。例えば、記事タイトルのすぐ下などが適しています。

開発ツール (F12) を使って、記事本文が .article-content に適用されていることを確認しましょう。

console.log("記事本文:", document.querySelector(".article-content"));

このコードをコンソールで実行し、null ではなく、リッチテキストブロックのHTMLが表示されればOKです。

2.ステップ 2: JavaScriptで読了時間を計算する

Webflowのページで動的に読了時間を表示するには、JavaScriptを使います。

読了時間の計算には、一般的に1分間で500文字程度を読む という前提を採用します。

2-1. 初期実装(基本的な読了時間計算)

このスクリプトをWebflowの 「Before </body> タグ」エリア に追加すれば、ページが読み込まれた際に 記事本文の文字数を計算し、読了時間を .reading-time-container に挿入 できます。

document.addEventListener("DOMContentLoaded", function () {
    let articleContent = document.querySelector(".article-content");
    let readTimeContainer = document.querySelector(".reading-time-container");

    if (articleContent && readTimeContainer) {
        let text = articleContent.innerText.trim();
        let charCount = text.length;
        let readingSpeed = 500;
        let readingTime = Math.ceil(charCount / readingSpeed);
        readTimeContainer.textContent = `この記事は約 ${readingTime} 分で読めます。`;
    }
});
    }
});

3.ステップ 3: WebflowのCMSのロード遅延に対応する

Webflowでは CMSのデータが非同期でロードされるため、スクリプト実行時に記事本文がまだ読み込まれていない ことがあります。

この問題を解決するために、最大5秒間(100ms × 50回)記事のロードを待機する処理 を追加します。

3-1.修正後のJavaScript(Webflow向け最適化版)

document.addEventListener("DOMContentLoaded", function () {
    let articleContent = document.querySelector(".article-content");
    let readTimeContainer = document.querySelector(".reading-time-container");

    if (articleContent && readTimeContainer) {
        let text = articleContent.innerText.trim();
        let charCount = text.length;
        let readingSpeed = 500;
        let readingTime = Math.ceil(charCount / readingSpeed);
        readTimeContainer.textContent = `この記事は約 ${readingTime} 分で読めます。`;
    }
});
        readTimeContainer.style.display = "block";
        readTimeContainer.style.visibility = "visible";
        console.log("✅ 読了時間が設定されました:", readTimeContainer.textContent);
    }

    function observeContentChanges(articleContent, readTimeContainer) {
        let observer = new MutationObserver(() => {
            let text = articleContent.innerText.trim();
            let charCount = text.length;
            if (charCount > 0) {
                updateReadingTime(charCount, readTimeContainer);
            }
        });

        observer.observe(articleContent, { childList: true, subtree: true, characterData: true });
        observer.observe(readTimeContainer, { childList: true, subtree: true, characterData: true });
    }
});

4.ステップ 4: 読了時間が表示されない場合のデバッグ

4-1. .reading-time-container が見つからない

コンソールで次のコマンドを実行し、要素が存在するか確認してください。

console.log(document.querySelector(".reading-time-container"));

null なら要素がページに存在していない ので、Webflowで .reading-time-container を正しく追加してください。

4-2. CSSで非表示になっていないか確認

次のコードを実行し、要素が正しく表示されるか試します。

document.querySelector(".reading-time-container").style.display = "block";document.querySelector(".reading-time-container").style.visibility = "visible";

4-3. 記事本文の取得を確認

console.log(document.querySelector(".article-content")?.innerText);

ここで記事本文が取得できていれば、スクリプトは正常に動作している可能性が高いです。

5.まとめ

WebflowのCMSで読了時間を自動表示するには、JavaScriptを使って記事の文字数を計算する
Webflowの遅延ロードに対応するため、最大5秒間待機する setInterval を活用する
.reading-time-container が正しく設定されているか確認し、CSSの影響をチェックする
MutationObserver を使って、記事の内容が変わった場合にも対応

Relation

関連記事

This is some text inside of a div block.

【実体験】Webflow AIで制作時間1/3に!コード不要で『売れる』サイトを作る新常識

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

効率的にウェブ制作を習得する方法〜脳科学に基づいた最適な学習法〜

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Webflowでコーディングは必要?ノーコードでできることとできないことを徹底解説

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

WebflowのCMSで記事の読了時間を自動計算して表示する方法

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

【2024年最新】GSAPとは?Webflowとの統合で変わるアニメーション制作の未来 | 完全解説

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

WordPressの課題とWebflowの利点!移行のメリットを分析してみた結果・・・

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Firecrawlでウェブサイト全体のデータを抽出!基本理解と活用法まで徹底解説

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

[Latest Edition] Must-See Plugins List to Power Up Your Webflow Site

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

[2024 Edition] Explaining how to use Elementor for beginners! Build a full-scale site with WordPress

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

[Website production cost] Market price and breakdown as seen from actual examples

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Even beginners can earn 50,000 a month! How to start web production as a side job?

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Use your AI skills as a side job! 11 ways that even beginners can challenge are revealed to the public

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Realized with no-code technology! What future entrepreneurs should know about digital innovation

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

[From price to features] Comparative analysis between WebFlow and Studio! Which one should I choose?

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Get creative freedom with the Webflow code output feature

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Basic usage and features of Microsoft Copilot Studio

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

How to create a concept - the secrets of design that captivates customers

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

This is all you need to read to create a piano classroom website! Strategies for success and 5 case studies

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

New OpenAI feature: GPT customization

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Beginner's Guide to Prompt Engineering

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Build a website with Webflow! Anyone can easily create a site without coding

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Advantages of UI design using Webflow

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

The Evolution of Webflow: New Possibilities for Design, Development, and Collaboration

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Applied Skills in the AI Era: Experience Strategy and Prompt Design

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Powering up your website with Webflow: Fivetran customer stories

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

How to create a website to express yourself

This is some text inside of a div block.
7 min read
This is some text inside of a div block.

Responding to a Changing Market: A Global Consulting Firm's Perspective

This is some text inside of a div block.
7 min read

Let's start with a free consultation

大変申し訳ありません。私たちのリソースには限りがあり、一社一社に質の高いサービスを提供するため、現在【毎月先着5社様】限定で、この特別な条件(全額返金保証+無料相談)でのご案内とさせていただいております。

さらに、今このページをご覧のあなただけに、無料相談へお申し込みいただいた方限定で、通常5万円相当の【競合サイト分析&改善提案レポート】を無料でプレゼントいたします。

枠がすぐに埋まる可能性がありますので、お早めにお申し込みください。

プライバシーポリシーに同意し、まずは無料相談をおこないます
Thank you! Your submission has been received!
Oops! Something went long while appearing the form.