I'm trying to make an extension in Google Chrome that count the number of <li>
elements on a website. The problem is that the script I made will not target the ActiveTab DOM but the extension DOM by default. How can I make it target the ActiveTab DOM and not the Extension DOM ?
This is my manifest.json:
"manifest_version": 2,
"name": "My Extension",
"description": "Still in test",
"version": "1.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["scripts.js"]
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"icons": { "16": "logo.png",
"48": "logo.png",
"128": "logo.png" },
"browser_action": {
"default_icon": "logo.png",
"default_popup": "popup.html",
"default_title": "Click to open menu"
},
"permissions": [
"tabs",
"activeTab",
"https://ajax.googleapis.com/"
]
This is my button:
<input type="button" class="button-1" value="Count elements" id="counter">
This is my script in scripts.js:
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('counter');
link.addEventListener('click', function() {
console.log(document.getElementById("mylist").getElementsByTagName("li").length);
});
});
And this is the error that shows up because the wrong DOM is targetted:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null