filter out stuff we dont want better

This commit is contained in:
Malte Jürgens 2023-02-17 17:13:49 +01:00
parent 3119e1df19
commit 30c0526ff7
No known key found for this signature in database
GPG key ID: D29FBD5F93C0CFC3
2 changed files with 45 additions and 47 deletions

View file

@ -9,7 +9,7 @@
#include <QWebEngineSettings> #include <QWebEngineSettings>
CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent) { CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent) {
setStyleSheet("background-color:#202225;"); setStyleSheet("background-color:#313338;");
m_layout = new QVBoxLayout(this); m_layout = new QVBoxLayout(this);
m_layout->setMargin(0); m_layout->setMargin(0);
m_layout->setSpacing(0); m_layout->setSpacing(0);
@ -70,6 +70,7 @@ void CentralWidget::setLoadingIndicator(QString text) {
m_loadingLabel = new QLabel(this); m_loadingLabel = new QLabel(this);
m_loadingLabel->setMaximumHeight(20); m_loadingLabel->setMaximumHeight(20);
m_loadingLabel->setAlignment(Qt::AlignHCenter); m_loadingLabel->setAlignment(Qt::AlignHCenter);
m_loadingLabel->setStyleSheet("color:#dedede;");
m_layout->addWidget(m_loadingLabel); m_layout->addWidget(m_loadingLabel);
} }
m_loadingLabel->setText(text); m_loadingLabel->setText(text);

View file

@ -16,7 +16,7 @@
#include <QWebEngineSettings> #include <QWebEngineSettings>
DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) { DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) {
setBackgroundColor(QColor("#202225")); setBackgroundColor(QColor("#313338"));
connect(this, &QWebEnginePage::featurePermissionRequested, this, connect(this, &QWebEnginePage::featurePermissionRequested, this,
&DiscordPage::featurePermissionRequested); &DiscordPage::featurePermissionRequested);
@ -53,24 +53,29 @@ void DiscordPage::setupPermissions() {
settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, true); settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, true);
} }
QString fileContent;
void DiscordPage::setupUserStyles() { void DiscordPage::setupUserStyles() {
auto file = new QFile( auto file = new QFile(
QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) +
"/userstyles.css"); "/userstyles.css");
if (file->exists()) { if (file->exists()) {
qDebug(userstylesLog) << "Found userstyles:" << file->fileName(); qDebug(userstylesLog) << "Found userstyles:" << file->fileName();
file->open(QIODevice::ReadOnly);
fileContent = file->readAll();
file->close();
fetchUserStyles(file); fetchUserStyles(file);
} }
} }
const QRegularExpression
importRegex(R"r(@import url\(['"]{0,1}([^'"]+?)['"]{0,1}\);)r");
const QRegularExpression urlRegex(
R"r(url\(['"]{0,1}((?!https:\/\/fonts.gstatic.com)(?!data:)(?!.*usrbgs?\.css)(?!.*\.woff2)(?!.*\.ttf)[^'"]+?)['"]{0,1}\))r");
void DiscordPage::fetchUserStyles(QFile *file) { void DiscordPage::fetchUserStyles(QFile *file) {
file->open(QIODevice::ReadOnly); m_userScript.setProperty(
auto fileContent = file->readAll(); "loadingMessage", "Loading userstyles: Fetching additional resources...");
file->close();
QRegularExpression importRegex(
R"r(@import url\(['"]{0,1}([^'"]+?)['"]{0,1}\);)r");
QRegularExpression urlRegex(
R"r(url\(['"]{0,1}((?!data:)[^'"]+?)['"]{0,1}\))r");
bool foundImport = true; bool foundImport = true;
auto match = importRegex.match(fileContent); auto match = importRegex.match(fileContent);
if (!match.hasMatch()) { if (!match.hasMatch()) {
@ -79,15 +84,10 @@ void DiscordPage::fetchUserStyles(QFile *file) {
} }
if (match.hasMatch()) { if (match.hasMatch()) {
auto url = match.captured(1); auto url = match.captured(1);
if (url.toLower().contains("usrbg.css") ||
url.toLower().contains("usrbgs.css")) {
qDebug(userstylesLog)
<< "Skipping" << url << "because it we can't prefetch it";
} else {
qDebug(userstylesLog) << "Fetching" << url; qDebug(userstylesLog) << "Fetching" << url;
m_userScript.setProperty( m_userScript.setProperty(
"loadingMessage", "loadingMessage",
QString("Loading userstyles: Fetching %1").arg(url)); QString("Loading userstyles: Fetching %1...").arg(url));
QNetworkRequest request(url); QNetworkRequest request(url);
auto reply = m_networkAccessManager.get(request); auto reply = m_networkAccessManager.get(request);
connect(reply, &QNetworkReply::finished, [=]() { connect(reply, &QNetworkReply::finished, [=]() {
@ -102,24 +102,21 @@ void DiscordPage::fetchUserStyles(QFile *file) {
content = reply->readAll(); content = reply->readAll();
} else } else
qDebug(userstylesLog) << reply->errorString().toUtf8().constData(); qDebug(userstylesLog) << reply->errorString().toUtf8().constData();
file->open(QIODevice::WriteOnly); reply->deleteLater();
file->write( fileContent = fileContent.replace(
QString(fileContent) match.captured(0), foundImport
.replace(match.captured(0),
foundImport
? content ? content
: "url(data:application/octet-stream;base64," + : "url(data:application/octet-stream;base64," +
content.toBase64() + ")") content.toBase64() + ")");
.toUtf8()
.constData());
file->close();
fetchUserStyles(file); fetchUserStyles(file);
}); });
return; return;
} }
}
qDebug(userstylesLog) << "Injecting userstyles"; qDebug(userstylesLog) << "Injecting userstyles";
m_userScript.setProperty("userstyles", fileContent); m_userScript.setProperty("userstyles", fileContent);
file->open(QIODevice::WriteOnly);
file->write(fileContent.toUtf8().constData());
file->close();
file->deleteLater(); file->deleteLater();
} }