I'm interested in creating extension

For my extension I need to get every new message in the chat.
How can I do that?

I thought about doing this with js and doing a loop and getting all the messages in the chat (because I have access to the page source), but thats seems wrong is there any better way of doing that?

You need a chat bot for that. Take a look at tmi.js

Or make your extension connect to Twitch Chat directly over WebSockets.

A library like TMI.js can help with that, but you could just direct connect and parse yourself