Last active
May 20, 2025 01:35
-
-
Save rushkeldon/959ed97c8afc104c9b8a5896d62e7554 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Ephemeral Element Capture & Injection | |
* | |
* Usage: | |
* ephemera.startObserving(); | |
* ephemera.trace(); // to see the captured HTML in the console | |
* ephemera.injectElement(); | |
*/ | |
const ephemera = { | |
// --- config --- | |
appMountPointID: 'appMountPoint', | |
injectPointSelector: '[data-uia="loc"] > div', | |
targetSelector: '.previewModal--wrapper', | |
// --- state --- | |
get appMountPoint() { | |
return document.getElementById(ephemera.appMountPointID); | |
}, | |
elementOuterHTML: null, | |
observer: null, | |
// --- API --- | |
startObserving() { | |
if (ephemera.observer) ephemera.observer.disconnect(); | |
ephemera.observer = new MutationObserver(() => { | |
const el = ephemera.appMountPoint?.querySelector(ephemera.targetSelector); | |
if (el) ephemera.elementOuterHTML = el.outerHTML; | |
}); | |
ephemera.observer.observe(ephemera.appMountPoint, { childList: true, subtree: true }); | |
}, | |
stopObserving() { | |
ephemera.observer?.disconnect(); | |
}, | |
trace() { | |
console.log(`elementOuterHTML:\n${ephemera.elementOuterHTML}`); | |
}, | |
injectElement() { | |
if (!ephemera.elementOuterHTML) return console.warn('injectElement: no captured HTML'); | |
const temp = document.createElement('div'); | |
temp.innerHTML = ephemera.elementOuterHTML; | |
const node = temp.firstElementChild; | |
const container = ephemera.appMountPoint?.querySelector(ephemera.injectPointSelector); | |
if (!container) return console.warn(`injectElement: container not found using ${ephemera.injectPointSelector}`); | |
container.appendChild(node); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment