|
| 1 | +// Copyright © 2025–2026 Adam Lui (https://github.com/adamlui) under the MIT license |
| 2 | +// Source: https://github.com/adamlui/ai-web-extensions/blob/main/assets/js/lib/dom.js/dom.js |
| 3 | + |
| 4 | +window.dom = { |
| 5 | + |
| 6 | + create: { |
| 7 | + anchor(linkHref, displayContent, attrs = {}) { |
| 8 | + const anchor = document.createElement('a'), |
| 9 | + defaultAttrs = { href: linkHref, target: '_blank', rel: 'noopener' }, |
| 10 | + finalAttrs = { ...defaultAttrs, ...attrs } |
| 11 | + Object.entries(finalAttrs).forEach(([attr, value]) => anchor.setAttribute(attr, value)) |
| 12 | + if (displayContent) anchor.append(displayContent) |
| 13 | + return anchor |
| 14 | + }, |
| 15 | + |
| 16 | + elem(elemType, attrs = {}) { |
| 17 | + const elem = document.createElement(elemType) |
| 18 | + for (const attr in attrs) { |
| 19 | + if (attr in elem) elem[attr] = attrs[attr] |
| 20 | + else elem.setAttribute(attr, attrs[attr]) |
| 21 | + } |
| 22 | + return elem |
| 23 | + }, |
| 24 | + |
| 25 | + style(content, attrs = {}) { |
| 26 | + const style = document.createElement('style') |
| 27 | + style.setAttribute('type', 'text/css') // support older browsers |
| 28 | + for (const attr in attrs) style.setAttribute(attr, attrs[attr]) |
| 29 | + if (content) style.textContent = content |
| 30 | + return style |
| 31 | + }, |
| 32 | + |
| 33 | + svgElem(type, attrs = {}) { |
| 34 | + const elem = document.createElementNS('http://www.w3.org/2000/svg', type) |
| 35 | + for (const attr in attrs) elem.setAttributeNS(null, attr, attrs[attr]) |
| 36 | + return elem |
| 37 | + } |
| 38 | + }, |
| 39 | + |
| 40 | + get: { |
| 41 | + |
| 42 | + computedSize(elems, { dimension } = {}) { // total width/height of elems (including margins) |
| 43 | + // * Returns { width: totalWidth, height: totalHeight } if no dimension passed |
| 44 | + // * Returns float if { dimension: 'width' | 'height' } passed |
| 45 | + |
| 46 | + // Validate args |
| 47 | + elems = elems instanceof NodeList ? [...elems] : [].concat(elems) |
| 48 | + elems.forEach(elem => { if (!(elem instanceof Node)) |
| 49 | + throw new Error(`Invalid elem: Element "${JSON.stringify(elem)}" is not a valid DOM node`) }) |
| 50 | + const validDimensions = ['width', 'height'], dimensionsToCompute = [].concat(dimension || validDimensions) |
| 51 | + dimensionsToCompute.forEach(dimension => { if (!validDimensions.includes(dimension)) |
| 52 | + throw new Error('Invalid dimension: Use \'width\' or \'height\'') }) |
| 53 | + |
| 54 | + // Compute dimensions |
| 55 | + const computedDimensions = { width: 0, height: 0 } |
| 56 | + elems.forEach(elem => { |
| 57 | + const elemStyle = getComputedStyle(elem) ; if (elemStyle.display == 'none') return |
| 58 | + Object.keys(computedDimensions).forEach(dimension => { |
| 59 | + if (dimensionsToCompute.includes(dimension)) |
| 60 | + computedDimensions[dimension] += elem.getBoundingClientRect()[dimension] |
| 61 | + + parseFloat(elemStyle[`margin${dimension == 'width' ? 'Left' : 'Top'}`]) |
| 62 | + + parseFloat(elemStyle[`margin${dimension == 'width' ? 'Right' : 'Bottom'}`]) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + // Return computed dimensions |
| 67 | + return dimensionsToCompute.length > 1 ? computedDimensions // obj w/ width/height |
| 68 | + : computedDimensions[dimensionsToCompute[0]] // single total val |
| 69 | + }, |
| 70 | + |
| 71 | + computedHeight(elems) { return this.computedSize(elems, { dimension: 'height' }) }, // including margins |
| 72 | + computedWidth(elems) { return this.computedSize(elems, { dimension: 'width' }) }, // including margins |
| 73 | + |
| 74 | + loadedElem(selector, { timeout = null } = {}) { |
| 75 | + const raceEntries = [ |
| 76 | + new Promise(resolve => { // when elem loads |
| 77 | + const elem = document.querySelector(selector) |
| 78 | + if (elem) resolve(elem) |
| 79 | + else new MutationObserver((_, obs) => { |
| 80 | + const elem = document.querySelector(selector) |
| 81 | + if (elem) { obs.disconnect() ; resolve(elem) } |
| 82 | + }).observe(document.documentElement, { childList: true, subtree: true }) |
| 83 | + }) |
| 84 | + ] |
| 85 | + if (timeout) raceEntries.push(new Promise(resolve => setTimeout(() => resolve(null), timeout))) |
| 86 | + return Promise.race(raceEntries) |
| 87 | + } |
| 88 | + } |
| 89 | +}; |
0 commit comments