'
- d.getElementById('dsqjs-force-dsqjs').addEventListener('click', forceDsqjs);
-
- s.src = 'https://' + disqusjs.config.shortname + '.disqus.com/embed.js';
- s.setAttribute('data-timestamp', + new Date());
- (d.head || d.body).appendChild(s);
- }
+ /*
+ * loadDisqus() - 加载 Disqus
+ */
+ function loadDisqus() {
+ let d = document;
+ let s = d.createElement('script');
- function checkDisqus() {
- // 测试 Disqus 的域名
- // *.disquscdn.com 没有被墙所以不做检查
- let domain = ['disqus.com', `${disqusjs.config.shortname}.disqus.com`],
- test = 0,
- success = 0;
-
- let checker = () => {
- // 测试域名数量 ==== 测试次数 === 成功次数
- // 如果 truw 则认定可以 Disqus 可以连通
- if ((domain.length === test) && (test === success)) {
- forceDisqus()
- // 否则认为 Disqus 无法连通
- } else if (domain.length === test) {
- forceDsqjs()
- }
- // 如果测试域名数量不等于测试次数则说明测试还没有完成,不执行任何操作
- }
+ // 显示提示信息
+ // Disqus 加载成功以后会把 #disqus_thread 内的内容全部覆盖
+ d.getElementById('disqus_thread').innerHTML = '
评论完整模式加载中...如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或使用
评论基础模式'
+ d.getElementById('dsqjs-force-dsqjs').addEventListener('click', forceDsqjs);
- for (let i of domain) {
- ((i) => {
- let img = new Image;
- // 处理加载超时
- let timeout = setTimeout(() => {
- img.onerror = img.onload = null;
- test++;
- checker()
- }, 3000);
-
- img.onerror = () => {
- clearTimeout(timeout);
- test++;
- checker()
- }
+ s.src = 'https://' + disqusjs.config.shortname + '.disqus.com/embed.js';
+ s.setAttribute('data-timestamp', + new Date());
+ (d.head || d.body).appendChild(s);
+ }
- img.onload = () => {
- clearTimeout(timeout);
- test++;
- success++;
- checker()
+ function checkDisqus() {
+ // 测试 Disqus 的域名
+ // *.disquscdn.com 没有被墙所以不做检查
+ let domain = ['disqus.com', `${disqusjs.config.shortname}.disqus.com`],
+ test = 0,
+ success = 0;
+
+ let checker = () => {
+ // 测试域名数量 ==== 测试次数 === 成功次数
+ // 如果 truw 则认定可以 Disqus 可以连通
+ if ((domain.length === test) && (test === success)) {
+ forceDisqus()
+ // 否则认为 Disqus 无法连通
+ } else if (domain.length === test) {
+ forceDsqjs()
}
+ // 如果测试域名数量不等于测试次数则说明测试还没有完成,不执行任何操作
+ }
- img.src = `https://${i}/favicon.ico?${+(new Date)}`
- })(i);
- }
- }
+ for (let i of domain) {
+ ((i) => {
+ let img = new Image;
+ // 处理加载超时
+ let timeout = setTimeout(() => {
+ img.onerror = img.onload = null;
+ test++;
+ checker()
+ }, 3000);
+
+ img.onerror = () => {
+ clearTimeout(timeout);
+ test++;
+ checker()
+ }
- function loadDsqjs() {
- (() => {
- // 在 #disqus_thread 中填充 DisqusJS Container
- /*
-
- */
+ img.onload = () => {
+ clearTimeout(timeout);
+ test++;
+ success++;
+ checker()
+ }
- let d = document;
- d.getElementById('disqus_thread').innerHTML = `
`;
- // DisqusJS 加载中信息
- d.getElementById('dsqjs-msg').innerHTML = `评论基础模式加载中。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 | 。`
- d.getElementById('dsqjs-reload-disqus').addEventListener('click', checkDisqus);
- d.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus);
+ img.src = `https://${i}/favicon.ico?${+(new Date)}`
+ })(i);
+ }
+ }
- /*
- * 获取 Thread 信息
- * Disqus API 只支持通过 Thread ID 获取评论列表,所以必须先通过 identifier 获取当前页面 Thread ID
- *
- * API Docs: https://disqus.com/api/docs/threads/list/
- * API URI: /3.0/threads/list.json?forum=[disqus_shortname]&thread=ident:[identifier]&api_key=[apikey]
- */
- let url = `${disqusjs.config.api}3.0/threads/list.json?forum=${disqusjs.config.shortname}&thread=ident:${disqusjs.config.identifier}&api_key=${disqusjs.config.apikey}`;
-
- get(url, (res) => {
- var d = document;
- // 如果只返回一条则找到了对应 thread,否则是当前 identifier 不能找到唯一的 thread
- // 如果 thread 不唯一则需要进行初始化
- if (res.code === 0 && res.response.length === 1) {
- var resp = res.response[0];
- disqusjs.page = {
- id: resp.id,
- title: resp.title,
- isClosed: resp.isClosed,
- length: resp.posts,
- comment: []
- };
+ function loadDsqjs() {
+ (() => {
+ // 在 #disqus_thread 中填充 DisqusJS Container
+ /*
+
+ */
- // 填充站点名称和评论数目
- d.getElementById('dsqjs-comment-num').innerHTML = disqusjs.page.length
+ let d = document;
+ d.getElementById('disqus_thread').innerHTML = `
`;
+ // DisqusJS 加载中信息
+ d.getElementById('dsqjs-msg').innerHTML = `评论基础模式加载中。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 | 。`
+ d.getElementById('dsqjs-reload-disqus').addEventListener('click', checkDisqus);
+ d.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus);
- if (disqusjs.config.siteName) {
- d.getElementById('dsqjs-site-name').innerHTML = disqusjs.config.siteName
+ /*
+ * 获取 Thread 信息
+ * Disqus API 只支持通过 Thread ID 获取评论列表,所以必须先通过 identifier 获取当前页面 Thread ID
+ *
+ * API Docs: https://disqus.com/api/docs/threads/list/
+ * API URI: /3.0/threads/list.json?forum=[disqus_shortname]&thread=ident:[identifier]&api_key=[apikey]
+ */
+ let url = `${disqusjs.config.api}3.0/threads/list.json?forum=${disqusjs.config.shortname}&thread=ident:${disqusjs.config.identifier}&api_key=${disqusjs.config.apikey}`;
+
+ get(url, (res) => {
+ var d = document;
+ // 如果只返回一条则找到了对应 thread,否则是当前 identifier 不能找到唯一的 thread
+ // 如果 thread 不唯一则需要进行初始化
+ if (res.code === 0 && res.response.length === 1) {
+ var resp = res.response[0];
+ disqusjs.page = {
+ id: resp.id,
+ title: resp.title,
+ isClosed: resp.isClosed,
+ length: resp.posts,
+ comment: []
+ };
+
+ // 填充站点名称和评论数目
+ d.getElementById('dsqjs-comment-num').innerHTML = disqusjs.page.length
+
+ if (disqusjs.config.siteName) {
+ d.getElementById('dsqjs-site-name').innerHTML = disqusjs.config.siteName
+ }
+
+ // 获取评论列表
+ getComment()
+ } else if (res.code === 0 && res.response.length !== 1) {
+ // 当前页面可能还未初始化(需要创建 thread)
+ // Disqus API 的 threads/create 需要在服务端发起请求,不支持 AJAX Call
+ d.getElementById('dsqjs-msg').innerHTML = `该 Thread 并没有初始化,是否 进行初始化?`
+ d.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus);
+ } else {
+ // 评论列表加载错误
+ loadError()
}
-
- // 获取评论列表
- getComment()
- } else if (res.code === 0 && res.response.length !== 1) {
- // 当前页面可能还未初始化(需要创建 thread)
- // Disqus API 的 threads/create 需要在服务端发起请求,不支持 AJAX Call
- d.getElementById('dsqjs-msg').innerHTML = `该 Thread 并没有初始化,是否 进行初始化?`
- d.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus);
- } else {
+ }, (e) => {
// 评论列表加载错误
loadError()
- }
- }, (e) => {
- // 评论列表加载错误
- loadError()
- })
- })()
+ })
+ })()
- /*
- * getComment(cursor) - 获取评论列表
- *
- * @param {string} cursor - 传入 cursor 用于加载下一页的评论
- */
- let getComment = (cursor) => {
- let getMoreComment = () => {
- // 执行完以后去除当前监听器避免重复调用
- $loadMoreBtn.removeEventListener('click', getMoreComment);
- // 加载下一页评论
- getComment(disqusjs.page.next);
- }
- var $loadMoreBtn = document.getElementById('dsqjs-load-more');
- // 处理传入的 cursor
- if (!cursor) {
- // 不存在 cursor,API 中不需要带上 cursor 参数
- cursor = '';
- var getCommentError = () => {
- // 不存在 cursor,出错时只需要在 #dsq-msg 中显示提示信息
- loadError();
- }
- } else {
- // 带上 cursor 参数
- cursor = `&cursor=${cursor}`;
- var getCommentError = () => {
- // 解禁 加载更多评论
- $loadMoreBtn.classList.remove('dsqjs-disabled');
- // 在按钮上显示提示信息
- $loadMoreBtn.innerHTML = '加载更多评论失败,点击重试'
- }
- }
- // 在发起请求前禁用 加载更多评论 按钮防止重复调用
- $loadMoreBtn.classList.add('dsqjs-disabled')
/*
- * 获取评论列表
- *
- * API Docs: https://disqus.com/api/docs/posts/list/
- * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey]
+ * getComment(cursor) - 获取评论列表
*
- * https://github.com/SukkaW/DisqusJS/issues/6
- * 可以使用 include=deleted 来获得已被删除评论列表
+ * @param {string} cursor - 传入 cursor 用于加载下一页的评论
*/
- let url = `${disqusjs.config.api}3.0/posts/list.json?forum=${disqusjs.config.shortname}&thread=${disqusjs.page.id}${cursor}&include=approved&include=deleted&api_key=${disqusjs.config.apikey}`;
- get(url, (res) => {
- if (res.code === 0 && res.response.length > 0) {
- // 解禁 加载更多评论
- $loadMoreBtn.classList.remove('dsqjs-disabled');
-
- // 将获得的评论数据和当前页面已有的评论数据合并
- disqusjs.page.comment = disqusjs.page.comment.concat(res.response);
- // 用当前页面的所有评论数据进行渲染
- renderComment(disqusjs.page.comment)
-
-
- if (res.cursor.hasNext) {
- // 将 cursor.next 存入 disqusjs 变量中供不能传参的不匿名函数使用
- disqusjs.page.next = res.cursor.next;
- // 确保 加载更多评论按钮 文字正常
- $loadMoreBtn.innerHTML = '加载更多评论'
- // 显示 加载更多评论 按钮
- $loadMoreBtn.classList.remove('dsqjs-hide');
- $loadMoreBtn.addEventListener('click', getMoreComment);
- } else {
- // 没有更多评论了,确保按钮隐藏
- $loadMoreBtn.classList.add('dsqjs-hide');
- }
- } else if (res.code === 0 && res.response.length === 0) {
- // 当前没有评论,显示提示信息
- let d = document;
- d.getElementById('dsqjs-msg').innerHTML = '你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 | 。'
- d.getElementById('dsqjs-header').classList.remove('dsqjs-hide')
- d.getElementById('dsqjs-post-container').innerHTML = ''
- d.getElementById('dsqjs-reload-disqus').addEventListener('click', checkDisqus);
- d.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus);
- } else {
- // 评DisqusJS 加载错误
- getCommentError()
- }
- }, (e) => {
- // 评论列表加载错误
- getCommentError()
- })
- }
-
- /*
- * parseCommentData(data) - 解析评论列表
- *
- * @param {Array} data - 评论列表 JSON
- * @return {Array} - 解析后的评论列表数据
- */
- let parseCommentData = (data) => {
- var topLevelComments = [],
- childComments = [];
-
- let getChildren = (id) => {
- // 如果没有子评论,就不需要解析子评论了
- if (childComments.length === 0) {
- return null;
+ let getComment = (cursor) => {
+ let getMoreComment = () => {
+ // 执行完以后去除当前监听器避免重复调用
+ $loadMoreBtn.removeEventListener('click', getMoreComment);
+ // 加载下一页评论
+ getComment(disqusjs.page.next);
}
-
- var list = [];
- for (let comment of childComments) {
- if (comment.parent === id) {
- list.unshift({
- comment,
- author: comment.author.name,
- // 如果不设置 admin 会返回 undefined,所以需要嘴一个判断
- isPrimary: (disqusjs.config.admin ? (comment.author.username === disqusjs.config.admin) : false),
- children: getChildren(+comment.id)
- });
+ var $loadMoreBtn = document.getElementById('dsqjs-load-more');
+ // 处理传入的 cursor
+ if (!cursor) {
+ // 不存在 cursor,API 中不需要带上 cursor 参数
+ cursor = '';
+ var getCommentError = () => {
+ // 不存在 cursor,出错时只需要在 #dsq-msg 中显示提示信息
+ loadError();
}
- }
-
- if (list.length) {
- return list;
} else {
- return null;
+ // 带上 cursor 参数
+ cursor = `&cursor=${cursor}`;
+ var getCommentError = () => {
+ // 解禁 加载更多评论
+ $loadMoreBtn.classList.remove('dsqjs-disabled');
+ // 在按钮上显示提示信息
+ $loadMoreBtn.innerHTML = '加载更多评论失败,点击重试'
+ }
}
+ // 在发起请求前禁用 加载更多评论 按钮防止重复调用
+ $loadMoreBtn.classList.add('dsqjs-disabled')
+ /*
+ * 获取评论列表
+ *
+ * API Docs: https://disqus.com/api/docs/posts/list/
+ * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey]
+ *
+ * https://github.com/SukkaW/DisqusJS/issues/6
+ * 可以使用 include=deleted 来获得已被删除评论列表
+ */
+ let url = `${disqusjs.config.api}3.0/posts/list.json?forum=${disqusjs.config.shortname}&thread=${disqusjs.page.id}${cursor}&include=approved&include=deleted&api_key=${disqusjs.config.apikey}`;
+ get(url, (res) => {
+ if (res.code === 0 && res.response.length > 0) {
+ // 解禁 加载更多评论
+ $loadMoreBtn.classList.remove('dsqjs-disabled');
+
+ // 将获得的评论数据和当前页面已有的评论数据合并
+ disqusjs.page.comment = disqusjs.page.comment.concat(res.response);
+ // 用当前页面的所有评论数据进行渲染
+ renderComment(disqusjs.page.comment)
+
+
+ if (res.cursor.hasNext) {
+ // 将 cursor.next 存入 disqusjs 变量中供不能传参的不匿名函数使用
+ disqusjs.page.next = res.cursor.next;
+ // 确保 加载更多评论按钮 文字正常
+ $loadMoreBtn.innerHTML = '加载更多评论'
+ // 显示 加载更多评论 按钮
+ $loadMoreBtn.classList.remove('dsqjs-hide');
+ $loadMoreBtn.addEventListener('click', getMoreComment);
+ } else {
+ // 没有更多评论了,确保按钮隐藏
+ $loadMoreBtn.classList.add('dsqjs-hide');
+ }
+ } else if (res.code === 0 && res.response.length === 0) {
+ // 当前没有评论,显示提示信息
+ let d = document;
+ d.getElementById('dsqjs-msg').innerHTML = '你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 | 。'
+ d.getElementById('dsqjs-header').classList.remove('dsqjs-hide')
+ d.getElementById('dsqjs-post-container').innerHTML = ''
+ d.getElementById('dsqjs-reload-disqus').addEventListener('click', checkDisqus);
+ d.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus);
+ } else {
+ // 评DisqusJS 加载错误
+ getCommentError()
+ }
+ }, (e) => {
+ // 评论列表加载错误
+ getCommentError()
+ })
}
- data.forEach((comment) => {
- // 如果没有 comment.parent 说明是第一级评论
- (comment.parent ? childComments : topLevelComments)['push'](comment);
- });
-
- let commentLists = topLevelComments.map((comment) => {
- return {
- comment,
- author: comment.author.name,
- isPrimary: (disqusjs.config.admin ? (comment.author.username === disqusjs.config.admin) : false),
- children: getChildren(+comment.id)
- };
- });
-
- return commentLists;
- }
-
- /*
- * renderCommentData(data) - 渲染评论列表
- *
- * @param {Array} data - 从 getComment() 获取到的 JSON
- */
- let renderComment = (data) => {
/*
- * processData(data) - 处理评论列表
+ * parseCommentData(data) - 解析评论列表
*
- * @param {Array} data - 解析后的评论列表 JSON
+ * @param {Array} data - 评论列表 JSON
+ * @return {Array} - 解析后的评论列表数据
*/
- let processData = (data) => {
- // 处理 Disqus Profile URL
- if (data.comment.author.profileUrl) {
- /*
- Avatar Element
-
-
-
-
- Author Element
-
- ${data.comment.author.name}
-
- */
- data.comment.avatarEl = `

`
- data.comment.authorEl = `
${data.comment.author.name}`
- } else {
- data.comment.avatarEl = `

`
- data.comment.authorEl = `
${data.comment.author.name}`
- }
+ let parseCommentData = (data) => {
+ var topLevelComments = [],
+ childComments = [];
+
+ let getChildren = (id) => {
+ // 如果没有子评论,就不需要解析子评论了
+ if (childComments.length === 0) {
+ return null;
+ }
+
+ var list = [];
+ for (let comment of childComments) {
+ if (comment.parent === id) {
+ list.unshift({
+ comment,
+ author: comment.author.name,
+ // 如果不设置 admin 会返回 undefined,所以需要嘴一个判断
+ isPrimary: (disqusjs.config.admin ? (comment.author.username === disqusjs.config.admin) : false),
+ children: getChildren(+comment.id)
+ });
+ }
+ }
- // 处理 Admin Label
- // 需要同时设置 isPrimary 和 adminLabel;admin 已经在 processData() 中做过判断了
- if (disqusjs.config.adminLabel && data.isPrimary) {
- data.comment.authorEl += `
${disqusjs.config.adminLabel}`;
+ if (list.length) {
+ return list;
+ } else {
+ return null;
+ }
}
- return data;
+ data.forEach((comment) => {
+ // 如果没有 comment.parent 说明是第一级评论
+ (comment.parent ? childComments : topLevelComments)['push'](comment);
+ });
+
+ let commentLists = topLevelComments.map((comment) => {
+ return {
+ comment,
+ author: comment.author.name,
+ isPrimary: (disqusjs.config.admin ? (comment.author.username === disqusjs.config.admin) : false),
+ children: getChildren(+comment.id)
+ };
+ });
+
+ return commentLists;
}
/*
- * removeDisqUs(msg) - 将 comment 中的短链接 disq.us 去除
- * @param {string} msg - 评论信息
- * @return {string} msg - 经过处理的评论信息
+ * renderCommentData(data) - 渲染评论列表
+ *
+ * @param {Array} data - 从 getComment() 获取到的 JSON
*/
- let removeDisqUs = (msg) => {
- // aMatcher - 只处理 Disqus 短链接
- var aMatcher = new RegExp(/
(.+?)<\/a>/gi),
- hrefMatcher = new RegExp(/href=\"(.+?)\"/gi)
- let link = (msg.match(aMatcher) || []);
- link.map((olink) => {
- // (.*) 是贪婪处理,会一致匹配到最后,可以用于匹配最后一次
- let link = olink.match(hrefMatcher)[0].replace(/href=\"https:\/\/disq.us\/url\?url=/g, '').replace(/(.*)"/, '$1');
- link = decodeURIComponent(link);
- link = link.replace(/(.*):(.*)cuid=(.*)/, '$1')
- msg = msg.replace(olink, `${link}`)
- })
+ let renderComment = (data) => {
+ /*
+ * processData(data) - 处理评论列表
+ *
+ * @param {Array} data - 解析后的评论列表 JSON
+ */
+ let processData = (data) => {
+ // 处理 Disqus Profile URL
+ if (data.comment.author.profileUrl) {
+ /*
+ Avatar Element
+
+
+
+
+ Author Element
+
+ ${data.comment.author.name}
+
+ */
+ data.comment.avatarEl = `

`
+ data.comment.authorEl = `
${data.comment.author.name}`
+ } else {
+ data.comment.avatarEl = `

`
+ data.comment.authorEl = `
${data.comment.author.name}`
+ }
- // 在最后添加 target="_blank" 可以生效到全局链接(包括 Disqus CDN 直链)
- msg = msg.replace(/href=/g, `target="_blank" href=`)
- return msg
- }
+ // 处理 Admin Label
+ // 需要同时设置 isPrimary 和 adminLabel;admin 已经在 processData() 中做过判断了
+ if (disqusjs.config.adminLabel && data.isPrimary) {
+ data.comment.authorEl += `
${disqusjs.config.adminLabel}`;
+ }
- let renderPostItem = (s) => {
- if (s.isDeleted) {
- var authorEl = ``,
- message = `
此评论已被删除`;
- } else {
- var authorEl = `${s.authorEl}
`,
- message = s.message;
+ return data;
}
+
/*
-