1

The doc here http://archive.is/m7For#selection-5667.63-5669.3 says:

In the case of "document_start", the files are injected after any files from css, but before any other DOM is constructed or any other script is run. 

However in a comment here I saw:

I'v realized now. That "CSS" does not refer to <style> and <link rel> but refers only to CSS injected by manifest

I'm confused about this .. Is it OK to modify the CSS of the page I inject into in document_start ?

6
  • 1
    What do you mean? document_start runs script before the page DOM is loaded. Commented Aug 27, 2017 at 2:36
  • "the files are injected after any files from css" .. is that the css of the page in which case they can be modified ... or the css of the injection script ? Commented Aug 27, 2017 at 3:23
  • You're concerned about one person that appears to not have really known how to do what they were trying to do (multiple statements in the question clearly indicate they don't really understand) and those problems went away once they reinstalled Chrome? Why are you concerned? Go with the documentation. Write the code. If you have problems, ask a new question. Commented Aug 27, 2017 at 3:23
  • The CSS files in the css entry in the content_scripts Object contained in the manifest.json. The manifest.json content_scripts js and css are injected prior to anything from the page, including the HTML, when they are directed to be injected at document_start. That's why document.head and document.body are both null at that time (which is why you have to append to document.documentElement, if you want to add something to the DOM at that point). Commented Aug 27, 2017 at 3:26
  • Yes, if you use JavaScript to add a <link> element to the DOM which contains a URL to a CSS file (including to a file in your extension), that file may be loaded after some other resource(s) in the page are loaded. It's not guaranteed to be loaded first, as you start getting into indeterminate asynchronous delays. However, the files in your manifest.json content_scripts css entry are guaranteed to load first. Commented Aug 27, 2017 at 3:35

1 Answer 1

1

The comment is correct and it is not OK to modify the CSS of the page you inject into in document_start.

When a script is injected with run_at = document_start it can modify only the CSS it itself injected. It does not have access to the DOM including CSS until some point later (probably after the head is created).

However you can modify the CSS of the page before it is shown by using an observer like this:

const convertCSS = () => {
    if (!convertCSS.nSheets) convertCSS.nSheets=0;
    if (convertCSS.nSheets===window.document.styleSheets.length) return;
    convertCSS.nSheets=window.document.styleSheets.length;

    for (const styleSheet of window.document.styleSheets) {
        const classes = styleSheet.rules || styleSheet.cssRules;
        if (!classes) continue;

        for (const cssRule of classes) {
            if (cssRule.type !== 1 || !cssRule.style) continue;
            const selector = cssRule.selectorText, style=cssRule.style;
            if (!selector || !style.cssText) continue;
            for (let i=0; i<style.length; i++) {
                const propertyName=style.item(i), propertyValue=style.getPropertyValue(propertyName);
                // YOUR LOGIC HERE ie:
                // if (propertyName==='background-color') cssRule.style.setProperty(propertyName, 'yellow',  style.getPropertyPriority(propertyName));
            }
        }
    }
}


const observer =new MutationObserver((mutations, observer) => convertCSS());
observer.observe(document, { childList: true, subtree:true });

If you don't need to modify the CSS on new elements once the page is loaded add :

document.addEventListener("DOMContentLoaded", e => observer.disconnect());

Also you probably want "all_frames": true in your manifest.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.