halo-theme-hao/templates/assets/zhheo/commentBarrage.js

1 line
4.5 KiB
JavaScript

"use strict";function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,r){if(e){if("string"==typeof e)return _arrayLikeToArray(e,r);var a=Object.prototype.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?_arrayLikeToArray(e,r):void 0}}function _iterableToArray(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,r){(null==r||r>e.length)&&(r=e.length);for(var a=0,t=new Array(r);a<r;a++)t[a]=e[a];return t}var commentBarrageConfig={maxBarrage:1,barrageTime:4e3,twikooUrl:"https://twikoo.zhheo.com/",accessToken:"ad44f65c5bda8a966a13f8a5326960b7",pageUrl:window.location.pathname,barrageTimer:[],barrageList:[],barrageIndex:0,dom:document.querySelector(".comment-barrage")},commentInterval=null,hoverOnCommentBarrage=!1;function initCommentBarrage(){var e=JSON.stringify({event:"COMMENT_GET","commentBarrageConfig.accessToken":commentBarrageConfig.accessToken,url:commentBarrageConfig.pageUrl}),r=new XMLHttpRequest;r.withCredentials=!0,r.addEventListener("readystatechange",function(){4===this.readyState&&(commentBarrageConfig.barrageList=commentLinkFilter(JSON.parse(this.responseText).data),commentBarrageConfig.dom.innerHTML="")}),r.open("POST",commentBarrageConfig.twikooUrl),r.setRequestHeader("Content-Type","application/json"),r.send(e),clearInterval(commentInterval),commentInterval=null,commentInterval=setInterval(function(){commentBarrageConfig.barrageList.length&&!hoverOnCommentBarrage&&(popCommentBarrage(commentBarrageConfig.barrageList[commentBarrageConfig.barrageIndex]),commentBarrageConfig.barrageIndex+=1,commentBarrageConfig.barrageIndex%=commentBarrageConfig.barrageList.length),commentBarrageConfig.barrageTimer.length>(commentBarrageConfig.barrageList.length>commentBarrageConfig.maxBarrage?commentBarrageConfig.maxBarrage:commentBarrageConfig.barrageList.length)&&!hoverOnCommentBarrage&&removeCommentBarrage(commentBarrageConfig.barrageTimer.shift())},commentBarrageConfig.barrageTime)}function commentLinkFilter(e){e.sort(function(e,r){return e.created-r.created});var r=[];return e.forEach(function(e){r.push.apply(r,_toConsumableArray(getCommentReplies(e)))}),r}function getCommentReplies(e){if(e.replies){var r=[e];return e.replies.forEach(function(e){r.push.apply(r,_toConsumableArray(getCommentReplies(e)))}),r}return[]}function popCommentBarrage(e){var r=document.createElement("div");commentBarrageConfig.dom.clientWidth,commentBarrageConfig.dom.clientHeight;r.className="comment-barrage-item",r.innerHTML='\n\t\t<div class="barrageHead">\n <div class="barrageTitle">热评</div>\n\t\t\t<div class="barrageNick">'.concat(e.nick,'</div>\n\t\t\t<img class="barrageAvatar" src="https://cravatar.cn/avatar/').concat(e.mailMd5,'"/>\n\t\t\t<a class="comment-barrage-close" href="javascript:heo.switchCommentBarrage()"><i class="fa-solid fa-xmark"></i></a>\n\t\t</div>\n\t\t<div class="barrageContent">').concat(e.comment,"</div>\n\t"),commentBarrageConfig.barrageTimer.push(r),commentBarrageConfig.dom.append(r)}function removeCommentBarrage(e){e.className="comment-barrage-item out",setTimeout(function(){commentBarrageConfig.dom.removeChild(e)},1e3)}$(".comment-barrage").hover(function(){hoverOnCommentBarrage=!0,console.log("热评悬浮")},function(){hoverOnCommentBarrage=!1,console.log("停止悬浮")}),document.addEventListener("scroll",btf.throttle(function(){var e,r=window.scrollY+document.documentElement.clientHeight,a=(window.scrollY,document.querySelector(".comment-barrage")),t=document.getElementById("post-tools");t&&a&&(e=t.offsetTop+t.offsetHeight/2,768<document.body.clientWidth&&(a.style.bottom=r<e?"0":"-200px"))},200)),initCommentBarrage(),"false"!==localStorage.getItem("commentBarrageSwitch")?($(".comment-barrage").show(),$(".menu-commentBarrage-text").text("关闭热评"),document.querySelector("#consoleCommentBarrage").classList.add("on")):($(".comment-barrage").hide(),$(".menu-commentBarrage-text").text("显示热评"),document.querySelector("#consoleCommentBarrage").classList.remove("on")),document.addEventListener("pjax:send",function(){clearInterval(commentInterval)});