I have a Chrome Extension that is injecting HTML/CSS into Google search pages from the Content Script.
Currently, I'm doing this using insertAdjacentHTML() and a long single string of HTML.
Relevant code snippets:
const chosenElements = document.getElementsByClassName('r');
for (var i = 0; i < chosenElements.length; i++) {
chosenElements[i].insertAdjacentHTML('afterbegin', createHTML(name));
}
function createHTML(name) {
return (
'<a style="display: flex; justify-content: center; .... font-weight: 500;" href="https://www.mywebsite.com/">Click here to go to ' +
name +
' website</a>'
);
This does the right thing, and adds the HTML/CSS to the page, but is a pretty impractical method of doing this for me, as I want to greatly expand the HTML and CSS of the injected content, and doing so in one big line like this isn't going to really work.
Is there a better method of doing this injection?
Thinking of something like linking to a separate HTML file that has its own CSS file styling, or something similar.
Thanks.