filter out stuff we dont want better
This commit is contained in:
parent
3119e1df19
commit
30c0526ff7
2 changed files with 45 additions and 47 deletions
|
@ -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);
|
||||||
|
|
|
@ -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,47 +84,39 @@ 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") ||
|
qDebug(userstylesLog) << "Fetching" << url;
|
||||||
url.toLower().contains("usrbgs.css")) {
|
m_userScript.setProperty(
|
||||||
qDebug(userstylesLog)
|
"loadingMessage",
|
||||||
<< "Skipping" << url << "because it we can't prefetch it";
|
QString("Loading userstyles: Fetching %1...").arg(url));
|
||||||
} else {
|
QNetworkRequest request(url);
|
||||||
qDebug(userstylesLog) << "Fetching" << url;
|
auto reply = m_networkAccessManager.get(request);
|
||||||
m_userScript.setProperty(
|
connect(reply, &QNetworkReply::finished, [=]() {
|
||||||
"loadingMessage",
|
QByteArray content = "";
|
||||||
QString("Loading userstyles: Fetching %1").arg(url));
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
QNetworkRequest request(url);
|
if (!reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
|
||||||
auto reply = m_networkAccessManager.get(request);
|
.isNull())
|
||||||
connect(reply, &QNetworkReply::finished, [=]() {
|
content =
|
||||||
QByteArray content = "";
|
reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
.toByteArray();
|
||||||
if (!reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
|
else
|
||||||
.isNull())
|
content = reply->readAll();
|
||||||
content =
|
} else
|
||||||
reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
|
qDebug(userstylesLog) << reply->errorString().toUtf8().constData();
|
||||||
.toByteArray();
|
reply->deleteLater();
|
||||||
else
|
fileContent = fileContent.replace(
|
||||||
content = reply->readAll();
|
match.captured(0), foundImport
|
||||||
} else
|
? content
|
||||||
qDebug(userstylesLog) << reply->errorString().toUtf8().constData();
|
: "url(data:application/octet-stream;base64," +
|
||||||
file->open(QIODevice::WriteOnly);
|
content.toBase64() + ")");
|
||||||
file->write(
|
fetchUserStyles(file);
|
||||||
QString(fileContent)
|
});
|
||||||
.replace(match.captured(0),
|
return;
|
||||||
foundImport
|
|
||||||
? content
|
|
||||||
: "url(data:application/octet-stream;base64," +
|
|
||||||
content.toBase64() + ")")
|
|
||||||
.toUtf8()
|
|
||||||
.constData());
|
|
||||||
file->close();
|
|
||||||
fetchUserStyles(file);
|
|
||||||
});
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue