Got it working using getComputedStyle
. It does not support ::after
but that shouldn't be too hard to add.
let text = '';
for (const child of children(document.querySelector('div'))) {
if (child.nodeType === 1) {
const before = window.getComputedStyle(child, '::before').getPropertyValue('content');
if (before && before !== 'none') {
text += before.replace(/^['"]/, '').replace(/['"]$/, '');
}
} else if (child.nodeType === 3) {
text += child.textContent;
}
}
alert(text);
function children(node) {
const ret = [];
for (let i = 0; i < node.childNodes.length; i++) {
const child = node.childNodes[i];
ret.push(child, ...children(child));
}
return ret;
}