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

View File

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