diff --git a/src/disqus.js b/src/disqus.js index 31a8c6d..7f88cf0 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -27,27 +27,22 @@ function DisqusJS(config) { - // 封装一下基于 Object.asign 的方法 - function _extends(...args) { - _extends = Object.assign || function (target) { - for (const source of arguments) { - for (const key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; + ((window, document, localStorage, fetch) => { + // 封装一下基于 Object.asign 的方法 + function _extends(...args) { + _extends = Object.assign || function (target) { + for (const source of arguments) { + for (const key in source) { + if (Object.prototype.hasOwnProperty.call(source, key)) { + target[key] = source[key]; + } } } + return target; } - return target; - } - return _extends.apply(this, args); - } - - // Array.isArray 的 Polyfill - if (!Array.isArray) { - Array.isArray = (arg) => Object.prototype.toString.call(arg) === '[object Array]'; - } + return _extends.apply(this, args); + }; - ((window, document, localStorage, fetch) => { const $$ = (elementID) => document.getElementById(elementID); /** * msg - 提示信息 @@ -65,62 +60,46 @@ function DisqusJS(config) { * msg: DisqusJS 提示信息模板 * footer: 尾部信息模板 */ - const htmlTpl = { - /* - target="_blank" rel="external nofollow noopener noreferrer" - */ - aAttr: `target="_blank" rel="external nofollow noopener noreferrer"`, - /* -
- */ - msg: `
`, - /* - - */ - footer: ``, - /* -
- -
- */ - header: (num, title) => `
`, - /* -
-
- ${s.avatarEl} -
-
-
- ${authorEl} - -
-
- ${message} -
+ + /* + target="_blank" rel="external nofollow noopener noreferrer" + */ + const HTML_TPL_A_ATTR = `target="_blank" rel="external nofollow noopener noreferrer"`; + /* +
+ */ + const HTML_TPL_EL_MSG = `
`; + /* + + */ + const HTML_TPL_EL_FOOTER = ``; + + /* +
+
- */ - comment: ({ avatarEl, createdAt }, authorEl, message) => `
${avatarEl}
${authorEl}
${message}
`, - /* - 如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 尝试完整 Disqus 模式 | 强制完整 Disqus 模式 - */ - askForFull: '如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 尝试完整 Disqus 模式 | 强制完整 Disqus 模式' - } + + + */ + const HTML_TPL_EL_HEADER = (num, title) => `
`; + + const HTML_TPL_EL_COMMENT = ({ avatarEl, createdAt }, authorEl, message) => `
${avatarEl}
${authorEl}
${message}
`; + + const HTML_TPL_EL_ASK_FOR_FULL = '如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 尝试完整 Disqus 模式 | 强制完整 Disqus 模式'; /** * _get(url) - 对 Fetch 的一个封装 @@ -201,7 +180,7 @@ function DisqusJS(config) { // 显示提示信息 // Disqus 加载成功以后会把 #disqus_thread 内的内容全部覆盖 - $$('disqus_thread').innerHTML = `
评论完整模式加载中...如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或切换至 评论基础模式
${htmlTpl.footer}
` + $$('disqus_thread').innerHTML = `
评论完整模式加载中...如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理,或切换至 评论基础模式
${HTML_TPL_EL_FOOTER}
` $$('dsqjs-force-dsqjs').addEventListener('click', useDsqjs); s.src = `https://${disqusjs.config.shortname}.disqus.com/embed.js`; @@ -211,7 +190,7 @@ function DisqusJS(config) { } function checkDisqus() { - $$('disqus_thread').innerHTML = `
正在检查 Disqus 能否访问...
${htmlTpl.footer}
`; + $$('disqus_thread').innerHTML = `
正在检查 Disqus 能否访问...
${HTML_TPL_EL_FOOTER}
`; // 测试 Disqus 的域名 // *.disquscdn.com 没有被墙所以不做检查 @@ -262,54 +241,54 @@ function DisqusJS(config) { } } - function loadDsqjs() { - (() => { - // DisqusJS 加载中信息 - msg(`评论基础模式加载中。${htmlTpl.askForFull}`) - $$('dsqjs-reload-disqus').addEventListener('click', checkDisqus); - $$('dsqjs-force-disqus').addEventListener('click', useDsqjs); - - /* - * 获取 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] - */ - const url = `${disqusjs.config.api}3.0/threads/list.json?forum=${disqusjs.config.shortname}&thread=ident:${disqusjs.config.identifier}&api_key=${apikey()}`; + function assignClickEventForAskForFulButton() { + $$('dsqjs-reload-disqus').addEventListener('click', checkDisqus); + $$('dsqjs-force-disqus').addEventListener('click', useDisqus); + } - _get(url).then(({ data }) => { - if (data.code === 0 && data.response.length === 1) { - const resp = data.response[0]; - disqusjs.page = { - id: resp.id, - title: resp.title, - isClosed: resp.isClosed, - length: resp.posts, - comment: [] - }; + function loadDsqjs() { + // DisqusJS 加载中信息 + msg(`评论基础模式加载中。${HTML_TPL_EL_ASK_FOR_FULL}`) + assignClickEventForAskForFulButton(); - // 在 #disqus_thread 中填充 DisqusJS Container - $$('disqus_thread').innerHTML = `
评论基础模式加载中。${htmlTpl.askForFull}
${htmlTpl.header(resp.posts, disqusjs.config.siteName)}

    评论列表加载中...

加载更多评论
${htmlTpl.footer}
`; + /* + * 获取 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] + */ + const url = `${disqusjs.config.api}3.0/threads/list.json?forum=${disqusjs.config.shortname}&thread=ident:${disqusjs.config.identifier}&api_key=${apikey()}`; + + _get(url).then(({ data }) => { + if (data.code === 0 && data.response.length === 1) { + const resp = data.response[0]; + disqusjs.page = { + id: resp.id, + title: resp.title, + isClosed: resp.isClosed, + length: resp.posts, + comment: [] + }; - $$('dsqjs-reload-disqus').addEventListener('click', checkDisqus); - $$('dsqjs-force-disqus').addEventListener('click', useDsqjs); + // 在 #disqus_thread 中填充 DisqusJS Container + $$('disqus_thread').innerHTML = `
评论基础模式加载中。${HTML_TPL_EL_ASK_FOR_FULL}
${HTML_TPL_EL_HEADER(resp.posts, disqusjs.config.siteName)}

    评论列表加载中...

加载更多评论
${HTML_TPL_EL_FOOTER}
`; - $$(`dsqjs-order-${disqusjs.sortType}`).setAttribute('checked', 'true'); + assignClickEventForAskForFulButton(); - // 获取评论列表 - getComment() - } else if (data.code === 0 && data.response.length !== 1) { - // 当前页面可能还未初始化(需要创建 thread) - // Disqus API 的 threads/create 需要在服务端发起请求,不支持 AJAX Call - msg('该 Thread 并没有初始化,是否切换至 完整 Disqus 模式 进行初始化?') - $$('dsqjs-force-disqus').addEventListener('click', useDsqjs); - } else { - throw new Error; - } - }).catch(loadError) + $$(`dsqjs-order-${disqusjs.sortType}`).setAttribute('checked', 'true'); - })() + // 获取评论列表 + getComment() + } else if (data.code === 0 && data.response.length !== 1) { + // 当前页面可能还未初始化(需要创建 thread) + // Disqus API 的 threads/create 需要在服务端发起请求,不支持 AJAX Call + msg('当前 Thread 尚未创建。是否切换至 完整 Disqus 模式?') + $$('dsqjs-force-disqus').addEventListener('click', useDisqus); + } else { + throw new Error; + } + }).catch(loadError) /* * getComment(cursor) - 获取评论列表 @@ -449,9 +428,11 @@ function DisqusJS(config) { } } else if (data.code === 0 && data.response.length === 0) { // 当前没有评论,显示提示信息 - msg(`你可能无法访问 Disqus,已启用评论基础模式。${htmlTpl.askForFull}`) + msg(`你可能无法访问 Disqus,已启用评论基础模式。${HTML_TPL_EL_ASK_FOR_FULL}`) $$('dsqjs-post-container').innerHTML = `

${disqusjs.config.nocomment}

` - $$('dsqjs-reload-disqus').addEventListener('click', checkDisqus); + + assignClickEventForAskForFulButton(); + $$('dsqjs-force-disqus').addEventListener('click', useDsqjs); } else { throw new Error; @@ -489,9 +470,7 @@ function DisqusJS(config) { const getChildren = (id) => { // 如果没有子评论,就不需要解析子评论了 - if (childComments.length === 0) { - return null; - } + if (childComments.length === 0) return null; const list = []; for (const comment of childComments) { @@ -527,16 +506,16 @@ function DisqusJS(config) { if (data.comment.author.profileUrl) { /* Avatar Element - + Author Element - ${data.comment.author.name} + ${data.comment.author.name} */ - data.comment.avatarEl = `` - data.comment.authorEl = `${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}` @@ -589,7 +568,7 @@ function DisqusJS(config) { message = removeDisqUs(replaceDisquscdn(s.message)); } - return htmlTpl.comment(s, authorEl, message) + return HTML_TPL_EL_COMMENT(s, authorEl, message) } const childrenComments = (data) => { @@ -651,13 +630,12 @@ function DisqusJS(config) { // 增加提示信息 - msg(`你可能无法访问 Disqus,已启用评论基础模式。${htmlTpl.askForFull}`) + msg(`你可能无法访问 Disqus,已启用评论基础模式。${HTML_TPL_EL_ASK_FOR_FULL}`) $$('dsqjs-post-container').innerHTML = html; // 为 checkDisqus 和 useDsqjs 按钮添加事件 - $$('dsqjs-reload-disqus').addEventListener('click', checkDisqus); - $$('dsqjs-force-disqus').addEventListener('click', useDsqjs); + assignClickEventForAskForFulButton(); } } @@ -666,7 +644,8 @@ function DisqusJS(config) { */ function loadError(err) { console.log(err); - msg('评论基础模式加载失败,是否 重载尝试完整 Disqus 模式 ?') + msg('评论基础模式加载失败,是否 重载尝试完整 Disqus 模式 ?'); + $$('dsqjs-reload-dsqjs').addEventListener('click', loadDsqjs); $$('dsqjs-reload-disqus').addEventListener('click', checkDisqus); } @@ -676,16 +655,16 @@ function DisqusJS(config) { * useDsqjs() - 强制使用 Disqus */ function useDsqjs() { - setLS('dsqjs_mode', 'dsqjs') - loadDsqjs() + setLS('dsqjs_mode', 'dsqjs'); + loadDsqjs(); } - function useDsqjs() { - setLS('dsqjs_mode', 'disqus') - loadDisqus() + function useDisqus() { + setLS('dsqjs_mode', 'disqus'); + loadDisqus(); } - let disqusjs = {}; + const disqusjs = {}; disqusjs.config = _extends({ api: 'https://disqus.skk.moe/disqus/', @@ -713,7 +692,7 @@ function DisqusJS(config) { }; // 填充 DisqusJS 的 Container - $$('disqus_thread').innerHTML = `
${htmlTpl.msg}${htmlTpl.footer}
` + $$('disqus_thread').innerHTML = `
${HTML_TPL_EL_MSG}${HTML_TPL_EL_FOOTER}
` function initDsqjs() { disqusjs.mode = localStorage.getItem('dsqjs_mode'); @@ -736,10 +715,9 @@ function DisqusJS(config) { // 引入 Fetch 以后,一堆浏览器将不再被支持,所以加个判断,劝退一些浏览器 if (!fetch || !localStorage) { - msg(`你的浏览器版本过低,不兼容评论基础模式。${htmlTpl.askForFull}`); + msg(`你的浏览器版本过低,不兼容评论基础模式。${HTML_TPL_EL_ASK_FOR_FULL}`); - $$('dsqjs-reload-disqus').addEventListener('click', checkDisqus); - $$('dsqjs-force-disqus').addEventListener('click', useDsqjs); + assignClickEventForAskForFulButton(); } else { initDsqjs(); }