querySelector not working as expected from my plugin but working in console [closed]

1 day ago 1
ARTICLE AD BOX

I'm creating a chrome plugin and i want to select few elements using querySelector, but when i run my plugin its returning null but when i run my same code in console im getting elements

MY PLUGIN CODE:

function elementCounter() { a = document.querySelectorAll(".companyWrapper"); console.log(a) console.log(a.length) } elementCounter();

OUTPUT:
output i get from extension

MY CONSOLE CODE:

document.querySelectorAll(".companyWrapper");

OUTPUT:
output i get directly running in console

MY PROJECT STRUCTURE

manifest.json scripts |- job_hider.js

manifest.json

{ "name": "Hello Extensions of the world!", "description": "Base Level Extension", "version": "1.0", "manifest_version": 3, "content_scripts": [ { "js": [ "scripts/job_hider.js" ], "matches": [ "https://www.naukri.com/*" ] } ] }

I though it would be a timing issue where elements are loaded after so I tried mutations but i dont find any mutation that are adding these elements

EDIT 1: my code works for other elements, when I try to access other elements its working fine, so I thought maybe those elements are getting loaded dynamically, so I tried mutation

Full job_hider.js

const observer = new MutationObserver((mutations) => { for (const mutation of mutations) { console.log(mutation) for (const node of mutation.addedNodes) { if (node instanceof Element && node.className === 'companyWrapper') { elementCounter(); } } } }); observer.observe(document.querySelector('body'), { childList: true }); function elementCounter() { a = document.querySelectorAll(".companyWrapper"); console.log(a) console.log(a.length) } elementCounter();
Read Entire Article