
言語 🇯🇵 日本語
背景
API
Extensions の開発
コンテキスト
FastComments は、Extensions と呼ぶスクリプトを介してコア機能を拡張する機能を提供します。
An Extension はコメントウィジェットに追加のマークアップを加えたり、イベントリスナーを設定したり、任意のコードを実行したりできます。
ここでは、本番で使用している Extensions の例と、Extensions の書き方に関するドキュメントを紹介します。
拡張機能のライフサイクル 
各拡張機能のスクリプトは、コメントウィジェットが最初のコメントセットの取得とUIのレンダリングを開始する前にフェッチされ、実行されます。
初回読み込み時に、次のデータが extension オブジェクトに付与されます:
config-configオブジェクトへの参照。translations-translationsオブジェクトへの参照。commentsById- IDごとの全コメントへの参照。root- ルートDOMノードへの参照。
拡張機能は、コメントウィジェットが適切なタイミングで呼び出す関数を必要に応じてオーバーライドしてください。
拡張機能の定義 
作成できる最小の拡張機能は次のとおりです:
Run 
この例のために、これを my-extension.js として保存し、https://example.com/my-extension.min.js で公開してください。
この拡張機能は何もしません。読み込み時にコアのコメントライブラリによって作成された拡張オブジェクトを取得するだけです。
この Extension オブジェクトはシングルトンで、他の拡張機能とは共有されません。
次に、拡張機能を読み込むには、コメントウィジェットにそのことを伝える必要があります。例えば:
Run 
機能的な例については、次のセクションを参照してください。
拡張機能の例 
At FastComments, we write our own extensions, using the same API. You can view the unminified code for these extensions at the following endpoints:
ダークモード
The Dark Mode extension is conditionally loaded when a "dark" page is detected. This extension simply adds some CSS to the comment widget. This way we do not have to load the dark mode CSS when it is not needed.
https://fastcomments.com/js/comment-ui/extensions/comment-ui.dark.extension.js
モデレーション
When the current user is a moderator, we use the moderation extension.
This is a good example for adding click-based event listeners, making API requests, adding to the comment menu and comment areas.
https://fastcomments.com/js/comment-ui/extensions/comment-ui.moderation.extension.js
ライブチャット
The Live Chat extension (in combination with other configuration and styling) turns the comment widget into a live chat component.
https://fastcomments.com/js/comment-ui/extensions/live-chat.extension.js
拡張機能オブジェクト 
拡張オブジェクトは次の定義で構成されています:
Run 
拡張機能のAPI 
Interacting with the Extension is simple, as we simply define references to functions we want invoked.
To build off the example earlier, let's say we want to add HTML to the top of each comment:
Run 
Whenever you return HTML like this, it will get merged into the UI via a dom-diffing algorithm.
コメントの再レンダリングを手動でトリガーする
We can wait for the initial page load and manually re-render a comment by invoking reRenderComment:
Run 