-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom-utils.js
More file actions
1 lines (1 loc) · 2.88 KB
/
Copy pathdom-utils.js
File metadata and controls
1 lines (1 loc) · 2.88 KB
1
function serializeDOM(){const e=document.body.cloneNode(!0);["script","style","iframe","noscript",'[aria-hidden="true"]',".ad",".advertisement",'[class*="cookie"]','[class*="banner"]'].forEach(t=>{e.querySelectorAll(t).forEach(e=>e.remove())});return extractStructure(e)}function extractStructure(e){const t=[],n=["button","a","input","select","textarea","h1","h2","h3","form"];return function e(o,i=0){if(i>10)return;const a=o.tagName?.toLowerCase();if(a){if(n.includes(a)){const e=o.textContent?.trim().slice(0,200)||"",n=o.id||"",i=Array.from(o.classList).join(" "),r=o.type||"",s=o.href||"";(e||n||r)&&t.push({tag:a,text:e,id:n,classes:i,type:r,href:s,visible:isVisible(o),xpath:getXPath(o)})}Array.from(o.children).forEach(t=>e(t,i+1))}}(e),t}function isVisible(e){if(!(e&&e instanceof Element))return!1;const t=window.getComputedStyle(e);return"none"!==t.display&&"hidden"!==t.visibility&&"0"!==t.opacity&&e.offsetWidth>0&&e.offsetHeight>0}function getXPath(e){if(!e)return"";if(e.id)return`//*[@id="${e.id}"]`;if(e===document.body)return"/html/body";if(!e.parentNode)return e.tagName?"/"+e.tagName.toLowerCase():"";let t=0;const n=e.parentNode.childNodes||[];for(let o=0;o<n.length;o++){const i=n[o];if(i===e)return getXPath(e.parentNode)+"/"+e.tagName.toLowerCase()+"["+(t+1)+"]";1===i.nodeType&&i.tagName===e.tagName&&t++}return""}function getElementByXPath(e){return document.evaluate(e,document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}function findTextNodes(e,t=document.body){const n=[],o=document.createTreeWalker(t,NodeFilter.SHOW_TEXT,{acceptNode:t=>t.textContent?.toLowerCase().includes(e.toLowerCase())?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP});let i;for(;i=o.nextNode();)n.push(i);return n}function detectAnimations(){const e=[];return document.querySelectorAll("*").forEach(t=>{const n=window.getComputedStyle(t),o="none"!==n.animationName&&""!==n.animationName;"all 0s ease 0s"!==n.transition&&n.transition;if(o){const o=parseFloat(n.animationDuration),i=n.animationIterationCount;(o<2||"infinite"===i)&&e.push({element:t,type:"animation",animation:n.animationName,duration:o})}"VIDEO"!==t.tagName&&"IFRAME"!==t.tagName||e.push({element:t,type:"media"})}),e}function injectCSS(e,t="easyview-styles"){let n=document.getElementById(t);n||(n=document.createElement("style"),n.id=t,document.head.appendChild(n)),n.textContent=e}function removeCSS(e="easyview-styles"){const t=document.getElementById(e);t&&t.remove()}function wrapTextNode(e,t="ev-jargon"){const n=document.createElement("span");return n.className=t,n.textContent=e.textContent,e.parentNode.replaceChild(n,e),n}"undefined"!=typeof module&&module.exports&&(module.exports={serializeDOM:serializeDOM,extractStructure:extractStructure,isVisible:isVisible,getXPath:getXPath,getElementByXPath:getElementByXPath,findTextNodes:findTextNodes,detectAnimations:detectAnimations,injectCSS:injectCSS,removeCSS:removeCSS,wrapTextNode:wrapTextNode});