Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda