diff --git a/dist/disqus.js b/dist/disqus.js index 1303890..ad6ea8a 100644 --- a/dist/disqus.js +++ b/dist/disqus.js @@ -6,4 +6,4 @@ * Link: https://github.com/SukkaW/DisqusJS * License: GPL-3.0 */ -disqusjs.page=[],window.disqus_config=function(){this.page.url=disqusjs.config.url,this.page.identifier=disqusjs.config.identifier};var xhr=new XMLHttpRequest;setLS=function(e,s){try{localStorage.setItem(e,s)}catch(e){console.log(e),console.log("Failed to set localStorage item")}},getLS=function(e){return localStorage.getItem(e)},Date.prototype.Format=function(e){var s={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),S:this.getMilliseconds()};for(var t in/(y+)/.test(e)&&(e=e.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))),s)new RegExp("("+t+")").test(e)&&(e=e.replace(RegExp.$1,1==RegExp.$1.length?s[t]:("00"+s[t]).substr((""+s[t]).length)));return e},loadDisqus=function(){var e=document;e.getElementById("dsqjs-load-disqus").classList.remove("dsqjs-hide"),e.getElementById("dsqjs-force-dsqjs").addEventListener("click",forceDsqjs);var s=e.createElement("script");s.src="https://"+disqusjs.config.shortname+".disqus.com/embed.js",s.setAttribute("data-timestamp",+new Date),(e.head||e.body).appendChild(s)},checkDisqus=function(){var s=new Image,t=setTimeout(function(){s.onerror=s.onload=null,setLS("disqusjs_mode","dsqjs")},2e3);s.onerror=function(){clearTimeout(t),setLS("disqusjs_mode","dsqjs"),main()},s.onload=function(){clearTimeout(t);var e=setTimeout(function(){s.onerror=s.onload=null,setLS("disqusjs_mode","dsqjs")},2e3);s.onerror=function(){clearTimeout(e),setLS("disqusjs_mode","dsqjs"),main()},s.onload=function(){clearTimeout(e),setLS("disqusjs_mode","disqus"),main()},s.src="https://"+disqusjs.config.shortname+".disqus.com/favicon.ico?"+ +new Date},s.src="https://disqus.com/favicon.ico?"+ +new Date},forceDsqjs=function(){setLS("disqusjs_mode","dsqjs"),main()},forceDisqus=function(){setLS("disqusjs_mode","disqus"),main()},loadError=function(){document.getElementById("dsqjs-load-error").classList.remove("dsqjs-hide"),document.getElementById("dsqjs-loading-dsqjs").classList.add("dsqjs-hide"),document.getElementById("dsqjs-reload").addEventListener("click",getThreadInfo)},getThreadInfo=function(){document.getElementById("dsqjs-loading-dsqjs").classList.remove("dsqjs-hide"),document.getElementById("dsqjs-force-disqus").addEventListener("click",forceDisqus),document.getElementById("dsqjs-reload-disqus").addEventListener("click",checkDisqus);var e=disqusjs.config.api+"3.0/threads/list.json?forum="+disqusjs.config.shortname+"&thread=ident:"+disqusjs.config.identifier+"&api_key="+disqusjs.config.apikey;xhr.open("GET",e,!0),xhr.timeout=4e3,xhr.send(),xhr.onload=function(){if(200==this.status||304==this.status){var e=JSON.parse(this.responseText).response[0];disqusjs.page={id:e.id,title:e.title,isClosed:e.isClosed,length:e.posts},getComment()}},xhr.ontimeout=function(e){loadError()},xhr.onerror=function(e){loadError()}},getComment=function(){var e=disqusjs.config.api+"3.0/posts/list.json?forum="+disqusjs.config.shortname+"&thread="+disqusjs.page.id+"&api_key="+disqusjs.config.apikey;xhr.open("GET",e,!0),xhr.timeout=4e3,xhr.send(),xhr.onload=function(){if(200==this.status||304==this.status){var e=JSON.parse(this.responseText);0===e.code&&0'),s.authorEl='').concat(s.author.name,"")):(s.avatarEl=''),s.authorEl="".concat(s.author.name)),s.author.name===disqusjs.config.admin&&(s.authorEl+=''.concat(disqusjs.config.adminLabel,"")),e.nesting=t+1,n+='
  • '),n+=baidu.template(a,s),n+="".concat(childrenComments(e),"
  • ")}),0!==(n+="").length?n:void 0}});var s=e.comment;s.author.profileUrl?(s.avatarEl=''),s.authorEl='').concat(s.author.name,"")):(s.avatarEl=''),s.authorEl="".concat(s.author.name)),s.author.name===disqusjs.config.admin&&(s.authorEl+=''.concat(disqusjs.config.adminLabel,"")),e.children&&(e.nesting=1);var t='
  • ');t+=baidu.template(a,s),t+="".concat(childrenComments(e),"
  • "),document.getElementById("dsqjs-list").insertAdjacentHTML("beforeend",t)})},main=function(){document.getElementById("disqus_thread").innerHTML='

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

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

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

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

      ',disqusjs.mode=getLS("disqusjs_mode"),"disqus"===disqusjs.mode?loadDisqus():"dsqjs"===disqusjs.mode?getThreadInfo():checkDisqus()},main(); \ No newline at end of file +function(){disqusjs.page=[],window.disqus_config=function(){this.page.url=disqusjs.config.url,this.page.identifier=disqusjs.config.identifier};var s=new XMLHttpRequest,n=function(e,s){try{localStorage.setItem(e,s)}catch(e){console.log(e),console.log("Failed to set localStorage item")}};Date.prototype.Format=function(e){var s={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),S:this.getMilliseconds()};for(var t in/(y+)/.test(e)&&(e=e.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))),s)new RegExp("("+t+")").test(e)&&(e=e.replace(RegExp.$1,1==RegExp.$1.length?s[t]:("00"+s[t]).substr((""+s[t]).length)));return e};var t=function(){var s=new Image,t=setTimeout(function(){s.onerror=s.onload=null,n("disqusjs_mode","dsqjs")},2e3);s.onerror=function(){clearTimeout(t),n("disqusjs_mode","dsqjs"),u()},s.onload=function(){clearTimeout(t);var e=setTimeout(function(){s.onerror=s.onload=null,n("disqusjs_mode","dsqjs")},2e3);s.onerror=function(){clearTimeout(e),n("disqusjs_mode","dsqjs"),u()},s.onload=function(){clearTimeout(e),n("disqusjs_mode","disqus"),u()},s.src="https://"+disqusjs.config.shortname+".disqus.com/favicon.ico?"+ +new Date},s.src="https://disqus.com/favicon.ico?"+ +new Date},a=function(){n("disqusjs_mode","dsqjs"),u()},o=function(){n("disqusjs_mode","disqus"),u()},i=function(){document.getElementById("dsqjs-load-error").classList.remove("dsqjs-hide"),document.getElementById("dsqjs-loading-dsqjs").classList.add("dsqjs-hide"),document.getElementById("dsqjs-reload").addEventListener("click",r)},r=function(){document.getElementById("dsqjs-loading-dsqjs").classList.remove("dsqjs-hide"),document.getElementById("dsqjs-force-disqus").addEventListener("click",o),document.getElementById("dsqjs-reload-disqus").addEventListener("click",t);var e=disqusjs.config.api+"3.0/threads/list.json?forum="+disqusjs.config.shortname+"&thread=ident:"+disqusjs.config.identifier+"&api_key="+disqusjs.config.apikey;s.open("GET",e,!0),s.timeout=4e3,s.send(),s.onload=function(){if(200==this.status||304==this.status){var e=JSON.parse(this.responseText).response[0];disqusjs.page={id:e.id,title:e.title,isClosed:e.isClosed,length:e.posts},d()}},s.ontimeout=function(e){i()},s.onerror=function(e){i()}},d=function(){var e=disqusjs.config.api+"3.0/posts/list.json?forum="+disqusjs.config.shortname+"&thread="+disqusjs.page.id+"&api_key="+disqusjs.config.apikey;s.open("GET",e,!0),s.timeout=4e3,s.send(),s.onload=function(){if(200==this.status||304==this.status){var e=JSON.parse(this.responseText);0===e.code&&0'),s.authorEl='').concat(s.author.name,"")):(s.avatarEl=''),s.authorEl="".concat(s.author.name)),s.author.name===disqusjs.config.admin&&(s.authorEl+=''.concat(disqusjs.config.adminLabel,"")),e.children&&(e.nesting=1);var t='
    • ');t+=baidu.template(o,s),t+="".concat(function t(e){var n=e.nesting,s=e.children||[];if("null"!=typeof s){if(n<4)var a='
        ';else a='
          ';return s.map(function(e){var s=e.comment;s.author.profileUrl?(s.avatarEl=''),s.authorEl='').concat(s.author.name,"")):(s.avatarEl=''),s.authorEl="".concat(s.author.name)),s.author.name===disqusjs.config.admin&&(s.authorEl+=''.concat(disqusjs.config.adminLabel,"")),e.nesting=n+1,a+='
        • '),a+=baidu.template(o,s),a+="".concat(t(e),"
        • ")}),0!==(a+="
        ").length?a:void 0}}(e),""),document.getElementById("dsqjs-list").insertAdjacentHTML("beforeend",t)})},u=function(){var e;document.getElementById("disqus_thread").innerHTML='

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

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

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

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

          ',disqusjs.mode=(e="disqusjs_mode",localStorage.getItem(e)),"disqus"===disqusjs.mode?function(){var e=document;e.getElementById("dsqjs-load-disqus").classList.remove("dsqjs-hide"),e.getElementById("dsqjs-force-dsqjs").addEventListener("click",a);var s=e.createElement("script");s.src="https://"+disqusjs.config.shortname+".disqus.com/embed.js",s.setAttribute("data-timestamp",+new Date),(e.head||e.body).appendChild(s)}():"dsqjs"===disqusjs.mode?r():t()};u()}(); \ No newline at end of file diff --git a/dist/disqusjs.css b/dist/disqusjs.css index f7827a3..bb332f7 100644 --- a/dist/disqusjs.css +++ b/dist/disqusjs.css @@ -3,4 +3,4 @@ * Author: SukkaW * Link: https://github.com/SukkaW/DisqusJS * License: GPL-3.0 - */#dsqjs .dsqjs-container *{margin:0;padding:0}#dsqjs .dsqjs-container{margin:20px 0 0}#dsqjs ul.dsqjs-list{list-style:none;list-style-type:none}#dsqjs .dsqjs-item-container{position:relative;margin-bottom:24px;-webkit-transition:all .2s ease-in-out;transition:all .2s ease-in-out}#dsqjs .dsqjs-item-container:after,#dsqjs .dsqjs-item-container:before{display:table;content:"";line-height:0;clear:both}#dsqjs .dsqjs-avater{margin-right:12px;float:left;position:relative;z-index:100;padding:0}#dsqjs a{text-decoration:none;color:#076dd0}#dsqjs .dsqjs-avater img{width:42px;height:42px;display:block;border-radius:4px}#dsqjs .dsqjs-body{overflow:hidden}#dsqjs .dsqjs-header{line-height:1;font-size:13px}#dsqjs .dsqjs-bullet{line-height:1.4;margin:0 4px}#dsqjs .dsqjs-bullet::after{color:#c2c6cc;content:"•"}#dsqjs .dsqjs-author{line-height:1;color:#2479cc;font-weight:700}#dsqjs .dsqjs-author .dsqjs-admin-badge{color:#fff;background:#687a86;padding:1px 3px;margin-left:4px;font-size:12px;line-height:1.1;font-weight:700;border-radius:3px;display:inline-block;position:relative;top:-1px;left:1px}#dsqjs .dsqjs-meta{font-size:12px;color:#656c7a}#dsqjs .dsqjs-content p:last-child{margin:0}#dsqjs .dsqjs-content p{font-size:15px;line-height:21px;margin:0 0 12px;word-wrap:break-word}#dsqjs .dsqjs-children>.dsqjs-item{margin-left:60px}#dsqjs .dsqjs-children .dsqjs-avater img{width:38px;height:38px}#dsqjs .dsqjs-message{text-align:center;margin-top:4px;margin-bottom:4px;font-size:14px}#dsqjs .dsqjs-no-comment{text-align:center;margin-top:8px;margin-bottom:4px;font-size:18px;font-weight:700}#dsqjs .dsqjs-message a{margin-left:2px;margin-right:2px}.dsqjs-hide{display:none!important} \ No newline at end of file + */#dsqjs>*{margin:0;padding:0}#dsqjs .dsqjs-container *{margin:0;padding:0}#dsqjs .dsqjs-container{margin:20px 0 0}#dsqjs ul.dsqjs-list{list-style:none;list-style-type:none}#dsqjs .dsqjs-item-container{position:relative;margin-bottom:24px;-webkit-transition:all .2s ease-in-out;transition:all .2s ease-in-out}#dsqjs .dsqjs-item-container:after,#dsqjs .dsqjs-item-container:before{display:table;content:"";line-height:0;clear:both}#dsqjs .dsqjs-avater{margin-right:12px;float:left;position:relative;z-index:100;padding:0}#dsqjs a{text-decoration:none;color:#076dd0}#dsqjs .dsqjs-avater img{width:42px;height:42px;display:block;border-radius:4px}#dsqjs .dsqjs-body{overflow:hidden}#dsqjs .dsqjs-header{line-height:1;font-size:13px}#dsqjs .dsqjs-bullet{line-height:1.4;margin:0 4px}#dsqjs .dsqjs-bullet::after{color:#c2c6cc;content:"•"}#dsqjs .dsqjs-author{line-height:1;color:#2479cc;font-weight:700}#dsqjs .dsqjs-author .dsqjs-admin-badge{color:#fff;background:#687a86;padding:1px 3px;margin-left:4px;font-size:12px;line-height:1.1;font-weight:700;border-radius:3px;display:inline-block;position:relative;top:-1px;left:1px}#dsqjs .dsqjs-meta{font-size:12px;color:#656c7a}#dsqjs .dsqjs-content p:last-child{margin:0}#dsqjs .dsqjs-content p{font-size:15px;line-height:21px;margin:0 0 12px;word-wrap:break-word}#dsqjs .dsqjs-children>.dsqjs-item{margin-left:60px}#dsqjs .dsqjs-children .dsqjs-avater img{width:38px;height:38px}#dsqjs .dsqjs-message{text-align:center;margin-top:4px;margin-bottom:4px;font-size:14px}#dsqjs .dsqjs-no-comment{text-align:center;margin-top:8px;margin-bottom:4px;font-size:18px;font-weight:700}#dsqjs .dsqjs-message a{margin-left:2px;margin-right:2px}.dsqjs-hide{display:none!important} \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index 8893870..c2f2f91 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -31,7 +31,7 @@ gulp.task('minify-js', () => { output: { comments: /^!/ }, - keep_fnames: true + keep_fnames: false })) .pipe(gulp.dest('dist')); }); diff --git a/package.json b/package.json index 2766365..9a7face 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "disqusjs", - "version": "0.1.0-alpha-5", + "version": "0.1.0-alpha-8", "description": "Show comments list from Disqus API", "main": "dist/disqus.js", "scripts": { diff --git a/src/disqus.js b/src/disqus.js index 442b67f..b60ba12 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -248,388 +248,391 @@ * disqusjs.page.lenfth - How many comment in this thread */ -disqusjs.page = []; +(function () { + disqusjs.page = []; -window.disqus_config = function () { - this.page.url = disqusjs.config.url; - this.page.identifier = disqusjs.config.identifier; -}; - -var xhr = new XMLHttpRequest(); - -setLS = (key, value) => { - try { - localStorage.setItem(key, value) - } catch (o) { - console.log(o), console.log("Failed to set localStorage item") - } -} - -getLS = (key) => { - return localStorage.getItem(key); -} - -/* - * Name: Date.Format() - * - * Usage: - * Month - M | MM - * Date - d | dd - * Hour - h | hh - * Minute - m | mm - * Second - s | ss - * Season - q | qq - * Year - y | yy | yyyy - * ms - S - * - * Example: (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") - */ - -Date.prototype.Format = function (fmt) { - var o = { - "M+": this.getMonth() + 1, // Minth - "d+": this.getDate(), // Date - "h+": this.getHours(), // Hour - "m+": this.getMinutes(), // Minute - "s+": this.getSeconds(), // Second - "q+": Math.floor((this.getMonth() + 3) / 3), // Season - "S": this.getMilliseconds() // ms + window.disqus_config = function () { + this.page.url = disqusjs.config.url; + this.page.identifier = disqusjs.config.identifier; }; - if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); - for (var k in o) - if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); - return fmt; -} -/* - * Name: loadDisqus() - * Descriptin: load disqus as it should be. - */ + var xhr = new XMLHttpRequest(); -loadDisqus = () => { - var d = document; - d.getElementById('dsqjs-load-disqus').classList.remove('dsqjs-hide'); - d.getElementById('dsqjs-force-dsqjs').addEventListener('click', forceDsqjs); - var s = d.createElement('script'); - s.src = 'https://' + disqusjs.config.shortname + '.disqus.com/embed.js'; - s.setAttribute('data-timestamp', + new Date()); - (d.head || d.body).appendChild(s); -} + let setLS = (key, value) => { + try { + localStorage.setItem(key, value) + } catch (o) { + console.log(o), console.log("Failed to set localStorage item") + } + }, -/* - * Name: checkDisqus() - * Description: Check disqus is avaliable for visitor or not - * How it works: check favicons under 2 domains can be loaded or not. - */ + getLS = (key) => { + return localStorage.getItem(key); + } -checkDisqus = () => { - var img = new Image; - let check1 = setTimeout(() => { - img.onerror = img.onload = null; - setLS('disqusjs_mode', 'dsqjs'); - }, 2000); + /* + * Name: Date.Format() + * + * Usage: + * Month - M | MM + * Date - d | dd + * Hour - h | hh + * Minute - m | mm + * Second - s | ss + * Season - q | qq + * Year - y | yy | yyyy + * ms - S + * + * Example: (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") + */ + + Date.prototype.Format = function (fmt) { + var o = { + "M+": this.getMonth() + 1, // Minth + "d+": this.getDate(), // Date + "h+": this.getHours(), // Hour + "m+": this.getMinutes(), // Minute + "s+": this.getSeconds(), // Second + "q+": Math.floor((this.getMonth() + 3) / 3), // Season + "S": this.getMilliseconds() // ms + }; + if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); + for (var k in o) + if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); + return fmt; + } - img.onerror = () => { - clearTimeout(check1); - setLS('disqusjs_mode', 'dsqjs'); - main(); - }; - img.onload = () => { - clearTimeout(check1); - let check2 = setTimeout(() => { + /* + * Name: loadDisqus() + * Descriptin: load disqus as it should be. + */ + + let loadDisqus = () => { + var d = document; + d.getElementById('dsqjs-load-disqus').classList.remove('dsqjs-hide'); + d.getElementById('dsqjs-force-dsqjs').addEventListener('click', forceDsqjs); + var s = d.createElement('script'); + s.src = 'https://' + disqusjs.config.shortname + '.disqus.com/embed.js'; + s.setAttribute('data-timestamp', + new Date()); + (d.head || d.body).appendChild(s); + } + + /* + * Name: checkDisqus() + * Description: Check disqus is avaliable for visitor or not + * How it works: check favicons under 2 domains can be loaded or not. + */ + + let checkDisqus = () => { + var img = new Image; + let check1 = setTimeout(() => { img.onerror = img.onload = null; setLS('disqusjs_mode', 'dsqjs'); }, 2000); img.onerror = () => { - clearTimeout(check2); + clearTimeout(check1); setLS('disqusjs_mode', 'dsqjs'); main(); }; - img.onload = () => { - clearTimeout(check2); - setLS('disqusjs_mode', 'disqus'); - main(); - }; - - img.src = 'https://' + disqusjs.config.shortname + '.disqus.com/favicon.ico?' + +(new Date); - }; - - img.src = 'https://disqus.com/favicon.ico?' + +(new Date); -} + clearTimeout(check1); + let check2 = setTimeout(() => { + img.onerror = img.onload = null; + setLS('disqusjs_mode', 'dsqjs'); + }, 2000); + + img.onerror = () => { + clearTimeout(check2); + setLS('disqusjs_mode', 'dsqjs'); + main(); + }; -/* - * Name: forceDsqjs() forceDisqus() - */ + img.onload = () => { + clearTimeout(check2); + setLS('disqusjs_mode', 'disqus'); + main(); + }; -forceDsqjs = () => { - setLS('disqusjs_mode', 'dsqjs'); - main(); -} + img.src = 'https://' + disqusjs.config.shortname + '.disqus.com/favicon.ico?' + +(new Date); + }; -forceDisqus = () => { - setLS('disqusjs_mode', 'disqus'); - main(); -} + img.src = 'https://disqus.com/favicon.ico?' + +(new Date); + } -/* - * Name: loadError() - * Description: When dsqjs mode load error - */ + /* + * Name: forceDsqjs() forceDisqus() + */ -loadError = () => { - document.getElementById('dsqjs-load-error').classList.remove('dsqjs-hide'); - document.getElementById('dsqjs-loading-dsqjs').classList.add('dsqjs-hide'); - document.getElementById('dsqjs-reload').addEventListener('click', getThreadInfo); -} + let forceDsqjs = () => { + setLS('disqusjs_mode', 'dsqjs'); + main(); + } -/* - * Name: getThreadInfo() - * Description: Disqus API only support get thread list by ID, not identifter. So get Thread ID before get thread list. - * 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 forceDisqus = () => { + setLS('disqusjs_mode', 'disqus'); + main(); + } -getThreadInfo = () => { - document.getElementById('dsqjs-loading-dsqjs').classList.remove('dsqjs-hide'); - document.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus); - document.getElementById('dsqjs-reload-disqus').addEventListener('click', checkDisqus); - let url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey; - xhr.open('GET', url, true); - xhr.timeout = 4000; - 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(); - } - }; - xhr.ontimeout = (e) => { - loadError(); - }; - xhr.onerror = (e) => { - loadError(); - }; -} + /* + * Name: loadError() + * Description: When dsqjs mode load error + */ + + let loadError = () => { + document.getElementById('dsqjs-load-error').classList.remove('dsqjs-hide'); + document.getElementById('dsqjs-loading-dsqjs').classList.add('dsqjs-hide'); + document.getElementById('dsqjs-reload').addEventListener('click', getThreadInfo); + } -/* - * Name: getComment() - * Description: get the comment content - * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey] - */ + /* + * Name: getThreadInfo() + * Description: Disqus API only support get thread list by ID, not identifter. So get Thread ID before get thread list. + * 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] + */ -getComment = () => { - let url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey; - xhr.open('GET', url, true); - xhr.timeout = 4000; - xhr.send(); - xhr.onload = function () { - if (this.status == 200 || this.status == 304) { - var res = JSON.parse(this.responseText); - if (res.code === 0 && res.response.length > 0) { - getCommentList(res.response); - } else if (res.code === 0 && res.response.length === 0) { - // Have no comments. - document.getElementById('dsqjs-no-comment').classList.remove('dsqjs-hide'); + let getThreadInfo = () => { + document.getElementById('dsqjs-loading-dsqjs').classList.remove('dsqjs-hide'); + document.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus); + document.getElementById('dsqjs-reload-disqus').addEventListener('click', checkDisqus); + let url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey; + xhr.open('GET', url, true); + xhr.timeout = 4000; + 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(); } + }; + xhr.ontimeout = (e) => { + loadError(); + }; + xhr.onerror = (e) => { + loadError(); + }; + } - } - }; - xhr.ontimeout = (e) => { - // Have error when get comments. - loadError(); - }; - xhr.onerror = (e) => { - // Have error when get comments. - loadError(); - }; -} - -/* - * Name: getCommentList(data) - * Description: Render JSON to comment list components - */ + /* + * Name: getComment() + * Description: get the comment content + * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey] + */ + + let getComment = () => { + let url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey; + xhr.open('GET', url, true); + xhr.timeout = 4000; + xhr.send(); + xhr.onload = function () { + if (this.status == 200 || this.status == 304) { + var res = JSON.parse(this.responseText); + if (res.code === 0 && res.response.length > 0) { + getCommentList(res.response); + } else if (res.code === 0 && res.response.length === 0) { + // Have no comments. + document.getElementById('dsqjs-no-comment').classList.remove('dsqjs-hide'); + } -getCommentList = (data) => { - var topLevelComments = []; - var childComments = []; + } + }; + xhr.ontimeout = (e) => { + // Have error when get comments. + loadError(); + }; + xhr.onerror = (e) => { + // Have error when get comments. + loadError(); + }; + } - data.forEach(comment => { - (comment.parent ? childComments : topLevelComments)['push'](comment) - }) + /* + * Name: getCommentList(data) + * Description: Render JSON to comment list components + */ + + let getCommentList = (data) => { + var topLevelComments = []; + var childComments = []; + + data.forEach(comment => { + (comment.parent ? childComments : topLevelComments)['push'](comment) + }) + + var commentLists = topLevelComments.map(comment => { + return { + comment, + author: comment.author.name, + isPrimary: comment.author.username === disqusjs.config.admin, + children: getChildren(+comment.id) + }; + }); + + function 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, + isPrimary: comment.author.username === disqusjs.config.admin, + children: getChildren(+comment.id) + }); + } + } - var commentLists = topLevelComments.map(comment => { - return { - comment, - author: comment.author.name, - isPrimary: comment.author.username === disqusjs.config.admin, - children: getChildren(+comment.id) - }; - }); - - function 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, - isPrimary: comment.author.username === disqusjs.config.admin, - children: getChildren(+comment.id) - }); + if (list.length) { + return list; + } else { + return null; } } - if (list.length) { - return list; - } else { - return null; - } + renderComment(commentLists) } - renderComment(commentLists) -} - -renderComment = (data) => { - /* -
          -
          - <%- avatarEl %> -
          -
          -
          - <%- authorEl %> - - -
          -
          <%- message %>
          + let renderComment = (data) => { + /* +
          +
          + <%- avatarEl %> +
          +
          +
          + <%- authorEl %> + + +
          +
          <%- message %>
          +
          -
          - */ - var commentBodyTpl = `
          <%- avatarEl %>
          <%- authorEl %>
          <%- message %>
          ` + */ + var commentBodyTpl = `
          <%- avatarEl %>
          <%- authorEl %>
          <%- message %>
          ` - data.map(s => { - childrenComments = (s) => { - var nesting = s.nesting + data.map(s => { + let childrenComments = (s) => { + var nesting = s.nesting - var children = (s.children || []); + var children = (s.children || []); - if (typeof children === 'null') { - return; - } + if (typeof children === 'null') { + return; + } - if (nesting < 4) { - var html = '
            '; - } else { - var html = '
              '; - } + if (nesting < 4) { + var html = '
                '; + } else { + var html = '
                  '; + } - children.map(s => { + children.map(s => { - let comment = s.comment + let comment = s.comment - if (comment.author.profileUrl) { - /* - - - - */ - comment.avatarEl = ``; - comment.authorEl = `${comment.author.name}` - } else { - comment.avatarEl = ``; - comment.authorEl = `${comment.author.name}` - } + if (comment.author.profileUrl) { + /* + + + + */ + comment.avatarEl = ``; + comment.authorEl = `${comment.author.name}` + } else { + comment.avatarEl = ``; + comment.authorEl = `${comment.author.name}` + } - if (comment.author.name === disqusjs.config.admin) { - comment.authorEl += `${disqusjs.config.adminLabel}` - } + if (comment.author.name === disqusjs.config.admin) { + comment.authorEl += `${disqusjs.config.adminLabel}` + } - s.nesting = nesting + 1 + s.nesting = nesting + 1 - html += `
                • ` + html += `
                • ` - html += baidu.template(commentBodyTpl, comment) + html += baidu.template(commentBodyTpl, comment) - html += `${childrenComments(s)}
                • ` - }) + html += `${childrenComments(s)}` + }) - html += '
                '; + html += '
              '; - if (html.length !== 0) { - return html; - } else { - return; - } - }; + if (html.length !== 0) { + return html; + } else { + return; + } + }; - let comment = s.comment; + let comment = s.comment; - if (comment.author.profileUrl) { - comment.avatarEl = ``; - comment.authorEl = `${comment.author.name}` - } else { - comment.avatarEl = ``; - comment.authorEl = `${comment.author.name}` - } + if (comment.author.profileUrl) { + comment.avatarEl = ``; + comment.authorEl = `${comment.author.name}` + } else { + comment.avatarEl = ``; + comment.authorEl = `${comment.author.name}` + } - if (comment.author.name === disqusjs.config.admin) { - comment.authorEl += `${disqusjs.config.adminLabel}` - } + if (comment.author.name === disqusjs.config.admin) { + comment.authorEl += `${disqusjs.config.adminLabel}` + } - if (s.children) { - s.nesting = 1 - } + if (s.children) { + s.nesting = 1 + } - let html = `
            • ` + let html = `
            • ` - html += baidu.template(commentBodyTpl, comment) + html += baidu.template(commentBodyTpl, comment) - html += `${childrenComments(s)}
            • `; + html += `${childrenComments(s)}`; - document.getElementById('dsqjs-list').insertAdjacentHTML('beforeend', html); - }) -} + document.getElementById('dsqjs-list').insertAdjacentHTML('beforeend', html); + }) + } -main = () => { - // Add dsqjs container element to #disqus_thread + let main = () => { + // Add dsqjs container element to #disqus_thread + + /* +
              +
              +
              +
              +

              评论完整模式加载中...如果长时间无法加载,请针对 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 模式

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

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

                  `; + document.getElementById('disqus_thread').innerHTML = disqusjsBaseTpl; + + disqusjs.mode = getLS('disqusjs_mode'); + if (disqusjs.mode === 'disqus') { + loadDisqus(); + } else if (disqusjs.mode === 'dsqjs') { + getThreadInfo(); + } else { + // Run checkDisqus() when no localStorage item + // disqusjs.mode will be set in checkDisqus() + checkDisqus(); + } - /* -
                  -
                  -
                  -
                  -

                  评论完整模式加载中...如果长时间无法加载,请针对 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 模式

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

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

                      `; - document.getElementById('disqus_thread').innerHTML = disqusjsBaseTpl; - - disqusjs.mode = getLS('disqusjs_mode'); - if (disqusjs.mode === 'disqus') { - loadDisqus(); - } else if (disqusjs.mode === 'dsqjs') { - getThreadInfo(); - } else { - // Run checkDisqus() when no localStorage item - // disqusjs.mode will be set in checkDisqus() - checkDisqus(); } -} + main(); -main(); \ No newline at end of file +})(); \ No newline at end of file