-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-tag.min.js
5 lines (5 loc) · 2.34 KB
/
json-tag.min.js
1
2
3
4
5
/**!
* @license JSON-Tag - A JavaScript library to easily render data from JSON locally or from a API / URL on your website inside of a <json> tag.
* VERSION: 1.0.0
* LICENSE & MORE INFO CAN BE FOUND AT https://github.com/MarketingPipeline/JSON-Tag/
*/ import Hogan from"https://cdn.skypack.dev/hogan.js@3.0.2";function renderJSONTags(){let JSON_Tag;function handleError(e,t){e.hasAttribute("error-message")?e.innerHTML=e.getAttribute("error-message"):e.innerHTML=`JSON-Tag Error: ${t.message}`,console.error(`JSON Tag: Error - ${t.message}`),setAttribute(e,"error")}function setAttribute(e,t){t?e.setAttribute("json-error",""):e.setAttribute("json-rendered","")}async function fetchJSON(e){let t=await fetch(e),n=await t.json();return{json:n}}async function fetchJSON_From_URL(e){try{let t=await fetchJSON(e.getAttribute("fetch-json")),n=Hogan.compile(e.innerHTML).render(t);e.innerHTML=n}catch(r){handleError(e,r)}}document.querySelectorAll("json").forEach(tag=>{try{let template=Hogan.compile(tag.innerHTML);if(tag.hasAttribute("local-json")){let output=template.render(eval(tag.getAttribute("local-json")));tag.innerHTML=output}else tag.hasAttribute("fetch-json")&&fetchJSON_From_URL(tag);setAttribute(tag)}catch(error){handleError(tag,error)}})}!function(e){let t=+new Date,n=100,r=[];function o(){let e=+new Date;if(e-t>n){for(let o=0;o<r.length;o++)r[o]();t=e}}let i=function(e,t){t&&(n=t),r.push(e)};function d(){let e=document.getElementsByTagName("*"),t=e.length;setTimeout(function r(){let i=document.getElementsByTagName("*"),d=i.length;d!=t&&(e=[]);for(let a=0;a<d;a++)if(i[a]!==e[a]){o(),e=i,t=d;break}setTimeout(r,n)},n)}let a={},s=document.documentElement,c=3;function l(){a.DOMNodeInserted?e.addEventListener("DOMContentLoaded",function(){a.DOMSubtreeModified?s.addEventListener("DOMSubtreeModified",o,!1):(s.addEventListener("DOMNodeInserted",o,!1),s.addEventListener("DOMNodeRemoved",o,!1))},!1):document.onpropertychange?document.onpropertychange=o:d()}function u(e){s.addEventListener(e,function t(){a[e]=!0,s.removeEventListener(e,t,!1),0==--c&&l()},!1)}e.addEventListener?(u("DOMSubtreeModified"),u("DOMNodeInserted"),u("DOMNodeRemoved")):l();let f=document.createElement("div");s.appendChild(f),s.removeChild(f),e.onDomChange=i}(window),onDomChange(function(){setTimeout(()=>{renderJSONTags()},"1")}),document.addEventListener("DOMContentLoaded",()=>renderJSONTags());