diff --git a/.gitignore b/.gitignore index 3892d32..d508b27 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ public src/index.html dist/index.html + +package-lock.json diff --git a/dist/disqus.js b/dist/disqus.js index db5d498..db6264e 100644 --- a/dist/disqus.js +++ b/dist/disqus.js @@ -1 +1 @@ -/*! DisqusJS v1.3.0 | Sukka (https://skk.moe) | https://disqusjs.skk.moe | MIT License */"use strict";function _createForOfIteratorHelperLoose(e,s){var t;if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator])return(t=e[Symbol.iterator]()).next.bind(t);if(Array.isArray(e)||(t=_unsupportedIterableToArray(e))||s&&e&&"number"==typeof e.length){t&&(e=t);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,s){if(e){if("string"==typeof e)return _arrayLikeToArray(e,s);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(e,s):void 0}}function _arrayLikeToArray(e,s){(null==s||s>e.length)&&(s=e.length);for(var t=0,r=new Array(s);t & DisqusJS

',u=function(e,s){return'
'},m=function(e,s,t){var r=e.avatarEl,n=e.createdAt;return'
'+r+'
'+s+'
'+t+"
"},g='如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 尝试完整 Disqus 模式 | 强制完整 Disqus 模式',j=function(e){return r(e,{method:"GET"}).then(function(e){return n.all([e.ok,e.status,e.json(),e.headers])}).then(function(e){var s=e[0],t=e[1],r=e[2],n=e[3];if(s)return{ok:s,status:t,data:r,headers:n};throw new Error}).catch(function(e){throw e})},b=function(e,s){try{t.setItem(e,s)}catch(e){}},i=function(e){function s(e){return e<10?"0"+e:e}return e=Date.parse(new Date(e)),(e=new Date(e+288e5)).getFullYear()+"-"+s(e.getMonth()+1)+"-"+s(e.getDate())+" "+s(e.getHours())+":"+s(e.getMinutes())};function e(){var e;s.DISQUS?s.DISQUS.reset({reload:!0,config:function(){this.page.identifier=D.config.identifier,this.page.url=D.config.url,this.page.title=D.config.title}}):(e=p.createElement("script"),q(d).innerHTML='
评论完整模式加载中... 如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或切换至 评论基础模式
'+l+"
",q("dsqjs-force-dsqjs").addEventListener(v,T),e.src="https://"+D.config.shortname+".disqus.com/embed.js",e.setAttribute("data-timestamp",+new Date),(p.head||p.body).appendChild(e))}function y(){q(d).innerHTML='
正在检查 Disqus 能否访问...
'+l+"
";for(var e=["disqus.com",D.config.shortname+".disqus.com"],r=0,n=0,a=function(){e.length===r&&r===n?w():e.length===r&&T()},s=0,t=e;s
评论基础模式加载中... '+g+"
"+u(o,D.config.siteName)+'

    评论列表加载中...

加载更多评论
'+l+"",L(),q("dsqjs-order-"+D.sortType).setAttribute("checked","true"),i()}else{if(0!==s.code||1===s.response.length)throw new Error;h('当前 Thread 尚未创建。是否切换至 完整 Disqus 模式?'),q("dsqjs-force-disqus").addEventListener(v,w)}}).catch(E);function s(e){function a(e){return{comment:e,author:e.author.name,isPrimary:!!D.config.admin&&e.author.username===D.config.admin,children:t(+e.id),hasMore:e.hasMore}}var s=[],o=[],t=function(e){if(0===o.length)return null;for(var s,t=[],r=_createForOfIteratorHelperLoose(o);!(s=r()).done;){var n=s.value;n.parent===e&&t.unshift(a(n))}return t.length?t:null};return e.forEach(function(e){(e.parent?o:s).push(e)}),s.map(a)}var i=function t(e){void 0===e&&(e="");function r(){for(var e,s=_createForOfIteratorHelperLoose(d);!(e=s()).done;){e.value.removeEventListener("change",u)}i.removeEventListener(v,l);for(var t,r=_createForOfIteratorHelperLoose(c);!(t=r()).done;){t.value.removeEventListener(v,y)}}var i=q("dsqjs-load-more"),d=p.getElementsByClassName("dsqjs-order-radio"),c=p.getElementsByClassName("dsqjs-has-more-btn"),l=function(){r(),t(D.page.next)},u=function(e){var s=e.target;D.sortType=s.getAttribute("value"),b(o,D.sortType),r(),D.page.comment=[],D.page.next="",q("dsqjs-post-container").innerHTML='

正在切换排序方式...

',i.classList.add("dsqjs-hide"),t()},s=""===e?"":"&cursor="+e;i.classList.add("dsqjs-disabled");function n(e){var s=e.createdAt;return Date.parse(new Date(s))}function m(e,s){return e.parent&&s.parent?n(e)-n(s):0}var a=D.config.api+"3.0/threads/listPostsThreaded?forum="+D.config.shortname+"&thread="+D.page.id+s+"&api_key="+H()+"&order="+D.sortType;j(a).then(function(e){var s,t=e.data;if(0===t.code&&0",L(),q("dsqjs-force-disqus").addEventListener(v,T)}}).catch(function(){""===e?E():(i.classList.remove("dsqjs-disabled"),i.innerHTML="加载更多评论失败,点击重试",i.addEventListener(v,l))})},f=function(e){function a(e){return e.comment.author.profileUrl?(e.comment.avatarEl='",e.comment.authorEl='"):(e.comment.avatarEl='',e.comment.authorEl='"),D.config.adminLabel&&e.isPrimary&&(e.comment.authorEl+=''+D.config.adminLabel+""),e}function o(e){var s="",t="",t=e.isDeleted?"此评论已被删除":(s=e.authorEl+'',function(e){var s=p.createElement("div");s.innerHTML=e;for(var t,r=_createForOfIteratorHelperLoose(s.getElementsByTagName("a"));!(t=r()).done;){var n=t.value,a=decodeURIComponent(n.href.replace(/https:\/\/disq.us\/url\?url=/g,"")).replace(/(.*):.+cuid=.*/,"$1");n.href=a,n.innerHTML=a,n.rel="external noopener nofollow noreferrer",n.target="_blank"}return s.innerHTML}(r(e.message)));return m(e,s,t)}var r=function(e){return e.replace("/a.disquscdn.com/ig","c.disquscdn.com")},t="";s(e).map(function(e){e.children&&(e.nesting=1);var s="";(e=a(e)).hasMore&&(s='

切换至 完整 Disqus 模式 显示更多回复

'),t+='
  • '+o(e.comment)+function t(e){var r=e.nesting,s=e.children||[];if(s){var n="",n=r':'
      ';return s.map(function(e){(e=a(e)).nesting=r+1;var s="";e.hasMore&&(s='

      切换至 完整 Disqus 模式 显示更多回复

      '),n+='
    • '+o(e.comment)+t(e)+s+"
    • "}),0!==(n+="
    ").length?n:void 0}}(e)+s+"
  • "}),h("你可能无法访问 Disqus,已启用评论基础模式。"+g),q("dsqjs-post-container").innerHTML=t,L()}}function E(e){console.log(e),h('评论基础模式加载失败,是否 重载尝试完整 Disqus 模式 ?'),q("dsqjs-reload-dsqjs").addEventListener(v,f),q("dsqjs-reload-disqus").addEventListener(v,y)}function T(){b("dsqjs_mode","dsqjs"),f()}function w(){b("dsqjs_mode","disqus"),e()}var D={},_=p.location.origin+p.location.pathname+p.location.search;D.config=a({api:"https://disqus.skk.moe/disqus/",identifier:_,url:_,title:p.title,siteName:"",nesting:parseInt(M.nesting)||4,nocomment:"这里冷冷清清的,一条评论都没有"},M),D.page={};var I=D.config.apikey,H=function(){return Array.isArray(I)?I[Math.floor(Math.random()*I.length)]:I};s.disqus_config=function(){this.page.url=D.config.url,this.page.identifier=D.config.identifier,this.page.title=D.config.title},q(d).innerHTML='
    '+l+"
    ",r&&t&&n?(D.mode=t.getItem("dsqjs_mode"),D.sortType=t.getItem(o)||t.getItem("disqus.sort"),D.sortType||(b(o,"desc"),D.sortType="desc"),("disqus"===D.mode?e:"dsqjs"===D.mode?f:y)()):(h("你的浏览器版本过低,不兼容评论基础模式。"+g),L())}(window,document,localStorage,fetch,Promise)}try{module.exports=DisqusJS}catch(e){} \ No newline at end of file +/*! DisqusJS v1.3.0 | Sukka (https://skk.moe) | https://disqusjs.skk.moe | MIT License */"use strict";function DisqusJS(C){!function(e,f,n,t,o){function r(){for(var s=arguments.length,e=new Array(s),n=0;n & DisqusJS

    ',m=function(s,e){return'
    '},g=function(s,e,n){var t=s.avatarEl,r=s.createdAt;return'
    '+t+'
    '+e+'
    '+n+"
    "},v='如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 尝试完整 Disqus 模式 | 强制完整 Disqus 模式',b=function(s){return t(s,{method:"GET"}).then(function(s){return o.all([s.ok,s.status,s.json(),s.headers])}).then(function(s){var e=s[0],n=s[1],t=s[2],r=s[3];if(e)return{ok:e,status:n,data:t,headers:r};throw new Error}).catch(function(s){throw s})},E=function(s,e){try{n.setItem(s,e)}catch(s){}},a=function(s){function e(s){return s<10?"0"+s:s}return s=Date.parse(new Date(s)),(s=new Date(s+288e5)).getFullYear()+"-"+e(s.getMonth()+1)+"-"+e(s.getDate())+" "+e(s.getHours())+":"+e(s.getMinutes())};function s(){var s;e.DISQUS?e.DISQUS.reset({reload:!0,config:function(){this.page.identifier=M.config.identifier,this.page.url=M.config.url,this.page.title=M.config.title}}):(s=f.createElement("script"),q(d).innerHTML='
    评论完整模式加载中... 如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或切换至 评论基础模式
    '+l+"
    ",q("dsqjs-force-dsqjs").addEventListener(h,D),s.src="https://"+M.config.shortname+".disqus.com/embed.js",s.setAttribute("data-timestamp",+new Date),(f.head||f.body).appendChild(s))}function y(){q(d).innerHTML='
    正在检查 Disqus 能否访问...
    '+l+"
    ";function s(r){return new o(function(s,e){var n=new Image,t=setTimeout(function(){n.onerror=n.onload=null,e()},3e3);n.onerror=function(){clearTimeout(t),e()},n.onload=function(){clearTimeout(t),s()},n.src="https://"+r+"/favicon.ico?"+ +new Date+"="+ +new Date})}return o.all([s("disqus.com"),s(M.config.shortname+".disqus.com")]).then(w,D)}function L(){q("dsqjs-reload-disqus").addEventListener(h,y),q("dsqjs-force-disqus").addEventListener(h,w)}function i(){p("评论基础模式加载中... "+v),L();var s=M.config.api+"3.0/threads/list.json?forum="+encodeURIComponent(M.config.shortname)+"&thread="+encodeURIComponent("ident:"+M.config.identifier)+"&api_key="+encodeURIComponent(k());b(s).then(function(s){var e=s.data;if(0===e.code&&1===e.response.length){var n=e.response[0],t=n.id,r=n.title,o=n.isClosed,a=n.posts;M.page={id:t,title:r,isClosed:o,length:a,comment:[]},q(d).innerHTML='
    评论基础模式加载中... '+v+"
    "+m(a,M.config.siteName)+'

      评论列表加载中...

    加载更多评论
    '+l+"
    ",L(),q("dsqjs-order-"+M.sortType).setAttribute("checked","true"),i()}else{if(0!==e.code||1===e.response.length)throw new Error;p('当前 Thread 尚未创建。是否切换至 完整 Disqus 模式?'),q("dsqjs-force-disqus").addEventListener(h,w)}}).catch(T);function e(s){function t(s){return{comment:s,author:s.author.name,isPrimary:!!M.config.admin&&s.author.username===M.config.admin,children:n(+s.id),hasMore:s.hasMore}}var e=[],r=[],n=function(e){if(0===r.length)return null;var n=[];return r.forEach(function(s){s.parent===e&&n.unshift(t(s))}),n.length?n:null};return s.forEach(function(s){(s.parent?r:e).push(s)}),e.map(t)}var i=function n(s){void 0===s&&(s="");function t(){Array.from(o).forEach(function(s){return s.removeEventListener("change",d)}),r.removeEventListener(h,i),Array.from(a).forEach(function(s){return s.removeEventListener(h,y)})}var r=q("dsqjs-load-more"),o=f.getElementsByClassName("dsqjs-order-radio"),a=f.getElementsByClassName("dsqjs-has-more-btn"),i=function(){t(),n(M.page.next)},d=function(s){var e=s.target;M.sortType=e.getAttribute("value"),E(j,M.sortType),t(),M.page.comment=[],M.page.next="",q("dsqjs-post-container").innerHTML='

    正在切换排序方式...

    ',r.classList.add("dsqjs-hide"),n()},e=""===s?"":"&cursor="+s;r.classList.add("dsqjs-disabled");function c(s){var e=s.createdAt;return Date.parse(new Date(e))}function l(s,e){return s.parent&&e.parent?c(s)-c(e):0}var m=M.config.api+"3.0/threads/listPostsThreaded?forum="+encodeURIComponent(M.config.shortname)+"&thread="+encodeURIComponent(M.page.id)+encodeURIComponent(e)+"&api_key="+encodeURIComponent(k())+"&order="+encodeURIComponent(M.sortType);b(m).then(function(s){var e,n=s.data;if(0===n.code&&0",L(),q("dsqjs-force-disqus").addEventListener(h,D)}}).catch(function(){""===s?T():(r.classList.remove("dsqjs-disabled"),r.innerHTML="加载更多评论失败,点击重试",r.addEventListener(h,i))})},u=function(s){function o(s){return s.comment.author.profileUrl?(s.comment.avatarEl='",s.comment.authorEl='"):(s.comment.avatarEl='',s.comment.authorEl='"),M.config.adminLabel&&s.isPrimary&&(s.comment.authorEl+=''+M.config.adminLabel+""),s}function a(s){var e="",n="",n=s.isDeleted?"此评论已被删除":(e=s.authorEl+'',function(s){var e=f.createElement("div");e.innerHTML=s;var n=e.getElementsByTagName("a");return Array.from(n).forEach(function(s){var e=decodeURIComponent(s.href.replace(/https:\/\/disq\.us\/url\?url=/g,"")).replace(/(.*):.+cuid=.*/,"$1");s.href=e,s.innerHTML=e,s.rel="external noopener nofollow noreferrer",s.target="_blank"}),e.innerHTML}(t(s.message)));return g(s,e,n)}var t=function(s){return s.replace(/a\.disquscdn\.com/g,"c.disquscdn.com")},n="";e(s).map(function(s){s.children&&(s.nesting=1);var e="";(s=o(s)).hasMore&&(e='

    切换至 完整 Disqus 模式 显示更多回复

    '),n+='
  • '+a(s.comment)+function n(s){var t=s.nesting,e=s.children||[];if(e){var r="",r=t':'
      ';return e.map(function(s){(s=o(s)).nesting=t+1;var e=s.hasMore?'

      切换至 完整 Disqus 模式 显示更多回复

      ':"";r+='
    • '+a(s.comment)+n(s)+e+"
    • "}),0!==(r+="
    ").length?r:void 0}}(s)+e+"
  • "}),p("你可能无法访问 Disqus,已启用评论基础模式。"+v),q("dsqjs-post-container").innerHTML=n,L()}}function T(s){console.log(s),p('评论基础模式加载失败,是否 重载尝试完整 Disqus 模式 ?'),q("dsqjs-reload-dsqjs").addEventListener(h,i),q("dsqjs-reload-disqus").addEventListener(h,y)}function D(){E("dsqjs_mode","dsqjs"),i()}function w(){E("dsqjs_mode","disqus"),s()}var M={},u=f.location.origin+f.location.pathname+f.location.search;M.config=r({api:"https://disqus.skk.moe/disqus/",identifier:u,url:u,title:f.title,siteName:"",nesting:parseInt(C.nesting)||4,nocomment:"这里冷冷清清的,一条评论都没有"},C),M.page={};var I=M.config.apikey,k=function(){return Array.isArray(I)?I[Math.floor(Math.random()*I.length)]:I};e.disqus_config=function(){this.page.url=M.config.url,this.page.identifier=M.config.identifier,this.page.title=M.config.title},q(d).innerHTML='
    '+l+"
    ",t&&n&&o?(M.mode=n.getItem("dsqjs_mode"),M.sortType=n.getItem(j)||n.getItem("disqus.sort"),M.sortType||(E(j,"desc"),M.sortType="desc"),("disqus"===M.mode?s:"dsqjs"===M.mode?i:y)()):(p("你的浏览器版本过低,不兼容评论基础模式。"+v),L())}(window,document,localStorage,fetch,Promise)}try{module.exports=DisqusJS}catch(s){} \ No newline at end of file diff --git a/src/disqus.js b/src/disqus.js index f192f1e..ca5fa9b 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -246,7 +246,7 @@ function DisqusJS(config) { * 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] */ - const url = `${disqusjs.config.api}3.0/threads/list.json?forum=${disqusjs.config.shortname}&thread=ident:${disqusjs.config.identifier}&api_key=${apikey()}`; + const url = `${disqusjs.config.api}3.0/threads/list.json?forum=${encodeURIComponent(disqusjs.config.shortname)}&thread=${encodeURIComponent(`ident:${disqusjs.config.identifier}`)}&api_key=${encodeURIComponent(apikey())}`; _get(url).then(({ data }) => { if (data.code === 0 && data.response.length === 1) { @@ -285,9 +285,9 @@ function DisqusJS(config) { const unregisterListenerForSwitchTypeRadioAndGetMoreCommentBtn = () => { // 为按钮们取消事件,避免重复绑定 // 重新 getComment() 时会重新绑定 - [...$orderRadio].forEach(i => i.removeEventListener('change', switchSortType)); + Array.from($orderRadio).forEach(i => i.removeEventListener('change', switchSortType)); $loadMoreBtn.removeEventListener(CLICK, getMoreComment); - [...$loadHideCommentInDisqus].forEach(i => i.removeEventListener(CLICK, checkDisqus)); + Array.from($loadHideCommentInDisqus).forEach(i => i.removeEventListener(CLICK, checkDisqus)); } const getMoreComment = () => { @@ -364,7 +364,7 @@ function DisqusJS(config) { } }; - const url = `${disqusjs.config.api}3.0/threads/listPostsThreaded?forum=${disqusjs.config.shortname}&thread=${disqusjs.page.id}${cursorParam}&api_key=${apikey()}&order=${disqusjs.sortType}`; + const url = `${disqusjs.config.api}3.0/threads/listPostsThreaded?forum=${encodeURIComponent(disqusjs.config.shortname)}&thread=${encodeURIComponent(disqusjs.page.id)}${encodeURIComponent(cursorParam)}&api_key=${encodeURIComponent(apikey())}&order=${encodeURIComponent(disqusjs.sortType)}`; _get(url).then(({ data }) => { if (data.code === 0 && data.response.length > 0) { @@ -381,8 +381,8 @@ function DisqusJS(config) { renderComment(disqusjs.page.comment); // 为排序按钮们委托事件 - [...$orderRadio].forEach(i => i.addEventListener('change', switchSortType)); - [...$loadHideCommentInDisqus].forEach(i => i.addEventListener(CLICK, checkDisqus)); + Array.from($orderRadio).forEach(i => i.addEventListener('change', switchSortType)); + Array.from($loadHideCommentInDisqus).forEach(i => i.addEventListener(CLICK, checkDisqus)); if (data.cursor.hasNext) { // 将 cursor.next 存入 disqusjs 变量中供不能传参的不匿名函数使用 @@ -509,7 +509,8 @@ function DisqusJS(config) { const el = document.createElement('div'); el.innerHTML = input; const aTag = el.getElementsByTagName('a'); - [...aTag].forEach(i => { + // Use Array.from(aTag) instead of [...aTag] because when using gulp, [..aTag] may be replaced by [].concat(aTag), which is not the same meaning. + Array.from(aTag).forEach(i => { const link = decodeURIComponent(i.href.replace(/https:\/\/disq\.us\/url\?url=/g, '')).replace(/(.*):.+cuid=.*/, '$1'); i.href = link;