I have an addon that changes the look of a website completely by injecting a CSS file. It works but for a split second before the new look appear the old look is there, thus causing the "flicker" each time a page loads. The new look changes the background color etc. so the switch is very noticeable. (I only notice this on my desktop, not on my laptop. I don't know why but other users report it as well, perhaps the faster computer makes the page show faster than it takes to get the CSS injected?). The CSS injection need to happen so the injected CSS is the most important (as in last).
What I've tried that cause this issue (code afterwards):
- Manifest: Made sure css is in web_accessible_resources
- Manifest: Doing the injection directly in content_scripts
- Manifest: Doing it through a javascrip run from the content_scripts
- Manifest: Making sure all content-scripts are run at document_start
- Manifest: Tried running the injection from a script running on the background page
- JS Injection Timing: Added eventlistener for DOMSubtreeModified
- JS injection Timing: Added eventlistener for chrome.webNavigation.onCommitted
- JS Injection Timing: Waiting for document.head / document.body
- JS Injection Method: appendChild
- JS Injection Method: chrome.tabs.executeScript()
- JS Injection Code: Link element linking to css file in extension
- JS Injection Code: Directly executed javascript
Code Examples:
Manifest:
{
"manifest_version": 2,
"name": "foo",
"short_name": "bar",
"description": "baz",
"options_page": "options.html",
"version": "2.1.1",
"homepage_url": "http://google.com/",
"permissions": ["storage", "*://google.com/*", "webNavigation", "tabs", "activeTab"],
"browser_action": {
"default_icon": "icon16.png",
"default_title": "title",
"default_popup": "popup.html"
},
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"content_scripts": [
{
"matches": ["*://google.com/*"],
"js": ["carbonicEditionScript.js"],
"all_frames": true,
"run_at": "document_start"
}
],
"background": {
"page": "popup.html"
},
"web_accessible_resources": ["carbonicEditionStyle.css"]
}
carbonicEditionScript.js
document.addEventListener('DOMSubtreeModified', injectStyle, false);
function injectStyle(){
document.removeEventListener('DOMSubtreeModified', injectStyle, false);
var style = document.createElement('style');
style.setAttribute("id", "CarbonicEditionStyle");
style.setAttribute("class", "CarbonicEditionStyle");
style.setAttribute("type", "text/css");
style.appendChild(document.createTextNode(css));
document.getElementsByTagName("html")[0].appendChild(style);
}
carbonicEditionScriptAlternative.js
document.addEventListener('DOMSubtreeModified', injectCSS, false);
function injectCSS(){
var style = document.createElement('link');
style.rel = 'stylesheet';
style.type = 'text/css';
style.href = chrome.extension.getURL('carbonicEditionStyle.css');
if(document.head){
document.removeEventListener('DOMSubtreeModified', injectCSS, false);
(document.head||document.documentElement).appendChild(style);
}}
background.js
chrome.webNavigation.onCommitted.addListener(function(o) {
chrome.tabs.executeScript(o.tabId, {
code: "var css = 'body{background-color: green !important;}'; var style = document.createElement('style'); style.setAttribute('id', 'CarbonicEditionStyle'); style.setAttribute('class', 'CarbonicEditionStyle'); style.setAttribute('type', 'text/css'); style.appendChild(document.createTextNode(css)); document.getElementsByTagName('html')[0].appendChild(style);"
});
}, {
url: [{hostContains: 'google.com'}]
});
Does anyone know what's going on? All the solutions above work but the flicker is still happening.