raingart / Nova-YouTube-extension

Apache License 2.0
199 stars 9 forks source link

[Feature] Plugin to set chat to "Live chat" automatically #161

Closed pointydev closed 3 months ago

pointydev commented 4 months ago

A plugin that could set livestream chats to "Live chat" mode instead of "Top chat" automatically would be really useful in smaller streams in order to prevent messages in low traffic chats from being missed.

Thanks, pointy

raingart commented 4 months ago

it turned out to be easy

add a new plugin after the line window.nova_plugins = [];

window.nova_plugins.push({
  id: 'livechat-toggle-mode',
  title: '"Live chat" mode instead of "Top chat"',
  run_on_pages: 'live_chat, -mobile',
  //restart_on_location_change: true,
  section: 'sidebar',
  _runtime: user_settings => {
        NOVA.waitSelector('#chat-messages #menu a:nth-child(2)[aria-selected="false"]'/*, { destroy_after_page_leaving: true }*/)
               .then(btn => btn.click);
  },
});

and replace the iframe launch limiter this code !location.pathname.startsWith('/embed') to (!location.pathname.startsWith('/embed') && !location.pathname.startsWith('/live_chat'))

raingart commented 4 months ago

crap I don't see any other way except to add a delay so that the default yt event listener connects

replace .then(btn => btn.click); to

    .then(async btn => {
          await NOVA.delay(500);
          btn.click();
        });
pointydev commented 4 months ago

It seems to be working just when running asynchronously, without using delay:

.then(async btn => {
      await btn.click();
    });
raingart commented 4 months ago

I think this is a bug and not a feature https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

I wonder what will happen in other browsers. I didn’t realize that click was asynchronous. Perhaps asynchronous calls have some lag which is enough to use event listener callback

For example, it was a surprise to me that this code works Promise.resolve(document.querySelector(selector); but it turned out that he would never return the element.

raingart commented 3 months ago

oh shit, it's broken

example video does not work - #chat-messages #menu a:last-child[aria-selected="false"] works correct - #chat-messages #menu a[aria-selected="false"]

ps about await btn.click(); yes this is a bug. await adds ~500ms latency. For example here the script does not work because the delay is already 1500ms needed