make themes work better

This commit is contained in:
Malte Jürgens 2023-02-18 15:23:14 +01:00
parent 30c0526ff7
commit b39e23d462
No known key found for this signature in database
GPG Key ID: D29FBD5F93C0CFC3
6 changed files with 70 additions and 30 deletions

View File

@ -161,13 +161,14 @@ function main() {
userscript.log("Loading userstyles...");
userscript.loadingMessage = "Loading userstyles...";
let stylesheet = document.getElementById("discordScreenaudioUserstyles");
if (!stylesheet) {
if (stylesheet) {
userscript.log("Removing old userstyles...");
stylesheet.remove();
}
stylesheet = document.createElement("style");
stylesheet.id = "discordScreenaudioUserstyles";
stylesheet.type = "text/css";
document.head.appendChild(stylesheet);
}
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",

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -4,6 +4,9 @@
#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QInputDialog>
#include <QStandardPaths>
#include <QTimer>
#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);
}

View File

@ -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,