0

So I have been redesigning some websites just for my own usage using chrome extension.

My goal is to inject css into a website so it would be using as a main css or it would replace some main css's codes.

What I have is the fallowing:

JS:

var style = document.createElement('link');
style.rel = 'stylesheet';
style.type = 'text/css';
style.href = chrome.extension.getURL('styles.css');
(document.head||document.documentElement).appendChild(style);

Manifest file:

{
  "name": "Extension",
  "version": "0",
  "description": "",
  "manifest_version": 2,
  "permissions": ["tabs", "https://www.youtube.com/*"],
    "content_scripts": [
    {
        "matches": [ "https://www.youtube.com/*"],
        "js": ["inject.js"],
        "all_frames": true
    }
  ],
  "web_accessible_resources": ["styles.css"]
}

And of course my css file named styles.css

The problem I am having is once I load the page, it loads in an old style and then just before it finished loading my custom css hits the road and then the styles are changed of the website. How can I make so the styles for the website would be changed as soon as it starts loading the content so the changes will not be seen whatsoever.

There is a gif image to show you what I mean by that:

enter image description here

1
  • 1
    Perhaps you could inspect the extension Stylish Commented Feb 5, 2016 at 21:43

1 Answer 1

1

You don't need to do programmatic injection (which happens, by default, at "run_at": "document_idle", and that is exactly your problem).

Just change your manifest to this:

{
  "name": "Extension",
  "version": "0",
  "description": "",
  "manifest_version": 2,
  "permissions": ["tabs", "https://www.youtube.com/*"],
  "content_scripts": [
    {
        "matches": [ "https://www.youtube.com/*"],
        "css": ["styles.css"],
        "all_frames": true
    }
  ]
}

If you still need to do it programmatically, then you need to add "run_at": "document_start"

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

2 Comments

Thank you, I will try your suggestion!
It works, but most of my css code is now ignored for some reason.. Edited: have to add !important in every line where my code is skipped. It works now fine!

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.