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

442
Visualizações
Error on chorme extension: Uncaught ReferenceError: window is not defined

I'm developing a Chrome Extension (don't have a lot experience with it), and I've the following code:

window.onload = function () {
    validateCNPJ();
};

function validateCNPJ() {
    document.querySelector('div[data-field-name="CNPJ"] > input').addEventListener('keydown', (bla) => {
        let ble = bla.target.value;
        console.log(ble.length)
        if (ble.length > 17) {
            bla.preventDefault()
            bla.stopPropagation()
            return
        }

        ble = ble.replace(/\D/g, "")
        ble = ble.replace(/^(\d{2})(\d)/, "$1.$2")
        ble = ble.replace(/^(\d{2}).(\d{3})(\d)/, "$1.$2.$3")
        ble = ble.replace(/.(\d{3})(\d)/, ".$1/$2")
        ble = ble.replace(/(\d{4})(\d)/, "$1-$2")
        bla.target.value = ble;
    })
}

I added window.load so that when the page loads, the validateCNPJ function is activated. That function validates a specific field in a form in real time.

The issue is that when I upload the extension it shows a error message saying:

Uncaught ReferenceError: window is not defined

I tested the code inside the function validateCPNJ in the console of the browser and it worked normally.

The manifest file is like this:

{
    "name": "CRM Validation Extension",
    "description": "Extension that validate fields of a opportunity",
    "version": "0.1",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    }
}

What can I do to solve this issue?

Thanks!

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

0

I manage to solve the issue. Previously my manifest.json was like this:

{
    "name": "CRM Validation Extension",
    "description": "Extension that validate fields of a opportunity",
    "version": "0.1",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    }
}

I changed the background object and replaced with the following:

"content_scripts":[
        {
            "matches": [
                "<all_urls>"
            ],
            "js": ["background.js"]
        }
    ]

This fixed the issue I was having.

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