diff --git a/UI/auth-youtube.cpp b/UI/auth-youtube.cpp index 11587cc2df5952..2287448ced5ba9 100644 --- a/UI/auth-youtube.cpp +++ b/UI/auth-youtube.cpp @@ -26,6 +26,7 @@ #ifdef BROWSER_AVAILABLE #include "window-dock-browser.hpp" +#include #endif using namespace json11; @@ -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) @@ -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); @@ -350,6 +342,28 @@ std::shared_ptr YoutubeAuth::Login(QWidget *owner, } #ifdef BROWSER_AVAILABLE +void YoutubeChatDock::timerEvent(QTimerEvent *event) +{ + if (event->timerId() != awaitLoginTimer) + return; + + QPointer 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(); @@ -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 diff --git a/UI/auth-youtube.hpp b/UI/auth-youtube.hpp index 28e17148d847d0..d640c2c79becf2 100644 --- a/UI/auth-youtube.hpp +++ b/UI/auth-youtube.hpp @@ -11,24 +11,35 @@ #include "window-dock-browser.hpp" #include "lineedit-autoresize.hpp" #include +#include + 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