From 63180f5d53271d771ab1bf87b01136b865ec1657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Fri, 17 Feb 2023 15:47:36 +0100 Subject: [PATCH] load userstyles on demand --- assets/userscript.js | 16 ++++++++++++++++ src/discordpage.cpp | 6 ++++-- src/userscript.cpp | 4 +++- src/userscript.h | 16 ++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/assets/userscript.js b/assets/userscript.js index b8605a6..bd0e25f 100644 --- a/assets/userscript.js +++ b/assets/userscript.js @@ -157,6 +157,22 @@ function main() { streamStartBtnClone.remove(); }); + function updateUserstyles() { + userscript.log("Loading userstyles..."); + let stylesheet = document.getElementById("discordScreenaudioUserstyles"); + if (!stylesheet) { + stylesheet = document.createElement("style"); + stylesheet.id = "discordScreenaudioUserstyles"; + stylesheet.type = "text/css"; + document.head.appendChild(stylesheet); + } + stylesheet.innerText = userscript.userstyles; + userscript.log("Finished loading userstyles"); + } + + userscript.userstylesChanged.connect(updateUserstyles); + setTimeout(() => updateUserstyles()); + setInterval(async () => { const streamActive = document.getElementsByClassName("panel-2ZFCRb activityPanel-9icbyU") diff --git a/src/discordpage.cpp b/src/discordpage.cpp index 2d1edb9..6d07d36 100644 --- a/src/discordpage.cpp +++ b/src/discordpage.cpp @@ -20,6 +20,8 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) { connect(this, &QWebEnginePage::featurePermissionRequested, this, &DiscordPage::featurePermissionRequested); + connect(this, &DiscordPage::fullScreenRequested, MainWindow::instance(), + &MainWindow::fullScreenRequested); setupPermissions(); @@ -113,8 +115,8 @@ void DiscordPage::fetchUserStyles(QFile *file) { return; } } - injectFile(&DiscordPage::injectStylesheet, "userstyles.js", file->fileName()); - file->close(); + qDebug(userstylesLog) << "Injecting userstyles"; + m_userScript.setProperty("userstyles", fileContent); file->deleteLater(); } diff --git a/src/userscript.cpp b/src/userscript.cpp index 18e3a28..db345e5 100644 --- a/src/userscript.cpp +++ b/src/userscript.cpp @@ -15,6 +15,8 @@ UserScript::UserScript() : QObject() { setupShortcutsDialog(); setupStreamDialog(); setupVirtmic(); + // connect(this, &UserScript::loadingMessageChanged, MainWindow::instance(), + // &MainWindow::setLoadingIndicator); } void UserScript::setupHelpMenu() { @@ -162,4 +164,4 @@ void UserScript::showStreamDialog() { else m_streamDialog->activateWindow(); m_streamDialog->updateTargets(); -} \ No newline at end of file +} diff --git a/src/userscript.h b/src/userscript.h index 2eacc1d..f632f40 100644 --- a/src/userscript.h +++ b/src/userscript.h @@ -18,6 +18,12 @@ #endif +#ifdef KNOTIFICATIONS +#include +#endif + +#include + class UserScript : public QObject { Q_OBJECT @@ -27,18 +33,26 @@ public: Q_PROPERTY(QString version READ version CONSTANT); Q_PROPERTY(bool kxmlgui MEMBER m_kxmlgui CONSTANT); Q_PROPERTY(bool kglobalaccel MEMBER m_kglobalaccel CONSTANT); + Q_PROPERTY(QString userstyles MEMBER m_userstyles NOTIFY userstylesChanged); + Q_PROPERTY(QString loadingMessage MEMBER m_loadingMessage NOTIFY + loadingMessageChanged); private: QProcess m_virtmicProcess; StreamDialog *m_streamDialog; bool m_kxmlgui = false; bool m_kglobalaccel = false; + QString m_userstyles; + QString m_loadingMessage; #ifdef KXMLGUI KHelpMenu *m_helpMenu; #ifdef KGLOBALACCEL KActionCollection *m_actionCollection; KShortcutsDialog *m_shortcutsDialog; #endif +#endif +#ifdef KNOTIFICATIONS + KJob *m_loadingJob = nullptr; #endif void setupHelpMenu(); void setupShortcutsDialog(); @@ -49,6 +63,8 @@ Q_SIGNALS: void muteToggled(); void deafenToggled(); void streamStarted(bool video, int width, int height, int frameRate); + void userstylesChanged(); + void loadingMessageChanged(); public Q_SLOTS: void log(QString message);