I am writing a chrome extension that defines a custom element. I am using the webcomponents
library (documentation here) to polyfill my custom element.
If I call the function in my custom element I get an Uncaught TypeError: ... is not a function
error. What am I doing wrong?
background.js
class CustomElement extends HTMLElement {
constructor() {
super();
}
foobar() {
console.log("foobar!");
}
}
// Define the new element
customElements.define('custom-element', CustomElement);
const test = document.createElement("custom-element");
test.id = "test";
// works
test.foobar();
// doesn't work
$("#test").get(0).foobar();
// doesn't work
document.getElementById("test").foobar();
Manifest.json
{
"name": "Test",
"version": "1.0",
"description": "Test",
"browser_action": {
"default_title": "Test",
"default_popup": "html/popup.html"
},
"content_scripts": [ {
"js": [ "scripts/jquery-3.3.1.min.js", "scripts/background.js", "scripts/webcomponents-bundle.js" ],
"matches": ["<all_urls>"]
}],
"manifest_version": 2
}
If I open the Chrome console and try to find my element/call foobar()
there, it also doesn't work.