diff --git a/CHANGELOG b/CHANGELOG index ce78fcb..4542454 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +v3.7.1 - 15/07/2023 +- Use window.onload to take async attribute into account + v3.7.0 - 10/07/2023 - Add Markdown to HTML feature - Add option to change Markdown symbol ">" into a blockquote HTML tag diff --git a/src/mastodon-timeline.css b/src/mastodon-timeline.css index 4c2d615..64c9318 100644 --- a/src/mastodon-timeline.css +++ b/src/mastodon-timeline.css @@ -1,4 +1,4 @@ -/* Mastodon embed feed timeline v3.7.0 */ +/* Mastodon embed feed timeline v3.7.1 */ /* More info at: */ /* https://gitlab.com/idotj/mastodon-embed-feed-timeline */ diff --git a/src/mastodon-timeline.js b/src/mastodon-timeline.js index 1b1025d..11c0174 100644 --- a/src/mastodon-timeline.js +++ b/src/mastodon-timeline.js @@ -1,9 +1,9 @@ -// Mastodon embed feed timeline v3.7.0 +// Mastodon embed feed timeline v3.7.1 // More info at: // https://gitlab.com/idotj/mastodon-embed-feed-timeline // Timeline settings -document.addEventListener("DOMContentLoaded", () => { +window.addEventListener("load", () => { let mapi = new MastodonApi({ // Id of the
containing the timeline container_body_id: "mt-body", diff --git a/src/mastodon-timeline.min.js b/src/mastodon-timeline.min.js index d7c147c..8e28c6c 100644 --- a/src/mastodon-timeline.min.js +++ b/src/mastodon-timeline.min.js @@ -1 +1 @@ -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='
\uD83D\uDCED
Sorry, no toots to show
Got '+e.length+' toots from the server but due to the "hide filters" applied, no toot is shown
';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='";this.mtBodyContainer.parentNode.insertAdjacentHTML("beforeend",n)}}).catch(t=>{this.mtBodyContainer.innerHTML='

Sorry, request failed:
'+t+"
",this.mtBodyContainer.setAttribute("role","none")});let o=function(t,e){let o,a,i,n,r;t.reblog?(n=t.reblog.url,o='
'+t.account.username+" avatar
",a='
'+t.reblog.account.username+' post
',r=this.formatDate(t.reblog.created_at)):(n=t.url,o=''+t.account.username+" avatar",a='
'+t.account.username+' post
',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?'
'+t.spoiler_text+'
'+this.formatTootText(t.content)+"
":t.reblog&&""!==t.reblog.content?'
'+this.formatTootText(t.reblog.content)+"
":'
'+this.formatTootText(t.content)+"
";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+="
  • "+t.poll.options[u].title+"
  • ";h='
    "}let p='
    '+r+"
    ",g='
    '+o+a+i+l+h+p+"
    ";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,"

    >","

    ","

    ","

    ")),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'
    '+(o?'':"")+'
    '},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)}; \ No newline at end of file +window.addEventListener("load",()=>{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='
    \uD83D\uDCED
    Sorry, no toots to show
    Got '+e.length+' toots from the server but due to the "hide filters" applied, no toot is shown
    ';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 r='";this.mtBodyContainer.parentNode.insertAdjacentHTML("beforeend",r)}}).catch(t=>{this.mtBodyContainer.innerHTML='

    Sorry, request failed:
    '+t+"
    ",this.mtBodyContainer.setAttribute("role","none")});let o=function(t,e){let o,a,i,r,n;t.reblog?(r=t.reblog.url,o='
    '+t.account.username+" avatar
    ",a='
    '+t.reblog.account.username+' post
    ',n=this.formatDate(t.reblog.created_at)):(r=t.url,o=''+t.account.username+" avatar",a='
    '+t.account.username+' post
    ',n=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?'
    '+t.spoiler_text+'
    '+this.formatTootText(t.content)+"
    ":t.reblog&&""!==t.reblog.content?'
    '+this.formatTootText(t.reblog.content)+"
    ":'
    '+this.formatTootText(t.content)+"
    ";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+="
  • "+t.poll.options[u].title+"
  • ";h='
    "}let p='
    '+n+"
    ",g='
    '+o+a+i+l+h+p+"
    ";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,"

    >","

    ","

    ","

    ")),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 r=RegExp(e+"(.*?)"+o,"gi");return t.replace(r,a+"$1"+i)}},MastodonApi.prototype.replaceMedias=function(t,e){let o=e||!1;return'
    '+(o?'':"")+'
    '},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)};