Prior methods tend to mess up the styles when they are altered before being expanded. This often results in a plethora of default styles being applied.
On the other hand, my approach focuses on stripping away the default styles while preserving the cascading styles across various elements.
To utilize this solution, simply execute the script in the console and copy the desired element from the Elements view (verified on Chrome).
function updateStyle(element) {
var children = element.children;
for(var i = 0; i < children.length; i++) {
updateStyle(children[i]);
var defaultElement = document.createElement(children[i].nodeName);
var child = document.body.appendChild(defaultElement);
var defaultStyles = window.getComputedStyle(defaultElement, null);
var computedStyles = window.getComputedStyle(children[i], null).cssText;
for(var j = 0; j < defaultStyles.length; j++) {
var defaultProperty = defaultStyles[j] + ": " + defaultStyles.getPropertyValue(defaultStyles[j]) + ";";
if(computedStyles.startsWith(defaultProperty)) {
computedStyles = computedStyles.substring(defaultProperty.length);
} else {
computedStyles = computedStyles.replace(" " + defaultProperty, "");
}
}
child.remove();
children[i].setAttribute("style", computedStyles);
}
}
updateStyle(document.getElementsByTagName("body")[0]);
console.log("DONE");