UNPKG

@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.72 kB
(()=>{"use strict";var e={d:(t,n)=>{for(var o in n){e.o(n,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:n[o]})}},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{elementHelper:()=>s});const n={};let o;const r=()=>{if("undefined"!=typeof window&&!o){const e={childList:!0,subtree:!0};if(o=new MutationObserver(e=>{for(const t of e){if("childList"===t.type){for(const e in n){n[e]()}}}}),document.body){return o.observe(document.body,e)}document.addEventListener("DOMContentLoaded",t=>o.observe(document.body,e))}},l=(e,t)=>{t&&(n[e]=t)},d=e=>{e&&delete n[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(n){e.isInDom()?n(e):l(t,()=>{let o=new i(e.selector);o.isInDom()&&(e=o,n(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,n=e.parentNode.childNodes;for(let o=0;o<n.length;o++){let r=n[o];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 n=0;n<e.length;n++){t=(t<<5)-t+e.charCodeAt(n),t&=t}return t}}const s=(e,t=document)=>new i(e,t);window.elementHelper=t.elementHelper})();