I\'d like to be able to take my DOM, as is, and convert it to a string. Let\'s say I open up the inspector and make a change to the margin-left property of a particular ele
In case you want to capture the whole page, it is easier to just get all non-inline stylesheets and inline them.
The approach in the accepted answer is magnificent, but quite slow and touches the whole document.
I took the following approach to capture a page including style:
document.documentElement.outerHTML;
get all stylesheets from the document.styleSheets API
Along the lines of:
function captureCss(){
var cssrules = "";
var sheets = document.styleSheets;
for(var i = 0; i\n"+fetched+"\n\n"
}
}catch(e){
console.log(e);
}
continue;
}
for(var j=0;j\n"+sheets[i].rules[j].cssText+"\n\n"
}
}
}
return cssrules;
}
cssrules as the first thing of the header in the outerHtml html textThis way you get a self contained styled page.
This is obviously less applicable for partial content.