I'm writing a Chrome extension that injects a javascript file into a page to add a button. When I go to the website I want to modify, the button always takes a fraction of a second longer to load after the page loads. Is there a way that I can load the button at the same time as the other elements on the page? I have tried using MutationObserver
, but I can't seem to figure it out.
manifest.json file:
{
"name": "Some extension",
"version": "1.0",
"description": "Some description",
"content_scripts": [
{
"js": ["script.js"],
"run_at": "document_start",
"matches": ["https://*.familysearch.org/*"]
}
],
"manifest_version": 2
}
Script to be injected:
// Create a button and append it to an element with a class of 'primaryNav'
let navElement = document.getElementById('primaryNav');
let newElement = document.createElement('button');
newElement.classList.add('primary-nav-text');
newElement.classList.add('nav-menu-trigger');
let textNode = document.createTextNode("FHTL");
newElement.append(textNode);
navElement.append(newElement);
I've tried this just to see if I can get MutationObserver
to work:
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
console.log(mutation.addedNodes[0]);
}
});
});
const navBar = document.getElementById('primaryNav');
observer.observe(navBar, {
childList: true
});
But I get this error (I assume it's because that element has not loaded yet):
Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.