FastComments.com

扩展开发

背景

FastComments 提供通过我们称为扩展的脚本来扩展核心功能的能力。

一个 Extension 可以向评论小部件添加额外的标记、事件监听器,并运行任意代码。

在这里你会找到我们在生产环境中使用的扩展示例,以及关于如何编写扩展的文档。


扩展的生命周期 Internal Link


在评论小部件开始获取第一批评论并呈现 UI 之前,会获取并调用每个扩展的脚本。

在初次加载时,以下数据将附加到扩展对象上:

  • config - 对 config 对象的引用。
  • translations - 对 translations 对象的引用。
  • commentsById - 对按 id 存储的所有评论的引用。
  • root - 对根 DOM 节点的引用。

扩展应覆盖所需的函数,评论小部件将在适当的时候调用这些函数。


定义扩展 Internal Link

最小可行的扩展如下:

一个简单的扩展
Copy CopyRun External Link
1
2(function () {
3 const extension = FastCommentsUI.extensions.find((extension) => {
4 return extension.id === 'my-extension';
5 });
6})();
7

为了这个示例,将其保存为 my-extension.js,并使其可以通过 https://example.com/my-extension.min.js 访问。

此扩展不会执行任何操作,除了在加载时它会获取由核心评论库创建的扩展对象。

Extension 对象是单例的,不会与其他扩展共享。

接下来,要加载我们的扩展,必须告知评论小部件有关它。例如:

Using a Custom Extension
Copy CopyRun External Link
1
2<script async src="https://cdn.fastcomments.com/js/embed-v2-async.min.js"></script>
3<div id="fastcomments-widget"></div>
4<script>
5window.fcConfigs = [{
6 "tenantId": "demo",
7 "extensions": [
8 {
9 "id": "my-extension",
10 "path": "https://example.com/my-extension.min.js"
11 }
12 ]
13}];
14</script>
15

有关功能示例,请参见下一节。


扩展对象 Internal Link


扩展对象包含以下定义:

