0

Currently I am trying to inject some HTML onto Youtube's Page with a chrome extension that I am working on. They use custom HTML on their page and so I am creating custom DOM Nodes like theirs.

  const channelSection = document.createElement("ytd-rich-section-renderer");

However, when I execute the above node I do not see my injected HTML anywhere on the page. To bug test this, I instead switched out the tag name for a basic div tag and it worked then.

For reference, I was looking at this other stackoverflow question to accomplish my goal. Thanks for the help!

Edit: Thanks for all the responses! So after reloading the extension a couple of times, I can actually see my custom HTML injected in the page via the console.

content.js


  const ce = function(tag) {
        return document.createElement(tag)
     }
  const channelSection = ce("ytd-rich-section-renderer");
  channelSection.classList.add("style-scope", "ytd-rich-grid-renderer");

  let content = ce("div");
  content.classList.add("style-scope", "ytd-rich-section-renderer");

  let subContent = ce("ytd-rich-shelf-renderer");
  subContent.classList.add("style-scope", "ytd-rich-section-renderer");

  let dismiss = ce("div");
  dismiss.setAttribute("id", "dismissable");
  dismiss.classList.add("style-scope", "ytd-rich-shelf-renderer");

  // Heading
  let richShelf = ce("div");
  richShelf.setAttribute("id", "rich-shelf-header");
  richShelf.classList.add("style-scope", "ytd-rich-shelf-renderer");

  // Put Cards In here
  let contents = ce("div");
  contents.setAttribute("id", "contents");
  contents.classList.add("style-scope", "ytd-rich-shelf-renderer");

  dismiss.appendChild(richShelf);
  dismiss.appendChild(contents);
  subContent.appendChild(dismiss);
  content.appendChild(subContent);
  channelSection.appendChild(content);

  // This works
  let mainPage = document.querySelector("#contents");
      mainPage.prepend(channelSection);

Now my main issue that is I only see the channelSection and content nodes on the page. Am I appending the DOM nodes incorrectly possibly?

4
  • can you please post the complete relevant code? Commented Sep 21, 2020 at 19:49
  • You're creating the element...where are you adding it to your page? Commented Sep 21, 2020 at 19:52
  • Does this answer your question? info on javascript document.createElement() Commented Sep 21, 2020 at 19:53
  • Now that you solved your initial question, mark the relevant answer as a solution and ask a new question. Don't transmogrify your original question into a new one. Commented Sep 22, 2020 at 4:26

1 Answer 1

0

Document.createElement method creates a Element and stores it in the variable, it is not inserted into the DOM, you have to do it manually, using appending methods like:

Note that when you create a custom HTML Tag, you will get a HTMLUnknownElement which derives from the HTMLElement interface and does not implement any additional properties or methods.

I recommend you to use the Template tag to create the node you want to insert to make your code more readable, example:

function htmlToElement(html) {
    var template = document.createElement('template');
    template.innerHTML = html;
    return template.content.firstChild;
}

You can use like this:

var myContent =  htmlToElement('<div class="my-class"><div class="mychild-class"></div><span id="my-span"></span></div>');
//Getting an element inside the created node
var mySpan = myContent.querySelector("#my-span");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.