I am trying to create a Chrome Extension to manipulate the HTML on a page. The extension should remove or hide some elements from a page when activated.
I created a simple manifest.json
file:
{
"manifest_version": 2,
"name": "hide-msgs-ext",
"version": "0.0.1",
"description": "Hide msgs extension",
"content_scripts": [{
"js": ["content.js"],
"matches": ["https://website.example.com/*"],
"run_at": "document_idle"
}]
}
and a script content.js
, which should remove/hide all my target elements:
var elements = document.querySelectorAll('div.card');
for (var i = 0, l = elements.length; i < l; i++) {
subElement = elements[i].querySelector(':scope div.card-body');
if (subElement.firstChild.tagName != 'A') {
elements[i].style.display = "none";
}
}
If I execute the above script in the chrome console I am able to hide all the elements I want and no errors are triggered. However I could not figure it out how to make it work in the extension.
Could someone point me in the right direction? Thanks you in advance!