mastodon-embed-timeline/src/mastodon-timeline.min.js
2023-07-10 18:16:53 +00:00

1 line
7.5 KiB
JavaScript

document.addEventListener("DOMContentLoaded",()=>{new MastodonApi({container_body_id:"mt-body",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,markdown_blockquote:!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_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.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.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="";"profile"===this.TIMELINE_TYPE?e=`${this.INSTANCE_URL}/api/v1/accounts/${this.USER_ID}/statuses?limit=${this.TOOTS_LIMIT}`:"hashtag"===this.TIMELINE_TYPE?e=`${this.INSTANCE_URL}/api/v1/timelines/tag/${this.HASHTAG_NAME}?limit=${this.TOOTS_LIMIT}`:"local"===this.TIMELINE_TYPE&&(e=`${this.INSTANCE_URL}/api/v1/timelines/public?local=true&limit=${this.TOOTS_LIMIT}`),fetch(e,{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 a in this.mtBodyContainer.innerHTML="",e)("public"==e[a].visibility||!this.HIDE_UNLISTED&&"unlisted"==e[a].visibility)&&(t.HIDE_REBLOG&&e[a].reblog||t.HIDE_REPLIES&&e[a].in_reply_to_id||o.call(t,e[a],a));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 '+e.length+' toots from the server but due to the "hide filters" applied, no toot is shown</div></div>';else if(t.LINK_SEE_MORE){let i="";"profile"===this.TIMELINE_TYPE?i=t.PROFILE_NAME:"hashtag"===this.TIMELINE_TYPE?i="tags/"+this.HASHTAG_NAME:"local"===this.TIMELINE_TYPE&&(i="public/local");let n='<div class="mt-footer"><a href="'+t.INSTANCE_URL+"/"+i+'" 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"><span class="mt-error-icon">❌</span><br/><strong>Sorry, request failed:</strong><br/><div class="mt-error-message">'+t+"</div></div>",this.mtBodyContainer.setAttribute("role","none")});let o=function(t,e){let o,a,i,n,r;t.reblog?(n=t.reblog.url,o='<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>",a='<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>',r=this.formatDate(t.reblog.created_at)):(n=t.url,o='<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>",a='<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>',r=this.formatDate(t.created_at));let s="";"0"!==this.TEXT_MAX_LINES&&(s="truncate",document.documentElement.style.setProperty("--text-max-lines",this.TEXT_MAX_LINES)),i=""!==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">'+this.formatTootText(t.content)+"</div></div>":t.reblog&&""!==t.reblog.content?'<div class="toot-text '+s+'"><div>'+this.formatTootText(t.reblog.content)+"</div></div>":'<div class="toot-text '+s+'"><div>'+this.formatTootText(t.content)+"</div></div>";let l="";if(t.media_attachments.length>0)for(let d in t.media_attachments)l=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)l=this.replaceMedias(t.reblog.media_attachments[c],t.sensitive);let h="",m="";if(t.poll){for(let u in t.poll.options)m+="<li>"+t.poll.options[u].title+"</li>";h='<div class="toot-poll"><ul>'+m+"</ul></div>"}let p='<div class="toot-date"><a href="'+n+'" rel="nofollow noopener noreferrer" tabindex="-1" target="_blank">'+r+"</a></div>",g='<article class="mt-toot" aria-posinset="'+(Number(e)+1)+'" aria-setsize="'+this.TOOTS_LIMIT+'" data-location="'+n+'" tabindex="0">'+o+a+i+l+h+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)&&a(t),"button"==t.target.localName&&"spoiler-link"==t.target.className&&i(t)}),this.mtBodyContainer.addEventListener("keydown",function(t){"Enter"===event.code&&"article"==t.target.localName&&a(t)});let a=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")},i=function(t){let e=t.target.nextSibling,o=t.target.textContent;e.classList.toggle("spoiler-text"),"Show more"==o?(o="Show less",t.target.setAttribute("aria-expanded","true")):(o="Show more",t.target.setAttribute("aria-expanded","false"))}},MastodonApi.prototype.formatTootText=function(t){let e=t;return e=this.addTarget2hashtagMention(e),this.MARKDOWN_BLOCKQUOTE&&(e=this.replaceHTMLtag(e,"<p>&gt;","</p>","<blockquote><p>","</blockquote></p>")),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.replaceHTMLtag=function(t,e,o,a,i){if(!t.includes(e))return t;{let n=RegExp(e+"(.*?)"+o,"gi");return t.replace(n,a+"$1"+i)}},MastodonApi.prototype.replaceMedias=function(t,e){let o=e||!1;return'<div class="toot-media '+(o?"toot-media-spoiler":"")+' img-ratio14_7 loading-spinner">'+(o?'<button class="spoiler-link" onclick="this.parentNode.classList.remove(\'toot-media-spoiler\')">Show content</button>':"")+'<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",o=t.closest("."+e);o&&o.classList.remove(e)};