From 09afb0e843059c19805f7335e00b2752b114bbf4 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 9 Oct 2018 10:32:11 +0800 Subject: [PATCH] feat: support init thread --- README.md | 3 ++- src/disqus.js | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7b9c048..409ae67 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ - [x] 展示评论列表 - [x] 自动判断访客的 DISQUS 可用性选择不同模式 - [ ] 展示评论中的媒体内容 +- [ ] 自动初始化新页面(使用 `disqusjs.config.identifier`) - [ ] 提交新评论(匿名)(WIP) ## 使用 @@ -144,7 +145,7 @@ disqusjs.config {} ## 注意 -- DisqusJS 尚不支持初始化一个新页面,在新创建了页面以后需要先使用原版 Disqus 访问一次该页面完成初始化。 +- ~~DisqusJS 尚不支持初始化一个新页面,在新创建了页面以后需要先使用原版 Disqus 访问一次该页面完成初始化。~~DisqusJS 如果检测到当前页面没有初始化、会提示是否切换到 Disqus 完整模式进行初始化。 - DisqusJS 仅在当前域名首次访问时检测 Disqus 可用性并选择模式,并把结果持久化在 localStorage 中,之后访问都会沿用之前的模式。 ## 调试、进阶使用 & 开发相关 diff --git a/src/disqus.js b/src/disqus.js index 66c7246..2ff672d 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -402,14 +402,21 @@ xhr.send(); xhr.onload = function () { if (this.status == 200 || this.status == 304) { - var response = JSON.parse(this.responseText).response[0]; - disqusjs.page = { - id: response.id, - title: response.title, - isClosed: response.isClosed, - length: response.posts - }; - getComment(); + let res = JSON.parse(this.responseText).response; + console.log(res.length) + if (res.length === 1) { + var response = res[0]; + disqusjs.page = { + id: response.id, + title: response.title, + isClosed: response.isClosed, + length: response.posts + }; + getComment(); + } else { + document.getElementById('dsqjs-thread-not-init').classList.remove('dsqjs-hide'); + document.getElementById('dsqjs-init-thread').addEventListener('click', forceDisqus); + } } }; xhr.ontimeout = (e) => { @@ -420,6 +427,16 @@ }; } + /* + * Name: initThread() + * Description: init the thread when it is not + * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey] + */ + + /* let initThread = () => { + + } */ + /* * Name: getComment() * Description: get the comment content @@ -609,7 +626,8 @@

评论完整模式加载中...如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或使用评论基础模式

-

你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并尝试使用完整 Disqus 模式 | 强制完整 Disqus 模式

+

你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并尝试完整 Disqus 模式 | 强制完整 Disqus 模式

+

该 Thread 并没有初始化,是否 切换到完整 Disqus 模式 进行初始化?

评论基础模式出现错误,是否重载

这里冷冷清清的,一条评论都没有

@@ -623,7 +641,7 @@ */ - var disqusjsBaseTpl = `

评论完整模式加载中...如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或使用评论基础模式

你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并尝试使用完整 Disqus 模式 | 强制完整 Disqus 模式

评论基础模式出现错误,是否重载

这里冷冷清清的,一条评论都没有

    `; + var disqusjsBaseTpl = `

    评论完整模式加载中...如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或使用评论基础模式

    你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并尝试完整 Disqus 模式 | 强制完整 Disqus 模式

    该 Thread 并没有初始化,是否 切换到完整 Disqus 模式 进行初始化?

    评论基础模式出现错误,是否重载

    这里冷冷清清的,一条评论都没有

      `; document.getElementById('disqus_thread').innerHTML = disqusjsBaseTpl; disqusjs.mode = getLS('disqusjs_mode');