From b39e23d46231d63566d355ebeff9d5dfdfaa073d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Sat, 18 Feb 2023 15:23:14 +0100 Subject: [PATCH] make themes work better --- assets/userscript.js | 23 ++++++++++++++----- src/centralwidget.cpp | 2 +- src/discordpage.cpp | 51 ++++++++++++++++++++++++------------------- src/discordpage.h | 7 +++++- src/userscript.cpp | 14 ++++++++++++ src/userscript.h | 3 +++ 6 files changed, 70 insertions(+), 30 deletions(-) diff --git a/assets/userscript.js b/assets/userscript.js index 129d1a0..7b925cb 100644 --- a/assets/userscript.js +++ b/assets/userscript.js @@ -161,13 +161,14 @@ function main() { userscript.log("Loading userstyles..."); userscript.loadingMessage = "Loading userstyles..."; let stylesheet = document.getElementById("discordScreenaudioUserstyles"); - if (!stylesheet) { - stylesheet = document.createElement("style"); - stylesheet.id = "discordScreenaudioUserstyles"; - stylesheet.type = "text/css"; - document.head.appendChild(stylesheet); + if (stylesheet) { + userscript.log("Removing old userstyles..."); + stylesheet.remove(); } + stylesheet = document.createElement("style"); + stylesheet.id = "discordScreenaudioUserstyles"; stylesheet.innerText = userscript.userstyles; + document.head.appendChild(stylesheet); userscript.log("Finished loading userstyles"); userscript.loadingMessage = ""; } @@ -329,6 +330,18 @@ function main() { }) ); + section.appendChild( + createButton("Install Theme", () => { + userscript.showThemeDialog(); + }) + ); + + section.appendChild( + createButton("Uninstall Theme", () => { + userscript.installUserStyles(""); + }) + ); + section.appendChild( createSwitch( "Move discord-screenaudio to the system tray instead of closing", diff --git a/src/centralwidget.cpp b/src/centralwidget.cpp index 71c4b6b..2e7aaf1 100644 --- a/src/centralwidget.cpp +++ b/src/centralwidget.cpp @@ -73,7 +73,7 @@ void CentralWidget::setLoadingIndicator(QString text) { m_loadingLabel->setStyleSheet("color:#dedede;"); m_layout->addWidget(m_loadingLabel); } - m_loadingLabel->setText(text); + m_loadingLabel->setText(text.mid(0, 100)); } else { if (m_loadingLabel != nullptr) { m_layout->removeWidget(m_loadingLabel); diff --git a/src/discordpage.cpp b/src/discordpage.cpp index 028b399..b6b06d8 100644 --- a/src/discordpage.cpp +++ b/src/discordpage.cpp @@ -53,34 +53,35 @@ void DiscordPage::setupPermissions() { settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, true); } -QString fileContent; - void DiscordPage::setupUserStyles() { - auto file = new QFile( + m_userStylesFile = new QFile( QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/userstyles.css"); - if (file->exists()) { - qDebug(userstylesLog) << "Found userstyles:" << file->fileName(); - file->open(QIODevice::ReadOnly); - fileContent = file->readAll(); - file->close(); - fetchUserStyles(file); + if (m_userStylesFile->exists()) { + qDebug(userstylesLog) << "Found userstyles:" + << m_userStylesFile->fileName(); + m_userStylesFile->open(QIODevice::ReadOnly); + m_userStylesContent = m_userStylesFile->readAll(); + m_userStylesFile->close(); + fetchUserStyles(); } + connect(&m_userScript, &UserScript::shouldInstallUserStyles, this, + &DiscordPage::getUserStyles); } -const QRegularExpression - importRegex(R"r(@import url\(['"]{0,1}([^'"]+?)['"]{0,1}\);)r"); +const QRegularExpression importRegex( + R"r(@import (?:url\(|)['"]{0,1}(?!.*usrbgs?\.css)([^'"]+?)['"]{0,1}(?:|\));)r"); const QRegularExpression urlRegex( - R"r(url\(['"]{0,1}((?!https:\/\/fonts.gstatic.com)(?!data:)(?!.*usrbgs?\.css)(?!.*\.woff2)(?!.*\.ttf)[^'"]+?)['"]{0,1}\))r"); + R"r(url\(['"]{0,1}((?!https:\/\/fonts.gstatic.com)(?!data:)(?!.*\.woff2)(?!.*\.ttf)[^'"]+?)['"]{0,1}\))r"); -void DiscordPage::fetchUserStyles(QFile *file) { +void DiscordPage::fetchUserStyles() { m_userScript.setProperty( "loadingMessage", "Loading userstyles: Fetching additional resources..."); bool foundImport = true; - auto match = importRegex.match(fileContent); + auto match = importRegex.match(m_userStylesContent); if (!match.hasMatch()) { foundImport = false; - match = urlRegex.match(fileContent); + match = urlRegex.match(m_userStylesContent); } if (match.hasMatch()) { auto url = match.captured(1); @@ -103,21 +104,26 @@ void DiscordPage::fetchUserStyles(QFile *file) { } else qDebug(userstylesLog) << reply->errorString().toUtf8().constData(); reply->deleteLater(); - fileContent = fileContent.replace( + m_userStylesContent = m_userStylesContent.replace( match.captured(0), foundImport ? content : "url(data:application/octet-stream;base64," + content.toBase64() + ")"); - fetchUserStyles(file); + fetchUserStyles(); }); return; } qDebug(userstylesLog) << "Injecting userstyles"; - m_userScript.setProperty("userstyles", fileContent); - file->open(QIODevice::WriteOnly); - file->write(fileContent.toUtf8().constData()); - file->close(); - file->deleteLater(); + m_userScript.setProperty("userstyles", m_userStylesContent); + m_userScript.setProperty("loadingMessage", ""); + m_userStylesFile->open(QIODevice::WriteOnly); + m_userStylesFile->write(m_userStylesContent.toUtf8()); + m_userStylesFile->close(); +} + +void DiscordPage::getUserStyles(QString url) { + m_userStylesContent = url == "" ? "" : QString("@import url(%1);").arg(url); + fetchUserStyles(); } void DiscordPage::injectScript( @@ -142,7 +148,6 @@ void DiscordPage::injectScript(QString name, QString content) { void DiscordPage::injectStylesheet(QString name, QString content) { auto script = QString(R"(const stylesheet = document.createElement("style"); -stylesheet.type = "text/css"; stylesheet.id = "%1"; stylesheet.innerText = `%2`; document.head.appendChild(stylesheet); diff --git a/src/discordpage.h b/src/discordpage.h index e42d484..6223507 100644 --- a/src/discordpage.h +++ b/src/discordpage.h @@ -17,10 +17,12 @@ public: private: UserScript m_userScript; + QFile *m_userStylesFile; + QString m_userStylesContent; QNetworkAccessManager m_networkAccessManager; void setupPermissions(); void setupUserStyles(); - void fetchUserStyles(QFile *file); + void fetchUserStyles(); bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override; @@ -39,6 +41,9 @@ private: private Q_SLOTS: void featurePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature); + +public Q_SLOTS: + void getUserStyles(QString url); }; // Will immediately get destroyed again but is needed for navigation to diff --git a/src/userscript.cpp b/src/userscript.cpp index b8a6171..14f65b0 100644 --- a/src/userscript.cpp +++ b/src/userscript.cpp @@ -4,6 +4,9 @@ #include #include +#include +#include +#include #include #ifdef KXMLGUI @@ -163,3 +166,14 @@ void UserScript::showStreamDialog() { m_streamDialog->activateWindow(); m_streamDialog->updateTargets(); } + +void UserScript::showThemeDialog() { + auto url = QInputDialog::getText(MainWindow::instance(), "Theme Installation", + "Please enter the URL of the Theme"); + if (url != "") + emit shouldInstallUserStyles(url); +} + +void UserScript::installUserStyles(QString url) { + emit shouldInstallUserStyles(url); +} diff --git a/src/userscript.h b/src/userscript.h index 8464b3a..16e4985 100644 --- a/src/userscript.h +++ b/src/userscript.h @@ -65,6 +65,7 @@ Q_SIGNALS: void streamStarted(bool video, int width, int height, int frameRate); void userstylesChanged(); void loadingMessageChanged(QString message); + void shouldInstallUserStyles(QString url); public Q_SLOTS: void log(QString message); @@ -78,6 +79,8 @@ public Q_SLOTS: void showStreamDialog(); void stopVirtmic(); void startVirtmic(QString target); + void showThemeDialog(); + void installUserStyles(QString url); private Q_SLOTS: void startStream(bool video, bool audio, int width, int height, int frameRate,