allow for custom user styles
This commit is contained in:
parent
8c4fae3410
commit
c52daab420
|
@ -6,6 +6,7 @@
|
|||
#include <QApplication>
|
||||
#include <QDesktopServices>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QMessageBox>
|
||||
#include <QNetworkReply>
|
||||
#include <QTimer>
|
||||
|
@ -20,11 +21,23 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) {
|
|||
connect(this, &QWebEnginePage::featurePermissionRequested, this,
|
||||
&DiscordPage::featurePermissionRequested);
|
||||
|
||||
connect(this, &QWebEnginePage::loadStarted, [=]() {
|
||||
runJavaScript(QString("window.discordScreenaudioVersion = '%1';")
|
||||
.arg(QApplication::applicationVersion()));
|
||||
});
|
||||
setupPermissions();
|
||||
|
||||
injectFile(&DiscordPage::injectScript, "qwebchannel.js",
|
||||
":/qtwebchannel/qwebchannel.js");
|
||||
|
||||
setUrl(QUrl("https://discord.com/app"));
|
||||
|
||||
setWebChannel(new QWebChannel(this));
|
||||
webChannel()->registerObject("userscript", &m_userScript);
|
||||
|
||||
injectFile(&DiscordPage::injectScript, "userscript.js",
|
||||
":/assets/userscript.js");
|
||||
|
||||
setupUserStyles();
|
||||
}
|
||||
|
||||
void DiscordPage::setupPermissions() {
|
||||
settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, true);
|
||||
settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);
|
||||
settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent,
|
||||
|
@ -36,18 +49,21 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) {
|
|||
false);
|
||||
settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
|
||||
settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, true);
|
||||
|
||||
injectScriptFile("qwebchannel.js", ":/qtwebchannel/qwebchannel.js");
|
||||
|
||||
setUrl(QUrl("https://discord.com/app"));
|
||||
|
||||
setWebChannel(new QWebChannel(this));
|
||||
webChannel()->registerObject("userscript", &m_userScript);
|
||||
|
||||
injectScriptFile("userscript.js", ":/assets/userscript.js");
|
||||
}
|
||||
|
||||
void DiscordPage::injectScriptText(QString name, QString content) {
|
||||
void DiscordPage::setupUserStyles() {
|
||||
QString file =
|
||||
QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) +
|
||||
"/userstyles.css";
|
||||
if (QFileInfo(file).exists()) {
|
||||
qDebug(mainLog) << "Found userstyles:" << file;
|
||||
injectFile(&DiscordPage::injectStylesheet, "userstyles.js", file);
|
||||
}
|
||||
}
|
||||
|
||||
void DiscordPage::injectScript(
|
||||
QString name, QString content,
|
||||
QWebEngineScript::InjectionPoint injectionPoint) {
|
||||
qDebug(mainLog) << "Injecting " << name;
|
||||
|
||||
QWebEngineScript script;
|
||||
|
@ -55,20 +71,37 @@ void DiscordPage::injectScriptText(QString name, QString content) {
|
|||
script.setSourceCode(content);
|
||||
script.setName(name);
|
||||
script.setWorldId(QWebEngineScript::MainWorld);
|
||||
script.setInjectionPoint(QWebEngineScript::DocumentCreation);
|
||||
script.setInjectionPoint(injectionPoint);
|
||||
script.setRunsOnSubFrames(false);
|
||||
|
||||
scripts().insert(script);
|
||||
}
|
||||
|
||||
void DiscordPage::injectScriptFile(QString name, QString source) {
|
||||
void DiscordPage::injectScript(QString name, QString content) {
|
||||
injectScript(name, content, QWebEngineScript::DocumentCreation);
|
||||
}
|
||||
|
||||
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);
|
||||
)")
|
||||
.arg(name)
|
||||
.arg(content);
|
||||
injectScript(name, script, QWebEngineScript::DocumentReady);
|
||||
}
|
||||
|
||||
void DiscordPage::injectFile(void (DiscordPage::*inject)(QString, QString),
|
||||
QString name, QString source) {
|
||||
QFile file(source);
|
||||
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qFatal("Failed to load %s with error: %s", source.toLatin1().constData(),
|
||||
file.errorString().toLatin1().constData());
|
||||
} else {
|
||||
injectScriptText(name, file.readAll());
|
||||
(this->*inject)(name, file.readAll());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <QWebEngineFullScreenRequest>
|
||||
#include <QWebEnginePage>
|
||||
#include <QWebEngineScript>
|
||||
|
||||
class DiscordPage : public QWebEnginePage {
|
||||
Q_OBJECT
|
||||
|
@ -13,6 +14,8 @@ public:
|
|||
|
||||
private:
|
||||
UserScript m_userScript;
|
||||
void setupPermissions();
|
||||
void setupUserStyles();
|
||||
bool acceptNavigationRequest(const QUrl &url,
|
||||
QWebEnginePage::NavigationType type,
|
||||
bool isMainFrame) override;
|
||||
|
@ -21,8 +24,12 @@ private:
|
|||
javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level,
|
||||
const QString &message, int lineNumber,
|
||||
const QString &sourceID) override;
|
||||
void injectScriptText(QString name, QString content);
|
||||
void injectScriptFile(QString name, QString source);
|
||||
void injectScript(QString name, QString content,
|
||||
QWebEngineScript::InjectionPoint injectionPoint);
|
||||
void injectScript(QString name, QString content);
|
||||
void injectStylesheet(QString name, QString content);
|
||||
void injectFile(void (DiscordPage::*inject)(QString, QString), QString name,
|
||||
QString source);
|
||||
|
||||
private Q_SLOTS:
|
||||
void featurePermissionRequested(const QUrl &securityOrigin,
|
||||
|
|
Loading…
Reference in New Issue