Skip to content

Instantly share code, notes, and snippets.

@javan
Last active March 23, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javan/069766058af90bd37a03d1d36706c2c3 to your computer and use it in GitHub Desktop.
Save javan/069766058af90bd37a03d1d36706c2c3 to your computer and use it in GitHub Desktop.
(async () => {
if (await mutionObserverIsBuggy()) {
patchInnerHTML()
}
})()
function mutionObserverIsBuggy(): Promise<boolean> {
return new Promise(resolve => {
const element = document.createElement("div")
element.innerHTML = "<a><em></em></a>"
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
if (mutation.type == "childList") {
const node = mutation.removedNodes[0]
const isBuggy = node.childNodes.length == 0
observer.disconnect()
return resolve(isBuggy)
}
}
})
observer.observe(element, { childList: true, subtree: true })
element.innerHTML = ""
})
}
function patchInnerHTML() {
const { prototype } = HTMLElement
const descriptor = Object.getOwnPropertyDescriptor(prototype, "innerHTML")
if (descriptor) {
Object.defineProperty(prototype, "innerHTML", {
get() {
if (typeof descriptor.get == "function") {
return descriptor.get.call(this)
}
},
set(value) {
while (this.lastChild) {
this.removeChild(this.lastChild)
}
if (typeof descriptor.set == "function") {
return descriptor.set.call(this, value)
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment