Skip to content

Instantly share code, notes, and snippets.

@developit
Last active April 26, 2024 14:08
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save developit/54f3e3d1ce9ed0e5a171044edcd0784f to your computer and use it in GitHub Desktop.
Save developit/54f3e3d1ce9ed0e5a171044edcd0784f to your computer and use it in GitHub Desktop.
if (!Element.prototype.getInnerHTML) {
Element.prototype.getInnerHTML = function(opts) {
var html = this.innerHTML;
if (!opts || !opts.includeShadowRoots) return html;
var m = new (self.WeakMap || Map)();
for (var c of (opts.closedRoots || [])) m.set(c.host, c);
var p = [];
function walk(node) {
var c, shadow = node.shadowRoot || m.get(node);
if (shadow) p.push(node.innerHTML, `<template shadowroot="${shadow.mode}">${shadow.innerHTML}</template>`);
var c = node.firstElementChild;
while (c) { walk(c); c = c.nextElementSibling; }
}
walk(this);
var out = '', c = 0, i = 0, o;
for (; c<p.length; c+=2) {
o = html.indexOf(p[c], i);
if (o < 0) continue;
out += html.substring(i, o) + p[c+1];
i = o;
}
return out + html.substring(i);
};
}
@LewisW
Copy link

LewisW commented Jul 11, 2023

Would you mind including a license with this? I'd like to include a modified version of it (with attribution) in an MIT licensed tool. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment