mastodon-embed-timeline/src/mastodon-timeline.min.js
2023-10-17 07:49:38 +00:00

2 lines
14 KiB
JavaScript

window.addEventListener("load",()=>{new MastodonApi({container_body_id:"mt-body",spinner_class:"loading-spinner",default_theme:"auto",instance_url:"https://mastodon.social",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,hide_counter_bar:!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.HIDE_COUNTER_BAR="undefined"!==t.hide_counter_bar&&t.hide_counter_bar,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=encodeURIComponent(this.PROFILE_NAME):"hashtag"===this.TIMELINE_TYPE?e="tags/"+encodeURIComponent(this.HASHTAG_NAME):"local"===this.TIMELINE_TYPE&&(e="public/local");let o='<div class="mt-footer"><a href="'+this.INSTANCE_URL+"/"+e+'" target="_blank" rel="nofollow noopener noreferrer">'+this.LINK_SEE_MORE+"</a></div>";this.mtBodyContainer.parentNode.insertAdjacentHTML("beforeend",o)}this.manageSpinner()}this.mtBodyContainer.addEventListener("click",function(t){("article"==t.target.localName||t.target.offsetParent?.localName=="article"||"img"==t.target.localName)&&i(t),"button"==t.target.localName&&"spoiler-btn"==t.target.className&&a(t)}),this.mtBodyContainer.addEventListener("keydown",function(t){"Enter"===t.key&&"article"==t.target.localName&&i(t)});let i=function(t){let e=t.target.closest(".mt-toot").dataset.location;"a"!==t.target.localName&&"span"!==t.target.localName&&"button"!==t.target.localName&&"time"!==t.target.localName&&"mt-toot-preview-noImage"!==t.target.className&&"mt-toot-avatar-image-big"!==t.target.parentNode.className&&"mt-toot-avatar-image-small"!==t.target.parentNode.className&&"mt-toot-preview-image"!==t.target.parentNode.className&&e&&window.open(e,"_blank","noopener")},a=function(t){let e=t.target.nextSibling;"img"===e.localName?(t.target.parentNode.classList.remove("mt-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 o(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 o=await e.json();return o}let i={};"profile"===this.TIMELINE_TYPE?i.timeline=`${this.INSTANCE_URL}/api/v1/accounts/${this.USER_ID}/statuses?limit=${this.TOOTS_LIMIT}`:"hashtag"===this.TIMELINE_TYPE?i.timeline=`${this.INSTANCE_URL}/api/v1/timelines/tag/${this.HASHTAG_NAME}?limit=${this.TOOTS_LIMIT}`:"local"===this.TIMELINE_TYPE&&(i.timeline=`${this.INSTANCE_URL}/api/v1/timelines/public?local=true&limit=${this.TOOTS_LIMIT}`),this.HIDE_EMOJOS||(i.emojos=this.INSTANCE_URL+"/api/v1/custom_emojis");let a=Object.entries(i).map(([t,i])=>o(i).then(e=>({[t]:e})).catch(o=>(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">'+o.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 o,i,a,s,r,n,l,d,c;t.reblog?(s=t.reblog.url,o='<a href="'+t.reblog.account.url+'" class="mt-toot-avatar" rel="nofollow noopener noreferrer" target="_blank"><div class="mt-toot-avatar-boosted"><div class="mt-toot-avatar-image-big"><img src="'+t.reblog.account.avatar+'" alt="'+this.escapeHtml(t.reblog.account.username)+' avatar" loading="lazy" /></div><div class="mt-toot-avatar-image-small"><img src="'+t.account.avatar+'" alt="'+this.escapeHtml(t.account.username)+' avatar" loading="lazy" /></div></div></a>',a=this.showEmojos(t.reblog.account.display_name?t.reblog.account.display_name:t.reblog.account.username,this.FETCHED_DATA.emojos),i='<div class="mt-toot-header-user"><a href="'+t.reblog.account.url+'" rel="nofollow noopener noreferrer" target="_blank">'+a+'<span class="visually-hidden"> account</span></a></div>',r=t.reblog.created_at,c=t.reblog.replies_count,d=t.reblog.reblogs_count,l=t.reblog.favourites_count):(s=t.url,o='<a href="'+t.account.url+'" class="mt-toot-avatar" rel="nofollow noopener noreferrer" target="_blank"><div class="mt-toot-avatar-standard"><div class="mt-toot-avatar-image-big"><img src="'+t.account.avatar+'" alt="'+this.escapeHtml(t.account.username)+' avatar" loading="lazy" /></div></div></a>',a=this.showEmojos(t.account.display_name?t.account.display_name:t.account.username,this.FETCHED_DATA.emojos),i='<div class="mt-toot-header-user"><a href="'+t.account.url+'" rel="nofollow noopener noreferrer" target="_blank">'+a+'<span class="visually-hidden"> account</span></a></div>',r=t.created_at,c=t.replies_count,d=t.reblogs_count,l=t.favourites_count),n=this.formatDate(r);let m='<div class="mt-toot-header-date"><a href="'+s+'" rel="nofollow noopener noreferrer" target="_blank"><time datetime="'+r+'">'+n+"</time></a></div>",h="";"0"!==this.TEXT_MAX_LINES&&(h="truncate",document.documentElement.style.setProperty("--text-max-lines",this.TEXT_MAX_LINES));let p="";p=""!==t.spoiler_text?'<div class="mt-toot-text">'+t.spoiler_text+' <button type="button" class="spoiler-btn" 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="mt-toot-text">'+t.reblog.spoiler_text+' <button type="button" class="spoiler-btn" 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="mt-toot-text'+h+'"><div class="mt-toot-text-wrapper">'+this.formatTootText(t.reblog.content)+"</div></div>":'<div class="mt-toot-text'+h+'"><div class="mt-toot-text-wrapper">'+this.formatTootText(t.content)+"</div></div>";let u="";if(t.media_attachments.length>0)for(let g in t.media_attachments)u=this.placeMedias(t.media_attachments[g],t.sensitive);if(t.reblog&&t.reblog.media_attachments.length>0)for(let v in t.reblog.media_attachments)u=this.placeMedias(t.reblog.media_attachments[v],t.reblog.sensitive);let E="";!this.HIDE_PREVIEW_LINK&&t.card&&(E=this.placePreviewLink(t.card));let _="";if(t.poll){let T="";for(let b in t.poll.options)T+="<li>"+t.poll.options[b].title+"</li>";_='<div class="mt-toot-poll '+(t.poll.expired?"mt-toot-poll-expired":"")+'"><ul>'+T+"</ul></div>"}let $="";if(!this.HIDE_COUNTER_BAR){let A='<div class="mt-toot-counter-bar-replies"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1792 1600" aria-hidden="true"><path d="M1792 1056q0 166-127 451q-3 7-10.5 24t-13.5 30t-13 22q-12 17-28 17q-15 0-23.5-10t-8.5-25q0-9 2.5-26.5t2.5-23.5q5-68 5-123q0-101-17.5-181t-48.5-138.5t-80-101t-105.5-69.5t-133-42.5t-154-21.5t-175.5-6H640v256q0 26-19 45t-45 19t-45-19L19 621Q0 602 0 576t19-45L531 19q19-19 45-19t45 19t19 45v256h224q713 0 875 403q53 134 53 333z"/></svg>'+c+"</div>",f='<div class="mt-toot-counter-bar-reblog"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1280" aria-hidden="true"><path d="M1280 1248q0 13-9.5 22.5t-22.5 9.5H288q-8 0-13.5-2t-9-7t-5.5-8t-3-11.5t-1-11.5V640H64q-26 0-45-19T0 576q0-24 15-41l320-384q19-22 49-22t49 22l320 384q15 17 15 41q0 26-19 45t-45 19H512v384h576q16 0 25 11l160 192q7 10 7 21zm640-416q0 24-15 41l-320 384q-20 23-49 23t-49-23l-320-384q-15-17-15-41q0-26 19-45t45-19h192V384H832q-16 0-25-12L647 180q-7-9-7-20q0-13 9.5-22.5T672 128h960q8 0 13.5 2t9 7t5.5 8t3 11.5t1 11.5v600h192q26 0 45 19t19 45z"/></svg>'+d+"</div>",L='<div class="mt-toot-counter-bar-favorites"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1664 1600" aria-hidden="true"><path d="M1664 615q0 22-26 48l-363 354l86 500q1 7 1 20q0 21-10.5 35.5T1321 1587q-19 0-40-12l-449-236l-449 236q-22 12-40 12q-21 0-31.5-14.5T301 1537q0-6 2-20l86-500L25 663Q0 636 0 615q0-37 56-46l502-73L783 41q19-41 49-41t49 41l225 455l502 73q56 9 56 46z"/></svg>'+l+"</div>";$='<div class="mt-toot-counter-bar">'+A+f+L+"</div>"}let I='<article class="mt-toot" aria-posinset="'+(e+1)+'" aria-setsize="'+this.TOOTS_LIMIT+'" data-location="'+s+'" tabindex="0"><div class="mt-toot-header">'+o+i+m+"</div>"+p+u+E+_+$+"</article>";return I},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>&gt;","</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 o of e){let i=RegExp(`\\:${o.shortcode}\\:`,"g");t=t.replace(i,`<img src="${o.url}" class="custom-emoji" alt="Emoji ${o.shortcode}" />`)}return t},MastodonApi.prototype.replaceHTMLtag=function(t,e,o,i,a){if(!t.includes(e))return t;{let s=RegExp(e+"(.*?)"+o,"gi");return t.replace(s,i+"$1"+a)}},MastodonApi.prototype.placeMedias=function(t,e){let o=e||!1,i='<div class="mt-toot-media img-ratio14_7 '+(o?"mt-toot-media-spoiler ":"")+this.SPINNER_CLASS+'">'+(o?'<button class="spoiler-btn">Show content</button>':"")+'<img src="'+t.preview_url+'" alt="'+(t.description?this.escapeHtml(t.description):"")+'" loading="lazy" /></div>';return i},MastodonApi.prototype.placePreviewLink=function(t){let e='<a href="'+t.url+'" class="mt-toot-preview" target="_blank" rel="noopener noreferrer">'+(t.image?'<div class="mt-toot-preview-image '+this.SPINNER_CLASS+'"><img src="'+t.image+'" alt="'+this.escapeHtml(t.image_description)+'" loading="lazy" /></div>':'<div class="mt-toot-preview-noImage">\uD83D\uDCC4</div>')+'</div><div class="mt-toot-preview-content">'+(t.provider_name?'<span class="mt-toot-preview-provider">'+this.parseHTMLstring(t.provider_name)+"</span>":"")+'<span class="mt-toot-preview-title">'+t.title+"</span>"+(t.author_name?'<span class="mt-toot-preview-author">'+this.parseHTMLstring(t.author_name)+"</span>":"")+"</div></a>";return e},MastodonApi.prototype.formatDate=function(t){let e=new Date(t),o=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",][e.getMonth()]+" "+e.getDate()+", "+e.getFullYear();return o},MastodonApi.prototype.parseHTMLstring=function(t){let e=new DOMParser,o=e.parseFromString(t,"text/html");return o.body.textContent},MastodonApi.prototype.escapeHtml=function(t){return(t??"").replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll('"',"&quot;").replaceAll("'","&#039;")},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)})};