Last active
March 23, 2018 20:23
-
-
Save javan/069766058af90bd37a03d1d36706c2c3 to your computer and use it in GitHub Desktop.
This file contains 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
(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