Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
How to render chrome extension UI depending on data?

I am creating my first chrome extension, i need to render the extension HTML once the extension is loaded depending on some data that i pre-created manually, no fetch from any external server, i have created all necessary assets of the extension and now i need to use popup.js to create the extension markup

popup.js

  const body = document.getElementById('body') // extenstion HTML body
  const bookList = document.getElementById('book-list')

  body.addEventListener('load', async () => {
     let [tab] = await chrome.tabs.query({ active:true,currentWindow:true})

     chrome.scripting.executeScript({
         target: { tabId: tab.id },
         function: renderExtension,
     });

  });

 function renderExtension() {
   chrome.storage.sync.get("books", ({ books}) => {
     books.forEach(book=> {
       bookList.innerHTML += `<input placeholder=${book.name}>`
     })
   })
 }

and in background.js

let books = [
   //books objects
]

chrome.runtime.onInstalled.addListener(() => {
  chrome.storage.sync.set({ books});

});

Unfortunately, this doesn't work, i get nothing rendered in the extension, it is the my first extension so may be i am missing something and i need to know what i am doing wrong here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

chrome.scripting.executeScript runs the script in the active web page in the tab, but the popup is a separate page with a chrome-extension:// URL shown in a separate window, completely unrelated to the web page. The popup.js script you load in your popup.html runs in that window so you should simply access its window, document, and DOM directly.

There's also no need for the background script and storage, just put the data in popup.js:

const books = [
  {name: 'foo'},
  {name: 'bar'},
];

document.getElementById('book-list').innerHTML =
  books.map(b => `<input placeholder=${b.name}>`).join('');

There's no need to wait for load either, just put your script at the end of body in popup.html:

<body>
  <div id="book-list"></div>
  <script src=popup.js></script>
</body>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda