dev-stamp
Version:
💮 Stamp your dev footprint into HTML
4 lines • 1.82 kB
JavaScript
var g={innerDisplay:"inline"},l={name:"dev-stamp",strictValidation:!0,overwrite:!0},m={mode:"comment",targetSelector:"body",comment:g,metaTag:l};function p(){if(typeof window=="undefined"||typeof window.document=="undefined")throw new Error("This function can only be run in a browser environment.")}function s(t){let e=window.document.querySelector(t);if(!e)throw new Error(`Target element not found: ${t}`);return e}function S(t){return{...m.metaTag,...t.metaTag}}function T(t){return{...m.comment,...t.comment}}function c(t){return{...m,...t,comment:T(t),metaTag:S(t)}}function f(t,e){let{innerDisplay:n}=e;return n==="block"?`
${t}
`:n==="spaced-inline"?` ${t} `:t}function d(t,e){let{targetSelector:n,comment:o}=e,a=s(n),r=f(t,o),i=window.document.createComment(r);a.appendChild(i)}function u(t,e,n){let{overwrite:o}=n;if(!o)throw new Error(`Meta tag with name "${n.name}" already exists. Use "metaTag.overwrite" option to replace it.`);e.setAttribute("content",t)}function w(t,e){if(!e.strictValidation)return;if(!t)throw new Error("Meta tag name cannot be empty. Set 'metaTag.strictValidation' to false to skip this check.");if(!/^[a-zA-Z0-9_\-:.]+$/u.test(t))throw new Error(`Invalid meta tag name: "${t}". Only alphanumeric characters, underscores, hyphens, colons, and periods are allowed. Set 'metaTag.strictValidation' to false to skip this check.`)}function O(t,e){let{head:n}=window.document,{name:o}=e.metaTag,a=o.trim();w(a,e.metaTag);let r=n.querySelector(`meta[name="${a}"]`);if(r){u(t,r,e.metaTag);return}let i=window.document.createElement("meta");i.setAttribute("name",a),i.setAttribute("content",t),n.appendChild(i)}function I(t,e=m){let n=c(e),o={comment:()=>d(t,n),"meta-tag":()=>O(t,n)};try{p(),o[n.mode]()}catch(a){console.error(a)}}export{I as stampInHtml};
//# sourceMappingURL=index.js.map