I am trying to remove an element
For this purpose I am trying to make small plug in in .js
I have tailored this to this one specific google form I want to use it at, and for some reason I am haveing more issues in docs.google.com than I have in other websites
// content.js
const element = document.getElementsByClassName("xxxxxx");
element[0].remove();
"xxxxx" is the class of the block that holds the email display, I’ve changed it here, idk if one could pull the emails with it
When I paste the code in the console manually it works as I want it ,
but if I run it by plug in it gives me an error code:
"Uncaught TypeError: Cannot read Properties of undefined (reading ‘remove’)"
2
Answers
I’m not really that skilled, but you may be getting that error because of all the nesting, try this.
Also, make sure the website is fully loaded before your plugin executes the code.
Looks like element[0] is undefined when you’re trying to access it’s remove() method.
The plugin might be executing before the element is loaded onto the page.
You could change the plugin code to wait for the page to load before calling the remove function that removes the element via DOMContentLoaded event or window.onload event.
Something like the below:
So here it waits for the DOMContentLoaded event before calling remove().
It also checks if the element with class "xxxxxx" exists before trying to remove it.