hexo-theme-stellar/dist/js/issues.js

1 line
3.4 KiB
JavaScript

const IssuesAPI={requestIssuesAPI(s,e,t){let i=10;!function n(){return new Promise(((a,o)=>{let r=0,l=setTimeout((()=>{0===r&&(r=2,l=null,o("请求超时"),0==i&&t())}),5e3);fetch(s).then((function(s){if(2!==r&&(clearTimeout(l),a(s),l=null,r=1),s.ok)return s.json();throw new Error("Network response was not ok.")})).then((function(s){i=0,e(s)})).catch((function(s){i>0?(i-=1,setTimeout((()=>{n()}),5e3)):t()}))}))}()},parseIssueStrToJson(s){let e=s.match(/```json[\s|\S]*```/);if(e&&e.length>0&&(e=e[0]),e&&(e=e.split("```json")[1].split("```")[0],e))return JSON.parse(e)},groupIssuesData(s,e){var t=new Object;if(e.length>0)if(null!=s.group){const a=s.group.split("=");if(a.length>1){const o=a[0];let r=a[1];for(o&&r&&(r=r.split(",")),s.group=r,i=0;i<e.length;i++){const s=this.parseIssueStrToJson(e[i].body);if(s&&o in s){let e=s[o];e=e.replace(", ",",").split(",");for(var n=0;n<e.length;n++)if(r.includes(e[n])){let i=t[e[n]];null==i&&(i=new Array),i.push(s),t[e[n]]=i}}}}}else for(s.group=[""],i=0;i<e.length;i++){const s=this.parseIssueStrToJson(e[i].body);if(s){let e=t[""];null==e&&(e=new Array),e.push(s),t[""]=e}}return t},getIssuesAPIForSites(s){const e=$(s.el)[0];$(e).append('<div class="loading"><i class="fa fa-cog fa-2x fa-spin"></i><p>正在加载</p></div>'),this.requestIssuesAPI(s.api,(function(t){$(e).find(".loading").remove();const i=IssuesAPI.groupIssuesData(s,t),n=Object.keys(i);s.group.forEach(((s,t)=>{const a=i[s];if(a&&a.length>0)for(s.length>0?$(e).append("<h2>"+s+"</h2>"):""==name&&n.length>1&&$(e).append("<h2>未分组</h2>"),$(e).append('<div class="site-card-group '+t+'"></div>'),j=0;j<a.length;j++){const s=a[j];let i="";i=s.screenshot&&s.screenshot.length>0?'<div class="img"><img src="'+s.screenshot+'" onerror="javascript:this.src=\'https://image.thum.io/get/width/1024/crop/768/'+s.url+"';\"/></div>":'<div class="img"></div>';let n='<div class="info">';s.avatar&&s.avatar.length>0&&(n+='<img src="'+s.avatar+'" onerror="javascript:this.src=\'https://image.thum.io/get/width/1024/crop/768/'+s.url+"';\"/>"),n+='<span class="title">'+s.title+'</span><span class="desc">'+s.description+"</span></div>";const o="<div><a class='site-card' target='_blank' href='"+s.url+"'>"+i+n+"</a></div>";$(e).find(".site-card-group."+t).append(o)}}))}),(function(){$(e).find(".loading i").remove(),$(e).find(".loading p").text("加载失败,请稍后重试。")}))},getIssuesAPIForTimeline(s){const e=$(s.el)[0];$(e).append('<div class="loading"><i class="fa fa-cog fa-2x fa-spin"></i><p>正在加载</p></div>'),this.requestIssuesAPI(s.api,(function(s){if($(e).find(".loading").remove(),s.length>0)for(i=0;i<s.length;i++){const t='&nbsp;&nbsp;<a class="comments" target="_blank" href="'+s[i].html_url+'"><i class="fa fa-comment-dots fa-fw"></i>'+s[i].comments+"</a>",n='<div class="timenode">'+('<div class="header"><p></p><p>'+s[i].title+t+"</p><p></p></div>")+('<div class="body"><p>'+s[i].body+"</p></div>")+"</div>";$(e).append(n)}}),(function(){$(e).find(".loading i").remove(),$(e).find(".loading p").text("加载失败,请稍后重试。")}))},request(){const s=document.getElementsByClassName("issues-wrap");for(var e=0;e<s.length;e++){const i=s[e],n=i.getAttribute("api"),a=i.getAttribute("group");var t=new Object;t.class=i.getAttribute("class"),t.el=i,t.api=n,t.group=a,t.class.split(" ").includes("sites")?this.getIssuesAPIForSites(t):t.class.split(" ").includes("timeline")&&this.getIssuesAPIForTimeline(t)}}};$((function(){IssuesAPI.request()}));