Skip to content

Instantly share code, notes, and snippets.

@ngseke
Last active June 27, 2021 06:06
Show Gist options
  • Save ngseke/018ff069160df9912309d85726a948f9 to your computer and use it in GitHub Desktop.
Save ngseke/018ff069160df9912309d85726a948f9 to your computer and use it in GitHub Desktop.
Observe Youtube Chat Room Items
var $chatList = document
.querySelector('#chatframe')
.contentWindow
.document
.querySelector('#item-offset #items')
var observer = new MutationObserver(([{ target }]) => {
const items = [...target.querySelectorAll('yt-live-chat-text-message-renderer')]
.map($el => ({
img: $el.querySelector('#img').getAttribute('src'),
timestamp: $el.querySelector('#timestamp').innerText,
authorName: $el.querySelector('#author-name').innerText,
}))
console.log(items[items.length - 1]) // 印出列表的最後一項
})
observer.observe($chatList, { childList: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment