try to prefetch external userstyle urls

This commit is contained in:
Malte Jürgens 2023-02-15 15:19:43 +01:00
parent 02fdc62fbe
commit 74fdef683f
No known key found for this signature in database
GPG key ID: D29FBD5F93C0CFC3
4 changed files with 68 additions and 5 deletions

View file

@ -52,15 +52,72 @@ void DiscordPage::setupPermissions() {
} }
void DiscordPage::setupUserStyles() { void DiscordPage::setupUserStyles() {
QString file = auto file = new QFile(
QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) +
"/userstyles.css"; "/userstyles.css");
if (QFileInfo(file).exists()) { if (file->exists()) {
qDebug(mainLog) << "Found userstyles:" << file; qDebug(userstylesLog) << "Found userstyles:" << file->fileName();
injectFile(&DiscordPage::injectStylesheet, "userstyles.js", file); fetchUserStyles(file);
} }
} }
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");
bool foundImport = true;
auto match = importRegex.match(fileContent);
if (!match.hasMatch()) {
foundImport = false;
match = urlRegex.match(fileContent);
}
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;
QNetworkRequest request(url);
auto reply = m_networkAccessManager.get(request);
connect(reply, &QNetworkReply::finished, [=]() {
QByteArray content = "";
if (reply->error() == QNetworkReply::NoError) {
if (!reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
.isNull())
content =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
.toByteArray();
else
content = reply->readAll();
} else
qDebug(userstylesLog) << reply->errorString().toUtf8().constData();
file->open(QIODevice::WriteOnly);
file->write(
QString(fileContent)
.replace(match.captured(0),
foundImport
? content
: "url(data:application/octet-stream;base64," +
content.toBase64() + ")")
.toUtf8()
.constData());
file->close();
fetchUserStyles(file);
});
return;
}
}
injectFile(&DiscordPage::injectStylesheet, "userstyles.js", file->fileName());
file->close();
file->deleteLater();
}
void DiscordPage::injectScript( void DiscordPage::injectScript(
QString name, QString content, QString name, QString content,
QWebEngineScript::InjectionPoint injectionPoint) { QWebEngineScript::InjectionPoint injectionPoint) {

View file

@ -2,6 +2,8 @@
#include "userscript.h" #include "userscript.h"
#include <QFile>
#include <QNetworkAccessManager>
#include <QWebEngineFullScreenRequest> #include <QWebEngineFullScreenRequest>
#include <QWebEnginePage> #include <QWebEnginePage>
#include <QWebEngineScript> #include <QWebEngineScript>
@ -14,8 +16,10 @@ public:
private: private:
UserScript m_userScript; UserScript m_userScript;
QNetworkAccessManager m_networkAccessManager;
void setupPermissions(); void setupPermissions();
void setupUserStyles(); void setupUserStyles();
void fetchUserStyles(QFile *file);
bool acceptNavigationRequest(const QUrl &url, bool acceptNavigationRequest(const QUrl &url,
QWebEnginePage::NavigationType type, QWebEnginePage::NavigationType type,
bool isMainFrame) override; bool isMainFrame) override;

View file

@ -5,3 +5,4 @@ Q_LOGGING_CATEGORY(discordLog, "discord");
Q_LOGGING_CATEGORY(userscriptLog, "userscript"); Q_LOGGING_CATEGORY(userscriptLog, "userscript");
Q_LOGGING_CATEGORY(virtmicLog, "virtmic"); Q_LOGGING_CATEGORY(virtmicLog, "virtmic");
Q_LOGGING_CATEGORY(shortcutLog, "shortcut"); Q_LOGGING_CATEGORY(shortcutLog, "shortcut");
Q_LOGGING_CATEGORY(userstylesLog, "userstyles");

View file

@ -7,3 +7,4 @@ Q_DECLARE_LOGGING_CATEGORY(discordLog);
Q_DECLARE_LOGGING_CATEGORY(userscriptLog); Q_DECLARE_LOGGING_CATEGORY(userscriptLog);
Q_DECLARE_LOGGING_CATEGORY(virtmicLog); Q_DECLARE_LOGGING_CATEGORY(virtmicLog);
Q_DECLARE_LOGGING_CATEGORY(shortcutLog); Q_DECLARE_LOGGING_CATEGORY(shortcutLog);
Q_DECLARE_LOGGING_CATEGORY(userstylesLog);