1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
window.addEventListener("load",()=>{new MastodonApi({container_body_id:"mt-body",spinner_class:"loading-spinner",default_theme:"auto",instance_url:"https://mastodon.online",timeline_type:"local",user_id:"",profile_name:"",hashtag_name:"",toots_limit:"20",hide_unlisted:!1,hide_reblog:!1,hide_replies:!1,hide_preview_link:!1,hide_emojos:!1,markdown_blockquote:!1,text_max_lines:"0",link_see_more:"See more posts at Mastodon"})});const MastodonApi=function(t){this.CONTAINER_BODY_ID=t.container_body_id||"mt-body",this.SPINNER_CLASS=t.spinner_class||"loading-spinner",this.DEFAULT_THEME=t.default_theme||"auto",this.INSTANCE_URL=t.instance_url,this.USER_ID=t.user_id||"",this.PROFILE_NAME=this.USER_ID?t.profile_name:"",this.TIMELINE_TYPE=t.timeline_type||"local",this.HASHTAG_NAME=t.hashtag_name||"",this.TOOTS_LIMIT=t.toots_limit||"20",this.HIDE_UNLISTED=void 0!==t.hide_unlisted&&t.hide_unlisted,this.HIDE_REBLOG=void 0!==t.hide_reblog&&t.hide_reblog,this.HIDE_REPLIES=void 0!==t.hide_replies&&t.hide_replies,this.HIDE_PREVIEW_LINK=void 0!==t.hide_preview_link&&t.hide_preview_link,this.HIDE_EMOJOS=void 0!==t.hide_emojos&&t.hide_emojos,this.MARKDOWN_BLOCKQUOTE=void 0!==t.markdown_blockquote&&t.markdown_blockquote,this.TEXT_MAX_LINES=t.text_max_lines||"0",this.LINK_SEE_MORE=t.link_see_more,this.FETCHED_DATA={},this.mtBodyContainer=document.getElementById(this.CONTAINER_BODY_ID),this.buildTimeline()};MastodonApi.prototype.buildTimeline=async function(){for(let t in this.setTheme(),await this.getTimelineData(),this.mtBodyContainer.innerHTML="",this.FETCHED_DATA.timeline)("public"==this.FETCHED_DATA.timeline[t].visibility||!this.HIDE_UNLISTED&&"unlisted"==this.FETCHED_DATA.timeline[t].visibility)&&(this.HIDE_REBLOG&&this.FETCHED_DATA.timeline[t].reblog||this.HIDE_REPLIES&&this.FETCHED_DATA.timeline[t].in_reply_to_id||this.appendToot(this.FETCHED_DATA.timeline[t],Number(t)));if(""===this.mtBodyContainer.innerHTML)this.mtBodyContainer.setAttribute("role","none"),this.mtBodyContainer.innerHTML='<div class="mt-error"><span class="mt-error-icon">\uD83D\uDCED</span><br/><strong>Sorry, no toots to show</strong><br/><div class="mt-error-message">Got '+this.FETCHED_DATA.timeline.length+' toots from the server but due to the "hide filters" applied, no toot is shown</div></div>';else{if(this.LINK_SEE_MORE){let e="";"profile"===this.TIMELINE_TYPE?e=this.PROFILE_NAME:"hashtag"===this.TIMELINE_TYPE?e="tags/"+this.HASHTAG_NAME:"local"===this.TIMELINE_TYPE&&(e="public/local");let i='<div class="mt-footer"><a href="'+this.INSTANCE_URL+"/"+e+'" class="btn" target="_blank" rel="nofollow noopener noreferrer">'+this.LINK_SEE_MORE+"</a></div>";this.mtBodyContainer.parentNode.insertAdjacentHTML("beforeend",i)}this.manageSpinner()}this.mtBodyContainer.addEventListener("click",function(t){("article"==t.target.localName||"article"==t.target.offsetParent.localName||"img"==t.target.localName&&"mt-avatar"!==t.target.offsetParent.className&&"mt-avatar-account"!==t.target.offsetParent.className)&&o(t),"button"==t.target.localName&&"spoiler-link"==t.target.className&&a(t)}),this.mtBodyContainer.addEventListener("keydown",function(t){"Enter"===t.key&&"article"==t.target.localName&&o(t)});let o=function(t){let e=t.target.closest(".mt-toot").dataset.location;"a"!==t.target.localName&&"span"!==t.target.localName&&"button"!==t.target.localName&&"toot-preview-image"!==t.target.parentNode.className&&e&&window.open(e,"_blank")},a=function(t){let e=t.target.nextSibling;"img"===e.localName?(t.target.parentNode.classList.remove("toot-media-spoiler"),t.target.style.display="none"):(e.classList.contains("spoiler-text-hidden")||e.classList.contains("spoiler-text-visible"))&&("Show more"==t.target.textContent?(e.classList.remove("spoiler-text-hidden"),e.classList.add("spoiler-text-visible"),t.target.setAttribute("aria-expanded","true"),t.target.textContent="Show less"):(e.classList.remove("spoiler-text-visible"),e.classList.add("spoiler-text-hidden"),t.target.setAttribute("aria-expanded","false"),t.target.textContent="Show more"))}},MastodonApi.prototype.setTheme=function(){let t=function(t){document.documentElement.setAttribute("data-theme",t)};if("auto"===this.DEFAULT_THEME){let e=window.matchMedia("(prefers-color-scheme: dark)");e.matches?t("dark"):t("light"),e.addEventListener("change",e=>{e.matches?t("dark"):t("light")})}else t(this.DEFAULT_THEME)},MastodonApi.prototype.getTimelineData=async function(){return new Promise((t,e)=>{async function i(t){let e=await fetch(t);if(!e.ok)throw Error("Failed to fetch the following URL: "+t+"<hr>Error status: "+e.status+"<hr>Error message: "+e.statusText);let i=await e.json();return i}let o={};"profile"===this.TIMELINE_TYPE?o.timeline=`${this.INSTANCE_URL}/api/v1/accounts/${this.USER_ID}/statuses?limit=${this.TOOTS_LIMIT}`:"hashtag"===this.TIMELINE_TYPE?o.timeline=`${this.INSTANCE_URL}/api/v1/timelines/tag/${this.HASHTAG_NAME}?limit=${this.TOOTS_LIMIT}`:"local"===this.TIMELINE_TYPE&&(o.timeline=`${this.INSTANCE_URL}/api/v1/timelines/public?local=true&limit=${this.TOOTS_LIMIT}`),this.HIDE_EMOJOS||(o.emojos=this.INSTANCE_URL+"/api/v1/custom_emojis");let a=Object.entries(o).map(([t,o])=>i(o).then(e=>({[t]:e})).catch(i=>(e(Error("Something went wrong fetching data")),this.mtBodyContainer.innerHTML='<div class="mt-error"><span class="mt-error-icon">❌</span><br/><strong>Sorry, request failed:</strong><br/><div class="mt-error-message">'+i.message+"</div></div>",this.mtBodyContainer.setAttribute("role","none"),{[t]:[]})));Promise.all(a).then(e=>{this.FETCHED_DATA=e.reduce((t,e)=>({...t,...e}),{}),t()})})},MastodonApi.prototype.appendToot=function(t,e){this.mtBodyContainer.insertAdjacentHTML("beforeend",this.assambleToot(t,e))},MastodonApi.prototype.assambleToot=function(t,e){let i,o,a,s,n;t.reblog?(s=t.reblog.url,i='<a href="'+t.reblog.account.url+'" class="mt-avatar" rel="nofollow noopener noreferrer" target="_blank"><div class="mt-avatar-image"><div class="mt-avatar-boosted"><img src="'+t.reblog.account.avatar+'" alt="'+t.reblog.account.username+' avatar" loading="lazy" /></div><div class="mt-avatar-account"><img src="'+t.account.avatar+'" alt="'+t.account.username+' avatar" loading="lazy" /></div></div></a>',o='<div class="mt-user"><a href="'+t.reblog.account.url+'" rel="nofollow noopener noreferrer" target="_blank">'+(t.reblog.account.display_name?t.reblog.account.display_name:t.reblog.account.username)+'<span class="visually-hidden"> account</span></a></div>',n=this.formatDate(t.reblog.created_at)):(s=t.url,i='<a href="'+t.account.url+'" class="mt-avatar" rel="nofollow noopener noreferrer" target="_blank"><div class="mt-avatar-image"><img src="'+t.account.avatar+'" alt="'+t.account.username+' avatar" loading="lazy" /></div></a>',o='<div class="mt-user"><a href="'+t.account.url+'" rel="nofollow noopener noreferrer" target="_blank">'+(t.account.display_name?t.account.display_name:t.account.username)+'<span class="visually-hidden"> account</span></a></div>',n=this.formatDate(t.created_at));let r="";"0"!==this.TEXT_MAX_LINES&&(r="truncate",document.documentElement.style.setProperty("--text-max-lines",this.TEXT_MAX_LINES)),a=""!==t.spoiler_text?'<div class="toot-text">'+t.spoiler_text+' <button type="button" class="spoiler-link" aria-expanded="false">Show more</button><div class="spoiler-text-hidden">'+this.formatTootText(t.content)+"</div></div>":t.reblog&&""!==t.reblog.content&&""!==t.reblog.spoiler_text?'<div class="toot-text">'+t.reblog.spoiler_text+' <button type="button" class="spoiler-link" aria-expanded="false">Show more</button><div class="spoiler-text-hidden">'+this.formatTootText(t.reblog.content)+"</div></div>":t.reblog&&""!==t.reblog.content&&""===t.reblog.spoiler_text?'<div class="toot-text '+r+'"><div>'+this.formatTootText(t.reblog.content)+"</div></div>":'<div class="toot-text '+r+'"><div>'+this.formatTootText(t.content)+"</div></div>";let l="";if(t.media_attachments.length>0)for(let d in t.media_attachments)l=this.placeMedias(t.media_attachments[d],t.sensitive);if(t.reblog&&t.reblog.media_attachments.length>0)for(let c in t.reblog.media_attachments)l=this.placeMedias(t.reblog.media_attachments[c],t.reblog.sensitive);let h="";!this.HIDE_PREVIEW_LINK&&t.card&&(h=this.placePreviewLink(t.card));let m="",p="";if(t.poll){for(let u in t.poll.options)p+="<li>"+t.poll.options[u].title+"</li>";m='<div class="toot-poll"><ul>'+p+"</ul></div>"}let E='<div class="toot-date"><a href="'+s+'" rel="nofollow noopener noreferrer" tabindex="-1" target="_blank">'+n+"</a></div>",g='<article class="mt-toot" aria-posinset="'+(e+1)+'" aria-setsize="'+this.TOOTS_LIMIT+'" data-location="'+s+'" tabindex="0">'+i+o+a+l+h+m+E+"</article>";return g},MastodonApi.prototype.formatTootText=function(t){let e=t;return e=this.addTarget2hashtagMention(e),this.HIDE_EMOJOS||(e=this.showEmojos(e,this.FETCHED_DATA.emojos)),this.MARKDOWN_BLOCKQUOTE&&(e=this.replaceHTMLtag(e,"<p>>","</p>","<blockquote><p>","</p></blockquote>")),e},MastodonApi.prototype.addTarget2hashtagMention=function(t){let e=t.replaceAll('rel="tag"','rel="tag" target="_blank"');return e.replaceAll('class="u-url mention"','class="u-url mention" target="_blank"')},MastodonApi.prototype.showEmojos=function(t,e){if(!t.includes(":"))return t;for(let i of e){let o=RegExp(`\\:${i.shortcode}\\:`,"g");t=t.replace(o,`<img src="${i.url}" class="custom-emoji" alt="Emoji ${i.shortcode}" />`)}return t},MastodonApi.prototype.replaceHTMLtag=function(t,e,i,o,a){if(!t.includes(e))return t;{let s=RegExp(e+"(.*?)"+i,"gi");return t.replace(s,o+"$1"+a)}},MastodonApi.prototype.placeMedias=function(t,e){let i=e||!1,o='<div class="toot-media '+(i?"toot-media-spoiler":"")+" img-ratio14_7 "+this.SPINNER_CLASS+'">'+(i?'<button class="spoiler-link">Show content</button>':"")+'<img src="'+t.preview_url+'" alt="'+(t.description?t.description:"")+'" loading="lazy" /></div>';return o},MastodonApi.prototype.placePreviewLink=function(t){return'<a href="'+t.url+'" class="toot-preview-link" target="_blank" rel="noopener noreferrer">'+(t.image?'<div class="toot-preview-image '+this.SPINNER_CLASS+'"><img src="'+t.image+'" alt="" loading="lazy" /></div>':'<div class="toot-preview-noImage">\uD83D\uDCC4</div>')+'</div><div class="toot-preview-content">'+(t.provider_name?'<span class="toot-preview-provider">'+t.provider_name+"</span>":"")+'<span class="toot-preview-title">'+t.title+"</span>"+(t.author_name?'<span class="toot-preview-author">By '+t.author_name+"</span>":"")+"</div></a>"},MastodonApi.prototype.formatDate=function(t){let e=new Date(t),i=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",][e.getMonth()]+" "+e.getDate()+", "+e.getFullYear();return i},MastodonApi.prototype.manageSpinner=function(){let t=this.SPINNER_CLASS,e=function(){this.parentNode.classList.remove(t),this.removeEventListener("load",e),this.removeEventListener("error",e)};this.mtBodyContainer.querySelectorAll(`.${this.SPINNER_CLASS} > img`).forEach(t=>{t.addEventListener("load",e),t.addEventListener("error",e)})}; |