Skip to content

Instantly share code, notes, and snippets.

@sorvell
Created September 16, 2013 16:58
Show Gist options
  • Save sorvell/6583385 to your computer and use it in GitHub Desktop.
Save sorvell/6583385 to your computer and use it in GitHub Desktop.
ShadowDOM + MutationObserver polyfill
<!DOCTYPE html>
<html>
<head>
<script src="../../../ShadowDOM/shadowdom.js"></script>
<script src="../../../MutationObservers/MutationObserver.js"></script>
</head>
<body>
<x-host></x-host>
<script>
function makeSR(scope, selector, html) {
var node = scope.querySelector(selector);
if (node) {
node.createShadowRoot().innerHTML = html;
return node.shadowRoot;
}
}
var root = makeSR(document, 'x-host', '<x-inner-host>inner</x-inner-host>');
requestAnimationFrame(function() {
window.MutationObserver = window.MutationObserver || JsMutationObserver;
var o = new MutationObserver(function(mutations) {
console.log('mutation!');
});
o.observe(document, {childList: true, subtree: true});
//
makeSR(root, 'x-inner-host', '<x-inner-inner-host>inner inner</x-inner-inner-host>');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment