@knighttower/utility
Version:
UtilityJs is a utility library that provides a collection of utility functions for various tasks. The library is designed to be easy to use and covers the most common use cases.
1 lines • 1.94 kB
JavaScript
var e,t;e=this,t=()=>(()=>{"use strict";var e={d:(t,o)=>{for(var n in o){e.o(o,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:o[n]})}},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{elementHelper:()=>s});const o={};let n;const r=()=>{if("undefined"!=typeof window&&!n){const e={childList:!0,subtree:!0};if(n=new MutationObserver(e=>{for(const t of e){if("childList"===t.type){for(const e in o){o[e]()}}}}),document.body){return n.observe(document.body,e)}document.addEventListener("DOMContentLoaded",t=>n.observe(document.body,e))}},l=(e,t)=>{t&&(o[e]=t)},d=e=>{e&&delete o[e]};class i{constructor(e,t=document){r(),this.selector=e,"object"==typeof e?this.domElement=e:String(e).includes("//")?this.domElement=this.getElementByXpath(e):this.domElement=t.querySelector(e)}isInDom(){return Boolean(this.domElement?.outerHTML)}whenInDom(){let e=this,t=Date.now()+Math.floor(1e3*Math.random());return new Promise(function(o){e.isInDom()?o(e):l(t,()=>{let n=new i(e.selector);n.isInDom()&&(e=n,o(e),d(t))})})}getElementByXpath(e){return document.evaluate(e,document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}getXpathTo(){let e=this.domElement;if(e.id){return`//*[@id='${e.id}']`}if(e===document.body){return"//"+e.tagName}let t=0,o=e.parentNode.childNodes;for(let n=0;n<o.length;n++){let r=o[n];if(r===e){return new i(e.parentNode).getXpathTo()+"/"+e.tagName+"["+(t+1)+"]"}1===r.nodeType&&r.tagName===e.tagName&&t++}}getAttribute(e){return this.domElement.getAttribute(e)||null}getHash(){let e=String(this.getXpathTo()),t=0;if(0===e.length){return t}for(let o=0;o<e.length;o++){t=(t<<5)-t+e.charCodeAt(o),t&=t}return t}}const s=(e,t=document)=>new i(e,t);return t=t.elementHelper})(),"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("elementHelper",[],t):"object"==typeof exports?exports.elementHelper=t():e.elementHelper=t();