Skip to content

Instantly share code, notes, and snippets.

@14paxton
Last active June 8, 2022 18:05
Show Gist options
  • Save 14paxton/70018ca1b4b990db4fbf4edfd1907af8 to your computer and use it in GitHub Desktop.
Save 14paxton/70018ca1b4b990db4fbf4edfd1907af8 to your computer and use it in GitHub Desktop.
function dumpCSSText(element) {
if (!element?.id) {
element.id = `${element.tagName}-${Math.floor(Math.random() * 7777)}`
}
let promises = []
let o = getComputedStyle(element);
promises.push(new Promise(function (res, rej) {
let s = `#${element.id}{ `;
for (let i = 0; i < o.length; i++) {
let html = ''
s += o[i] + `: ` + o.getPropertyValue(o[i]) + '; ';
}
s += ` } `
res(s)
}));
const children = element?.children
if (children) {
for (let child of children) {
promises.push.apply(promises, dumpCSSText(child))
}
}
return promises;
}
export const generateRulesAll = async (element) => {
let promises = [];
promises = dumpCSSText(element)
let cssString = ''
await Promise.all(promises).then(result => {
result.forEach(function (css) {
cssString += css
});
})
return cssString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment