Vloxxity
Vloxxity

Reputation: 980

Chrome extension too slow

ive got a small script im using at Firefox and Chrome, it just changes the color of this page. in Firefox it just works fine, but in Chrome i can see the original color of the page first and seconds after that it changes.

Is that normal? (How) can i change that?

Upvotes: 0

Views: 713

Answers (1)

PAEz
PAEz

Reputation: 8542

Ive got no idea how to do it with a GM script, but here's how to do it as a chrome extension....

manifest.json

{
    "name": "SO css",
    "content_scripts": [
        {
         "matches": [
            "http://*.stackoverflow.com/*"
         ],
         "css": ["new.css"],
         "run_at" : "document_start"
        }
    ],
    "version":"1.0",
    "manifest_version" : 2
}

new.css

#custom-header       {background-color: rgb(251,122,35) !important}

#nav-questions       {background-color: rgb(251,122,35) !important}
#nav-tags            {background-color: rgb(251,122,35) !important}
#nav-users           {background-color: rgb(251,122,35) !important}
#nav-badges          {background-color: rgb(251,122,35) !important}
#nav-unanswered      {background-color: rgb(251,122,35) !important}
#nav-askquestion     {background-color: rgb(251,122,35) !important}

Info on content scripts....
http://developer.chrome.com/extensions/content_scripts.html
Info on Chrome extensions....
http://developer.chrome.com/extensions/getstarted.html

If GM scripts have a run_at equivalent then it needs to be document_start as its sounds like the css is getting injected at document_idle which will be after the dom/page is loaded. You want it before that so you dont see it change. Thats why I added !important to every css rule in my answer, to make sure their not changed by any css that might come after yours.

EDIT
Looked it up and there is a run_at variable, here's the same as above but as a GM script....

// ==UserScript==
// @name        SO
// @namespace   stackoverflow.com
// @include     *stackoverflow.com/*
// @version     1
// @grant       GM_addStyle
// @run-at      document-start
// ==/UserScript==

changeHeaderColor ();

function changeHeaderColor () {
    GM_addStyle ( "                                                 \
        /*body { color: white; background-color: black !important}            \
        */                                                          \
        #custom-header       {background-color: rgb(251,122,35) !important}    \
                                                                    \
        #nav-questions       {background-color: rgb(251,122,35) !important}    \
        #nav-tags            {background-color: rgb(251,122,35) !important}    \
        #nav-users           {background-color: rgb(251,122,35) !important}    \
        #nav-badges          {background-color: rgb(251,122,35) !important}    \
        #nav-unanswered      {background-color: rgb(251,122,35) !important}    \
        #nav-askquestion     {background-color: rgb(251,122,35) !important}    \
        /*Blau: rgb(0,160,160) rgb(0,200,200)                       \
        */                                                          \
    " );
}

Upvotes: 3

Related Questions