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

449
Views
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 answers
Answer question

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 Report
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!