Hi,
I have the following code (more or less) in a Firefox extension:
var styles = `
.NEAR {
display:block;
}
`
window.styleSheet = document.createElement("style")
styleSheet.textContent = styles
document.head.appendChild(styleSheet)
const element = document.head.querySelector('.NEAR');
But element
is always null
…
I’ve also tried adding the style section to document.body
and directly to the html file, with the same effect.
The quirk is that I’m reading a Firefox bookmark html export file (<!DOCTYPE NETSCAPE-Bookmark-file-1>
), so it’s processed in quirk mode.
I haven’t explored this behaviour with a well formed HTML file yet.
The value of the document.head.style
attribute in the DOM is CSS2Properties(0)
and isn’t expandable, so presumably the list is empty, as per the source file. However, the dynamically added styles are working when the document is rendered, so they must be somewhere.
I want to modify the style for the class at runtime, but I’ve resorted to manipulating the classList
of elements directly, which is a royal pain. Another option (which I haven’t tried yet) is to remove the whole style
element and recreate it with changes, but it would be much simpler to just modify the element.style
attribute.
Any explanation would be appreciated, as would a link to a tutorial that explains how these structures (sources, elements, nodes, DOM, etc) are related and how they interact.
Cheers,
bitrat