Skip to content

Commit

Permalink
UI: Enable first-party YouTube Chat features in OBS
Browse files Browse the repository at this point in the history
Unlock the full feature set of the YouTube Chat dock in OBS by removing
custom scripting/CSS logic. Enable the signed-in experience for live
streaming content creators (sharing login credentials with the YouTube
Control panel dock, see
#10747).

This will allow OBS users to utilize features _already_ supported in the
YouTube Chat plugin, such as

* creating polls
* managing Q&A sessions
* a rich emoji set in the input panel
* emoji fountains
* moderation tools

and many more. These features are available to users who are logged-in
to YouTube Chat and/or the YouTube Control panel.
  • Loading branch information
msuman-google committed Jun 10, 2024
1 parent 14fa71f commit 380a81d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
39 changes: 27 additions & 12 deletions UI/auth-youtube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#ifdef BROWSER_AVAILABLE
#include "window-dock-browser.hpp"
#include <QTimerEvent>
#endif

using namespace json11;
Expand Down Expand Up @@ -130,14 +131,6 @@ bool YoutubeAuth::LoadInternal()
return implicit ? !token.empty() : !refresh_token.empty();
}

#ifdef BROWSER_AVAILABLE
static const char *ytchat_script = "\
const obsCSS = document.createElement('style');\
obsCSS.innerHTML = \"#panel-pages.yt-live-chat-renderer {display: none;}\
yt-live-chat-viewer-engagement-message-renderer {display: none;}\";\
document.querySelector('head').appendChild(obsCSS);";
#endif

void YoutubeAuth::LoadUI()
{
if (uiLoaded)
Expand All @@ -163,7 +156,6 @@ void YoutubeAuth::LoadUI()

browser = cef->create_widget(chat, YOUTUBE_CHAT_PLACEHOLDER_URL,
panel_cookies);
browser->setStartupScript(ytchat_script);

chat->SetWidget(browser);
main->AddDockWidget(chat, Qt::RightDockWidgetArea);
Expand Down Expand Up @@ -350,6 +342,28 @@ std::shared_ptr<Auth> YoutubeAuth::Login(QWidget *owner,
}

#ifdef BROWSER_AVAILABLE
void YoutubeChatDock::timerEvent(QTimerEvent *event)
{
if (event->timerId() != awaitLoginTimer)
return;

QPointer<YoutubeChatDock> this_ = this;
auto cb = [this_](bool found) {
bool previouslyLoggedIn = this_->isLoggedIn;
this_->isLoggedIn = found;
if (found ^ previouslyLoggedIn) {
// There was a change in login state.
QMetaObject::invokeMethod(this_, "EnableChatInput",
Qt::QueuedConnection,
Q_ARG(bool, !found));
}
};
if (panel_cookies) {
panel_cookies->CheckForCookie("https://www.youtube.com", "SID",
cb);
}
}

void YoutubeChatDock::SetWidget(QCefWidget *widget_)
{
lineEdit = new LineEditAutoResize();
Expand Down Expand Up @@ -422,9 +436,10 @@ void YoutubeChatDock::ShowErrorMessage(const QString &error)
QTStr("YouTube.Chat.Error.Text").arg(error));
}

void YoutubeChatDock::EnableChatInput()
void YoutubeChatDock::EnableChatInput(bool visible)
{
lineEdit->setVisible(true);
sendButton->setVisible(true);
bool setVisible = visible && !isLoggedIn;
lineEdit->setVisible(setVisible);
sendButton->setVisible(setVisible);
}
#endif
15 changes: 13 additions & 2 deletions UI/auth-youtube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,35 @@
#include "window-dock-browser.hpp"
#include "lineedit-autoresize.hpp"
#include <QHBoxLayout>
#include <QTimerEvent>

class YoutubeChatDock : public BrowserDock {
Q_OBJECT

private:
std::string apiChatId;
int awaitLoginTimer;
bool isLoggedIn;
LineEditAutoResize *lineEdit;
QPushButton *sendButton;
QHBoxLayout *chatLayout;

protected:
void timerEvent(QTimerEvent *event);

public:
inline YoutubeChatDock(const QString &title) : BrowserDock(title) {}
inline YoutubeChatDock(const QString &title) : BrowserDock(title)
{
// Check youtube.com login status periodically
awaitLoginTimer = startTimer(1000);
}
void SetWidget(QCefWidget *widget_);
void SetApiChatId(const std::string &id);

private slots:
void SendChatMessage();
void ShowErrorMessage(const QString &error);
void EnableChatInput();
void EnableChatInput(bool visible = true);
};
#endif

Expand Down

0 comments on commit 380a81d

Please sign in to comment.