From c52daab420ffb72c1614a67d4a85dab5514a68ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Wed, 15 Feb 2023 01:59:22 +0100 Subject: [PATCH] allow for custom user styles --- src/discordpage.cpp | 67 +++++++++++++++++++++++++++++++++------------ src/discordpage.h | 11 ++++++-- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/discordpage.cpp b/src/discordpage.cpp index 65596f3..bcb4ed7 100644 --- a/src/discordpage.cpp +++ b/src/discordpage.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -20,11 +21,23 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) { connect(this, &QWebEnginePage::featurePermissionRequested, this, &DiscordPage::featurePermissionRequested); - connect(this, &QWebEnginePage::loadStarted, [=]() { - runJavaScript(QString("window.discordScreenaudioVersion = '%1';") - .arg(QApplication::applicationVersion())); - }); + setupPermissions(); + injectFile(&DiscordPage::injectScript, "qwebchannel.js", + ":/qtwebchannel/qwebchannel.js"); + + setUrl(QUrl("https://discord.com/app")); + + setWebChannel(new QWebChannel(this)); + webChannel()->registerObject("userscript", &m_userScript); + + injectFile(&DiscordPage::injectScript, "userscript.js", + ":/assets/userscript.js"); + + setupUserStyles(); +} + +void DiscordPage::setupPermissions() { settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, true); settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true); settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, @@ -36,18 +49,21 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) { false); settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false); settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, true); - - injectScriptFile("qwebchannel.js", ":/qtwebchannel/qwebchannel.js"); - - setUrl(QUrl("https://discord.com/app")); - - setWebChannel(new QWebChannel(this)); - webChannel()->registerObject("userscript", &m_userScript); - - injectScriptFile("userscript.js", ":/assets/userscript.js"); } -void DiscordPage::injectScriptText(QString name, QString content) { +void DiscordPage::setupUserStyles() { + QString file = + QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + + "/userstyles.css"; + if (QFileInfo(file).exists()) { + qDebug(mainLog) << "Found userstyles:" << file; + injectFile(&DiscordPage::injectStylesheet, "userstyles.js", file); + } +} + +void DiscordPage::injectScript( + QString name, QString content, + QWebEngineScript::InjectionPoint injectionPoint) { qDebug(mainLog) << "Injecting " << name; QWebEngineScript script; @@ -55,20 +71,37 @@ void DiscordPage::injectScriptText(QString name, QString content) { script.setSourceCode(content); script.setName(name); script.setWorldId(QWebEngineScript::MainWorld); - script.setInjectionPoint(QWebEngineScript::DocumentCreation); + script.setInjectionPoint(injectionPoint); script.setRunsOnSubFrames(false); scripts().insert(script); } -void DiscordPage::injectScriptFile(QString name, QString source) { +void DiscordPage::injectScript(QString name, QString content) { + injectScript(name, content, QWebEngineScript::DocumentCreation); +} + +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); +)") + .arg(name) + .arg(content); + injectScript(name, script, QWebEngineScript::DocumentReady); +} + +void DiscordPage::injectFile(void (DiscordPage::*inject)(QString, QString), + QString name, QString source) { QFile file(source); if (!file.open(QIODevice::ReadOnly)) { qFatal("Failed to load %s with error: %s", source.toLatin1().constData(), file.errorString().toLatin1().constData()); } else { - injectScriptText(name, file.readAll()); + (this->*inject)(name, file.readAll()); } } diff --git a/src/discordpage.h b/src/discordpage.h index cc0da53..2b0f58f 100644 --- a/src/discordpage.h +++ b/src/discordpage.h @@ -4,6 +4,7 @@ #include #include +#include class DiscordPage : public QWebEnginePage { Q_OBJECT @@ -13,6 +14,8 @@ public: private: UserScript m_userScript; + void setupPermissions(); + void setupUserStyles(); bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override; @@ -21,8 +24,12 @@ private: javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID) override; - void injectScriptText(QString name, QString content); - void injectScriptFile(QString name, QString source); + void injectScript(QString name, QString content, + QWebEngineScript::InjectionPoint injectionPoint); + void injectScript(QString name, QString content); + void injectStylesheet(QString name, QString content); + void injectFile(void (DiscordPage::*inject)(QString, QString), QString name, + QString source); private Q_SLOTS: void featurePermissionRequested(const QUrl &securityOrigin,