扩展对象 JSDoc
Copy CopyRun External Link
1
2/**
3 * The FastCommentsUI extension object. Used for lazy-loading certain components. For example, the review system is not
4 * used by all customers, so we only load that extension when we want it.
5 *
6 * @typedef {Object} FastCommentsUIExtension
7 * @property {string} id
8 * @property {Element} scriptNode
9 * @property {Element} root - 小部件的根 DOM 节点。
10 * @property {string} [css]
11 * @property {Object} config - FastComments 配置对象。
12 * @property {Object} commentsById - 对按 id 存储的所有评论的引用,并实时更新。
13 * @property {Object} translations - 对所有翻译的引用。
14 * @property {Function} reRenderComment - 可调用以重新渲染评论的函数引用。
15 * @property {Function} removeCommentAndReRender - 可调用以从内存中移除评论并重新渲染相应 DOM 部分的函数引用。
16 * @property {Function} newBroadcastId - 可调用以创建新的广播 id 并将其添加到本地忽略的广播 id 列表中的函数引用。
17 * @property {FastCommentsUIExtensionSetupEventHandlers} [setupEventHandlers]
18 * @property {FastCommentsUIExtensionPrepareCommentForSavingCallback} [prepareCommentForSaving]
19 * @property {FastCommentsUIExtensionNewCommentCallback} [newComment]
20 * @property {FastCommentsUIExtensionReplyAreaFilter} [replyAreaFilter] - 过滤回复区域的 HTML。
21 * @property {FastCommentsUIExtensionWidgetFilter} [widgetFilter] - 在渲染时过滤整个小部件的 HTML。
22 * @property {FastCommentsUIExtensionCommentTopFilter} [commentFilter] - 在渲染前为每条评论过滤 HTML。
23 * @property {FastCommentsUIExtensionReplyAreaFilter} [commentMenuFilter] - 在渲染前为每条评论的菜单过滤 HTML。
24 * @property {FastCommentsUIExtensionMenuFilter} [menuFilter] - 在渲染时为整个小部件过滤 HTML。
25 * @property {FastCommentsUIExtensionReplyAreaTop} [replyAreaTop] - (LEGACY) 返回要添加到回复区域顶部的 HTML。
26 * @property {FastCommentsUIExtensionWidgetTopCallback} [widgetTop] - (LEGACY) 返回要添加到小部件顶部的 HTML。
27 * @property {FastCommentsUIExtensionCommentTopCallback} [commentTop] - (LEGACY) 返回要添加到评论元素顶部的 HTML。
28 * @property {FastCommentsUIExtensionCommentBottomCallback} [commentBottom] - (LEGACY) 返回要添加到评论元素底部的 HTML。
29 * @property {FastCommentsUIExtensionMenuBottomCallback} [menuBottom] - (LEGACY) 返回要添加到每条评论的菜单元素底部的 HTML。
30 * @property {FastCommentsUIExtensionRenderCallback} [onRender]
31 * @property {FastCommentsUIExtensionConnectionStatusCallback} [onLiveConnectionStatusUpdate]
32 * @property {FastCommentsUIExtensionInitialRenderCallback} [onInitialRenderComplete]
33 * @property {FastCommentsUIExtensionPresenceUpdateCallback} [onPresenceUpdate]
34 */
35
36/**
37 * @callback FastCommentsUIExtensionSetupEventHandlers
38 * @param {Element} element - 根元素。
39 * @param {Object.<string, Function>} clickListeners - 按类名组织的点击事件处理程序,可通过引用进行修改。
40 * @returns void
41 */
42
43/**
44 * @callback FastCommentsUIExtensionWidgetTopCallback
45 * @param {Object} moduleData
46 * @returns {string}
47 */
48
49/**
50 * @callback FastCommentsUIExtensionWidgetFilter
51 * @param {Object} moduleData
52 * @param {Object} html
53 * @returns {string}
54 */
55
56/**
57 * @callback FastCommentsUIExtensionCommentTopCallback
58 * @param {Object} comment
59 * @returns {string}
60 */
61
62/**
63 * @callback FastCommentsUIExtensionCommentTopFilter
64 * @param {Object} comment
65 * @param {string} html
66 * @returns {string}
67 */
68
69/**
70 * @callback FastCommentsUIExtensionCommentBottomCallback
71 * @param {Object} comment
72 * @returns {string}
73 */
74
75/**
76 * @callback FastCommentsUIExtensionMenuBottomCallback
77 * @param {Object} comment
78 * @returns {string}
79 */
80
81/**
82 * @callback FastCommentsUIExtensionMenuFilter
83 * @param {Object} comment
84 * @param {string} html
85 * @returns {string}
86 */
87
88/**
89 * @callback FastCommentsUIExtensionRenderCallback
90 * @returns {string}
91 */
92
93/**
94 * @callback FastCommentsUIExtensionConnectionStatusCallback
95 * @param {boolean} isConnected
96 * @returns {void}
97 */
98
99/**
100 * @callback FastCommentsUIExtensionInitialRenderCallback
101 * @returns {void}
102 */
103
104/**
105 * @callback FastCommentsUIExtensionReplyAreaTop
106 * @param {Object|null} currentUser
107 * @param {boolean} isSaving
108 * @param {boolean} isReplyOpen
109 * @param {string|null} parentId
110 * @returns {string}
111 */
112
113/**
114 * @callback FastCommentsUIExtensionReplyAreaFilter
115 * @param {Object|null} currentUser
116 * @param {boolean} isSaving
117 * @param {boolean} isReplyOpen
118 * @param {string|null} parentId
119 * @param {string|null} html
120 * @returns {string}
121 */
122
123/**
124 * @callback FastCommentsUIExtensionPrepareCommentForSavingCallback
125 * @param {Object} comment
126 * @param {string} parentId
127 */
128
129/**
130 * @callback FastCommentsUIExtensionNewCommentCallback
131 * @param {Object} comment
132 */
133
134/**
135 * @callback FastCommentsUIExtensionPresenceUpdateCallback
136 * @param {Object} update
137 */
138

扩展 API Internal Link

Extension 交互很简单,因为我们只需定义要调用的函数的引用。

基于前面的示例,假设我们想在每条评论的顶部添加 HTML:

一个简单的扩展 - 续篇
Copy CopyRun External Link
1
2(function () {
3 const extension = FastCommentsUI.extensions.find((extension) => {
4 return extension.id === 'my-extension';
5 });
6
7 extension.commentFilter = function(comment, html) {
8 return `<h3>The user's name is ${comment.commenterName}!</h3>` + html;
9 }
10})();
11

每当你以这种方式返回 HTML 时,它会通过一个 DOM 差分算法合并到 UI 中。

手动触发评论的重新渲染

我们可以等待初始页面加载完成,然后通过调用 reRenderComment 手动重新渲染某条评论:

重新渲染评论
Copy CopyRun External Link
1
2(function () {
3 const extension = FastCommentsUI.extensions.find((extension) => {
4 return extension.id === 'my-extension';
5 });
6
7 let renderCount = 0;
8
9 extension.commentFilter = function(comment, html) {
10 renderCount++;
11 return `<h3>The render count is ${renderCount}!</h3>` + html;
12 }
13
14 extension.onInitialRenderComplete = function() {
15 setInterval(function() {
16 extension.reRenderComment(extension.commentsById[Object.keys(extension.commentsById)[0]], function renderDone() {
17 console.log('Comment re-render done.');
18 });
19 }, 2000); // timeout not required, just an example.
20 }
21})();
22