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

222
Vistas
chrome extension get number of open tabs

I'm writing an extension that has a button with different functions. So I'm trying to figure out how to retrieve number of tabs that are open and in my content.js I want to execute different thing when clicking the button depending on how many tabs that are open.

Been trying different approaches with chrome.tabs.query in background.js and sending it back but having troubles since it's asynchronous.

content.js

     if (tabcount < 2) {
    $(function () {
        $("#Back").on('click', function (e) {
            e.preventDefault();
            window.history.back();
        });
    });
    console.log("one tab");
}

if (tabcount > 1) {
    $(function () {
        $("#Back").on('click', function (e) {
            e.preventDefault();
            window.location.href = 'exit';
        });
    });
    console.log("more tabs");
}

Edit: added message code

Background message code

function sendMessageToTab(tabId, message) {
    return new Promise((resolve) => {
        chrome.tabs.sendMessage(tabId, message, resolve)
    })
}

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    if (changeInfo.status == 'complete') {
        chrome.tabs.query({ windowType: 'normal' }, function (tabs) {
            const msg = tabs.length;
            sendMessageToTab(tabs[0].id, msg)
        });
    } return true;
});

content recieve

 chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {

        if (request > 1) {console.log('more than 1 tab open'); }

    });
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