You don't need any polyfills. I thank @fxnoob for pointing me to ProjectVisBug, but it doesn't use a polyfill.
Instead of using customElements.define
in the main content script that is loaded with chrome.tabs.executeScript
, VisBug uses that script as merely a wrapper to inject a <script>
tag. This tag loads the main JavaScript bundle and the custom element is defined there.
manifest.json:
{
"manifest_version": 2,
"version": "0.0.1",
"name": "My Extension",
"permissions": ["activeTab"],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Click to toggle"
},
"web_accessible_resources": ["build/*"]
}
background.js:
chrome.browserAction.onClicked.addListener(function(activeTab) {
chrome.tabs.executeScript(activeTab.id, {
file: "./inject.js",
runAt: "document_start",
});
});
inject.js: (runs in extension content script context)
const script = document.createElement("script");
script.type = "module";
script.src = chrome.runtime.getURL("build/main.js");
document.body.appendChild(script);
const myElement = document.createElement("my-element");
document.body.prepend(myElement);
build/main.js: (runs in webpage main world context)
class MyElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
}
}
customElements.define("my-element", MyElement);