diff --git a/src/disqus.js b/src/disqus.js index 6757126..47d9d0a 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -307,6 +307,8 @@ Date.prototype.Format = function (fmt) { 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()); @@ -320,24 +322,10 @@ loadDisqus = () => { */ checkDisqus = () => { - let domain = ['disqus.com', disqusjs.config.shortname + '.disqus.com'], - test = 0, - success = 0; - - setmode = () => { - if (success = test) { - setLS('disqusjs_mode', 'disqus'); - } else { - setLS('disqusjs_mode', 'dsqjs'); - } - }; - var img = new Image; let check1 = setTimeout(() => { img.onerror = img.onload = null; - test = test + 1; - console.log(img.src + ' timeout') - setmode(); + setLS('disqusjs_mode', 'dsqjs'); }, 2000); img.onerror = () => { @@ -349,9 +337,7 @@ checkDisqus = () => { clearTimeout(check1); let check2 = setTimeout(() => { img.onerror = img.onload = null; - test = test + 1; - console.log(img.src + ' timeout') - setmode(); + setLS('disqusjs_mode', 'dsqjs'); }, 2000); img.onerror = () => { @@ -372,6 +358,31 @@ checkDisqus = () => { img.src = 'https://disqus.com/favicon.ico?' + +(new Date); } +/* + * Name: forceDsqjs() forceDisqus() + */ + +forceDsqjs = () => { + setLS('disqusjs_mode', 'dsqjs'); + main(); +} + +forceDisqus = () => { + setLS('disqusjs_mode', 'disqus'); + main(); +} + +/* + * Name: loadError() + * Description: When dsqjs mode load error + */ + +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: getThreadInfo() * Description: Disqus API only support get thread list by ID, not identifter. So get Thread ID before get thread list. @@ -380,6 +391,8 @@ checkDisqus = () => { */ getThreadInfo = () => { + document.getElementById('dsqjs-loading-dsqjs').classList.remove('dsqjs-hide'); + document.getElementById('dsqjs-force-disqus').addEventListener('click', forceDisqus); 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; @@ -397,10 +410,10 @@ getThreadInfo = () => { } }; xhr.ontimeout = (e) => { - console.log(e) + loadError(); }; xhr.onerror = (e) => { - console.log(e) + loadError(); }; } @@ -482,15 +495,6 @@ getCommentList = (data) => { } renderComment = (data) => { - var disqusjsBaseTpl = ` -
评论完整模式加载中...如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或使用评论基础模式
+你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并切换到完整 Disqus 模式。
+评论基础模式出现错误,是否重载?
+