mastodon-embed-timeline/src/mastodon-timeline.min.js
2023-04-03 17:13:31 +02:00

1 line
6.2 KiB
JavaScript

document.addEventListener("DOMContentLoaded",()=>{new MastodonApi({container_body_id:"mt-body",default_theme:"auto",instance_uri:"https://mastodon.online",user_id:"000180745",profile_name:"@idotj",toots_limit:"20",hide_reblog:!1,hide_replies:!1,text_max_lines:"0",link_see_more:"See more posts at Mastodon"})});let MastodonApi=function(t){this.DEFAULT_THEME=t.default_theme||"auto",this.INSTANCE_URI=t.instance_uri,this.USER_ID=t.user_id||"",this.PROFILE_NAME=this.USER_ID?t.profile_name:"",this.TOOTS_LIMIT=t.toots_limit||"20",this.HIDE_REBLOG=void 0!==t.hide_reblog&&t.hide_reblog,this.HIDE_REPLIES=void 0!==t.hide_replies&&t.hide_replies,this.TEXT_MAX_LINES=t.text_max_lines||"0",this.LINK_SEE_MORE=t.link_see_more,this.mtBodyContainer=document.getElementById(t.container_body_id),this.applyTheme(),this.getToots()};MastodonApi.prototype.applyTheme=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.getToots=function(){let t=this,e="";fetch(e=this.USER_ID?this.INSTANCE_URI+"/api/v1/accounts/"+this.USER_ID+"/statuses?limit="+this.TOOTS_LIMIT:this.INSTANCE_URI+"/api/v1/timelines/public?limit="+this.TOOTS_LIMIT,{method:"get"}).then(t=>{if(t.ok)return t.json();if(404===t.status)throw Error("404 Not found",{cause:t});throw Error(t.status)}).then(e=>{for(let o in this.mtBodyContainer.innerHTML="",e)("public"==e[o].visibility||"unlisted"==e[o].visibility)&&(t.HIDE_REBLOG&&e[o].reblog||t.HIDE_REPLIES&&e[o].in_reply_to_id||a.call(t,e[o],o));let r=document.querySelectorAll("#mt-body .hashtag, .u-url.mention");for(let i=0;i<r.length;i++)r[i].target="_blank",r[i].hasAttribute("rel","tag")?r[i].setAttribute("rel","tag nofollow noopener noreferrer"):r[i].setAttribute("rel","nofollow noopener noreferrer");if(t.LINK_SEE_MORE){let n="";n=this.USER_ID?'<div class="mt-footer"><a href="'+t.INSTANCE_URI+"/"+t.PROFILE_NAME+'" class="btn" target="_blank" rel="nofollow noopener noreferrer">'+t.LINK_SEE_MORE+"</a></div>":'<div class="mt-footer"><a href="'+t.INSTANCE_URI+'/public/local" class="btn" target="_blank" rel="nofollow noopener noreferrer">'+t.LINK_SEE_MORE+"</a></div>",this.mtBodyContainer.parentNode.insertAdjacentHTML("beforeend",n)}}).catch(t=>{this.mtBodyContainer.innerHTML='<div class="mt-error">✖️<br/><strong>Request Failed:</strong><br/>'+t+"</div>",this.mtBodyContainer.setAttribute("role","none")});let a=function(t,e){let a,o,r,i,n;t.reblog?(i=t.reblog.url,a='<a href="'+t.reblog.account.url+'" class="mt-avatar mt-avatar-boosted" style="background-image:url('+t.reblog.account.avatar+');" rel="nofollow noopener noreferrer" target="_blank"><div class="mt-avatar mt-avatar-booster" style="background-image:url('+t.account.avatar+');"></div><span class="visually-hidden">'+t.account.username+" avatar</span></a>",o='<div class="mt-user"><a href="'+t.reblog.account.url+'" rel="nofollow noopener noreferrer" target="_blank">'+t.reblog.account.username+'<span class="visually-hidden"> post</span></a></div>',n=this.formatDate(t.reblog.created_at)):(i=t.url,a='<a href="'+t.account.url+'" class="mt-avatar" style="background-image:url('+t.account.avatar+');" rel="nofollow noopener noreferrer" target="_blank"><span class="visually-hidden">'+t.account.username+" avatar</span></a>",o='<div class="mt-user"><a href="'+t.account.url+'" rel="nofollow noopener noreferrer" target="_blank">'+t.account.username+'<span class="visually-hidden"> post</span></a></div>',n=this.formatDate(t.created_at));let l="";"0"!==this.TEXT_MAX_LINES&&(l="truncate",document.documentElement.style.setProperty("--text-max-lines",this.TEXT_MAX_LINES)),r=""!==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">'+t.content+"</div></div>":t.reblog&&""!==t.reblog.content?'<div class="toot-text '+l+'"><div>'+t.reblog.content+"</div></div>":'<div class="toot-text '+l+'"><div>'+t.content+"</div></div>";let s="";if(t.media_attachments.length>0)for(let d in t.media_attachments)s=this.replaceMedias(t.media_attachments[d],t.sensitive);if(t.reblog&&t.reblog.media_attachments.length>0)for(let c in t.reblog.media_attachments)s=this.replaceMedias(t.reblog.media_attachments[c],t.sensitive);let m="",u="";if(t.poll){for(let h in t.poll.options)u+="<li>"+t.poll.options[h].title+"</li>";m='<div class="toot-poll"><ul>'+u+"</ul></div>"}let p='<div class="toot-date"><a href="'+i+'" rel="nofollow noopener noreferrer" tabindex="-1" target="_blank">'+n+"</a></div>",g='<article class="mt-toot" aria-posinset="'+(Number(e)+1)+'" aria-setsize="'+this.TOOTS_LIMIT+'" data-location="'+i+'" tabindex="0">'+a+o+r+s+m+p+"</article>";this.mtBodyContainer.insertAdjacentHTML("beforeend",g)};this.mtBodyContainer.addEventListener("click",function(t){("article"==t.target.localName||"article"==t.target.offsetParent.localName||"img"==t.target.localName)&&openTootURL(t),"button"==t.target.localName&&"spoiler-link"==t.target.className&&toogleSpoiler(t)}),this.mtBodyContainer.addEventListener("keydown",function(t){"Enter"===t.code&&"article"==t.target.localName&&openTootURL(t)}),openTootURL=function(t){let e=t.target.closest(".mt-toot").dataset.location;"a"!==t.target.localName&&"span"!==t.target.localName&&"button"!==t.target.localName&&e&&window.open(e,"_blank")},toogleSpoiler=function(t){let e=t.target.nextSibling,a=t.target.textContent;e.classList.toggle("spoiler-text"),"Show more"==a?(a="Show less",t.target.setAttribute("aria-expanded","true")):(a="Show more",t.target.setAttribute("aria-expanded","false"))}},MastodonApi.prototype.replaceMedias=function(t,e){return'<div class="toot-media '+(e?"toot-media-spoiler":"")+' img-ratio14_7 loading-spinner"><img onload="removeSpinner(this)" onerror="removeSpinner(this)" src="'+t.preview_url+'" alt="" loading="lazy" /></div>'},MastodonApi.prototype.formatDate=function(t){let e=new Date(t);return["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][e.getMonth()]+" "+e.getDate()+", "+e.getFullYear()},removeSpinner=function(t){let e="loading-spinner",a=t.closest("."+e);a&&a.classList.remove(e)};