Injecting CSS styles into a webpage using a Chrome extension before the page has completely loaded to achieve instant customization

As a newcomer to creating Chrome (or other browser) extensions, I am working on developing one that applies custom CSS rules to specific page elements. Overall, it seems to be functioning as intended, but with some minor issues.

One issue I have encountered is a delay between the complete rendering of the page in the browser and when my extension's CSS rules take effect after applying them using JavaScript.

To address this delay, I have discovered that adding the desired CSS file to the manifest file under "content_scripts" can provide instant application, like so:

"content_scripts": [
    {
      "run_at": "document_start",
      "all_frames": true,
      "matches": ["<all_urls>"],
      "js": ["filter.js"],
      "css": ["filter.css"]
    }
   ],

However, I now face the challenge of determining if the user has clicked the 'enable' button on my extension's pop-up before applying the CSS. To achieve this, I check for the enabled flag set to true in the "filter.js" and background scripts using chrome storage.

I then utilize the "chrome.tabs.insertCSS" method to insert my CSS files accordingly.

In cases where the user disables the extension, the browser still displays the effects of "filter.css" until the JS removes them. This results in the user seeing the unwanted effects of "filter.css" before they are removed, which is not ideal.

My goal is to have the browser instantly apply or not apply my styles based on the user's settings before displaying the page content.

All methods I've tried so far to inject CSS have caused delays. It must be possible to add or remove CSS without delay, as demonstrated by extensions like Dark Reader that apply their styles immediately without showing the browser content without their CSS.

While conditional checking in the manifest may not be feasible, I welcome any suggestions on how to overcome this issue.

Any assistance would be greatly appreciated!

Thank you for your attention!

Answer №1

After much troubleshooting, I was able to successfully resolve the issue at hand. Surprisingly, the problem did not lie with the CSS insertion functions themselves, but rather with my timing of executing the code within the window.onload function. Lesson learned! :man_facepalming:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the reasons behind the lack of smooth functionality in the Bootstrap 4 slider?

My customized bootstrap4 slider is functional, but lacks smoothness when clicking on the "next" and "prev" buttons. The slider transitions suddenly instead of smoothly. Any suggestions on how to fix this? Here is the code for the slider: $('.carous ...

What is the best practice for accessing specific components of JSON values that are delimited by colons?

When working with an API that returns a JSON containing strings separated by colons, such as: { "id": "test:something:69874354", "whatever": "maybe" } It may be necessary to extract specific values from these strings. For example, in the given JSON s ...

What is the most efficient way to prevent duplicate items from being added to an array in a Vue 3 shopping cart

I currently have a functional shopping cart system, but I am facing an issue where it creates duplicates in the cart instead of incrementing the quantity. How can I modify it to only increment the item if it already exists in the cart? Also, I would like t ...

The text of the keyword in AdGroupCriterionService remains unchanged

I've been utilizing the GoogleAds Node Module from https://www.npmjs.com/package/googleads-node-lib. I have successfully managed to modify the Tracking Template and the CpcBid microAmount, however, I am facing an issue with changing the keyword text. ...

Is there a way to send JSON using ExpressJS in UTF-8 encoding?

Currently facing an issue with my new web app that I haven't encountered in the past. Experimenting with a simple code snippet like this: var jsonToSend = {hello: "woørld"}; app.get('/someUrl', function(req, res) { res.setHeader('Co ...

Ways to determine if prototype methods vary

Is there a technique to verify if functions are distinct despite originating from the same prototype? I'm inquiring because I want to save functions in an array, and when attempting to delete one, it removes all functions due to sharing prototypes. ...

Is it possible that images appear normal locally, but become stretched once deployed? (CSS/HTML only)

I designed a straightforward webpage using only HTML and CSS. Everything looks great when I view it on my local machine. However, once I deployed the site on Droppages, a static-page hosting service integrated with Dropbox, all my images became stretched ...

Changing the Style Sheets on the Fly

I have designed a grid of 5x5 boxes. My goal is to be able to click on a link and have that specific link change color after it has been clicked (a:visited) - one at a time. Unfortunately, my current code changes the color of all the links instead of just ...

Issue: Animation not triggered on exit by React-transition-group Transition

I am aiming to create a unique animation where an icon spins 90 degrees to become flat and then seamlessly transitions into another icon, simulating a coin-spin effect. The desired effect is for the icons to spin both on enter and exit, although there may ...

Guide on how to retrieve Twitter Username and Profile Photo through the Twit NPM Package

Utilizing the twit npm package to retrieve the Authenticated Twitter Username and Profile Image, here is the code I am using: const Twit = require('twit'); let T = new Twit({ consumer_key: 'xxx', ...

The building process of Ember encountered an error due to a problem with the broccoli builder

I'm currently working on an Ember project and facing an issue while trying to upgrade the version from 2.8 to 3.5.0. After changing the version and some dependencies, I encountered the following error : Error stack Even after attempting to resolve i ...

Struggling to make Vue.js transition effects function properly?

I'm having trouble getting a vue.js (version 1) transition to work. I copied the code from their official website, but it's not running the javascript console.logs! Vue.transition('fade', { css: false, enter: function ( ...

Modify capital letters to dashed format in the ToJSON method in Nest JS

I am working with a method that looks like this: @Entity() export class Picklist extends BaseD2CEntity { @ApiHideProperty() @PrimaryGeneratedColumn() id: number; @Column({ name: 'picklist_name' }) @IsString() @ApiProperty({ type: Str ...

Ways to have the "li" element displayed on a separate line

let selector = document.querySelector('.activate') selector.addEventListener('mouseover', function(){ document.querySelector('#hidden-li').classList.remove("hidden") }) selector.addEventListener('mouseleave', f ...

One CSS file linked, but only one is applied; the other is left unused

After including two CSS files, I am experiencing an issue where only one of them is being utilized. The other file does not seem to be included and the reason behind this remains unknown. My website has a standard header file that is present on all pages. ...

jQuery Ajax error 403 (unlike XMLHttpRequest)

Recently, I encountered an issue with two Ajax calls in my code. One of the calls was implemented using XMLHttpRequest and the other one using jQuery. Surprisingly, the first call completed successfully without any errors. However, the second call, which s ...

Is there a way to adjust my input value to correspond to December of 2022?

I am working on a project where I need to set a default month and year that the user cannot modify. They should only be able to change the date in December months. Any tips on how I can achieve this? <input id="input_date" type="date&qu ...

Increase the number of button groups when clicked

I have a button group with three buttons: left, middle, and right. I want to enhance this functionality so that when any of the main buttons are clicked, the corresponding sub-buttons (left-sub, middle-sub, or right-sub) appear accordingly: <div class= ...

Javascript and Codeigniter interaction: Using conditionals with Ajax

I am having trouble understanding this code snippet. I am currently studying Ajax and came across this piece of code that automatically inserts data. However, I am unsure about the line if(result=='12') then trigger ajax. What does the number 12 ...

Uncover the solution to eliminating webpack warnings associated with incorporating the winston logger by utilizing the ContextReplacementPlugin

When running webpack on a project that includes the winston package, several warnings are generated. This is because webpack automatically includes non-javascript files due to a lazy-loading mechanism in a dependency called logform. The issue arises when ...