1

I have chrome-extension content_script that need to add something to youtube video description and comment section.

Problem is that youtube loads async, first you can see template, than video, than description section and comments section.

Is there an event that can be triggered when page fully loaded? Or some other easy solution?

I can think only to one solution - rerun useEffect every second until all the containers could be found.

export const App = () => {
  const [postContainer, setPostContainer] = useState<null | HTMLElement>(null)
  const [commentsContainer, setCommentsContainer] = useState<null | HTMLElement>(null)

  useEffect(() => {  
    $('#meta-contents').first().prepend(`<div id='my-post' />`)
    $('#comments').first().prepend(`<div id='my-comment' />`)

    setPostContainer($('#my-post').get(0))
    setCommentsContainer($('#my-comment').get(0))
  }, [])

  if (!postContainer || !commentsContainer) return null

  return (
    <div>
      <Portal target={postContainer}>
        <h1>POST</h1>
      </Portal>
      <Portal target={commentsContainer}>
        <h1>COMMENT</h1>
      </Portal>
    </div>
  )
}
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105

0 Answers0