check-if-css-is-disabled
Version:
☂️ Frontend JavaScript module that can determine if CSS is disabled or if it failed to load, then take action to stop JS enhancements from applying if the CSS isn't working first.
2 lines • 1.89 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("checkIfCssIsDisabled",[],t):"object"==typeof exports?exports.checkIfCssIsDisabled=t():e.checkIfCssIsDisabled=t()}(this,(()=>{return e={597:e=>{e.exports=e=>{const t=!!e?.justCheck,o=e?.exemptedIds||[];function r(e){if(e&&(window.linkTagError=!0),!t&&window.linkTagError){for(const e of document.querySelectorAll('link[rel="stylesheet"]'))o.includes(e.id)||e.remove();for(const e of document.querySelectorAll("style"))o.includes(e.id)||e.remove()}return window.linkTagError?(window.dispatchEvent(new CustomEvent("cssDisabled",{detail:{message:"At least one stylesheet failed to load. It is unsafe to execute any further JavaScript if the CSS has not loaded properly."}})),!0):!!e}document.querySelectorAll('link[rel="stylesheet"]').forEach((e=>{e.addEventListener("error",r)})),new window.MutationObserver((e=>{for(const t of e)"childList"===t.type&&t.addedNodes.forEach((e=>{"LINK"===e.tagName&&"stylesheet"===e.rel&&e.addEventListener("error",r)}))})).observe(document.head,{childList:!0,subtree:!0});let s=!1;const n=document.createElement("div");if(n.style.position="absolute",document.body.appendChild(n),s="static"===document.defaultView.getComputedStyle(n,null).getPropertyValue("position"),document.body.removeChild(n),t)return s?(window.dispatchEvent(new CustomEvent("cssDisabled",{detail:{message:"CSS is disabled. It is unsafe to execute any further JavaScript if the CSS has not loaded properly."}})),!0):r();if(s||r())throw new Error("CSS is disabled. It is unsafe to execute any further JavaScript if the CSS has not loaded properly.");return!1}}},t={},function o(r){var s=t[r];if(void 0!==s)return s.exports;var n=t[r]={exports:{}};return e[r](n,n.exports,o),n.exports}(597);var e,t}));
//# sourceMappingURL=check-if-css-is-disabled.min.js.map