Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Background wont update the UI css in chrome extension

This is a chrome extension that supposes to change the look of a website to dark, I am storing the state of the checkbox from the popup then retrieving that in the background to to update the tab, the information seems to get stored but could not update the tab accordingly, I don't know what I am doing wrong here

popup.js

      function save_options() {
    
    var chekboxState = document.getElementById('toggle_button').checked;
    chrome.storage.sync.set({
      darkMode: chekboxState
    }, function() {
      // Update status to let user know options were saved.
      console.log('chekc box is ' + chekboxState);
      
    });
  }
  
  
  // Restores select box and checkbox state using the preferences
  function restore_options() {
    chrome.storage.sync.get({darkMode: 'true',}, 
    function(item) {
        console.log('chekc box is ' + item.darkMode);
        document.getElementById('toggle_button').checked = item.darkMode;
       
    });
  }

  document.addEventListener('DOMContentLoaded', function () {

    restore_options();
    document.getElementById('toggle_button').addEventListener('change',save_options);
  });

background.js

chrome.tabs.onUpdated.addListener(function(tabId, info,tab) {
   
    if (changeState()){
        chrome.scripting.insertCSS({
            files: ['newstyle.css'],
            target: { tabId: tab.id }
        });
        console.log('style was put back');
    }else {
        chrome.scripting.removeCSS({
            files: ['newstyle.css'],
            target: { tabId: tab.id }
            
        });
        console.log('style was removed');
    }

    //  }   
   
});


function changeState(){
    
    chrome.storage.sync.get(['darkMode'], function(result) {
        console.log('Value currently is ' + result.darkMode);
        console.log('popup opened');
        if (result.darkMode == true){
            console.log('newstyle.css');
            // return 'newstyle.css';
            return true;
        }
        else {
            console.log('nostyle.css');
            // return 'nostyle.css';
            return false;
        }
        
    });

}

chrome.runtime.onInstalled.addListener(function() {
    chrome.storage.sync.set({
        darkMode: true
      }, function() {
        // Update status to let user know options were saved.
        console.log('chekc box is true on instalations');
        
      });
});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!