From 58ecbbc6f49e49a3b54d0de59dfed1cf8ea20add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Wed, 27 Jul 2022 14:53:16 +0200 Subject: [PATCH] Allow navigation to external links --- src/discordpage.cpp | 13 +++++++++++++ src/discordpage.h | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/discordpage.cpp b/src/discordpage.cpp index aa4d946..21aab35 100644 --- a/src/discordpage.cpp +++ b/src/discordpage.cpp @@ -26,6 +26,7 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) { settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true); settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false); + settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false); setUrl(QUrl("https://discord.com/app")); @@ -85,6 +86,18 @@ bool DiscordPage::acceptNavigationRequest(const QUrl &url, return true; }; +bool ExternalPage::acceptNavigationRequest(const QUrl &url, + QWebEnginePage::NavigationType type, + bool isMainFrame) { + QDesktopServices::openUrl(url); + deleteLater(); + return false; +} + +QWebEnginePage *DiscordPage::createWindow(QWebEnginePage::WebWindowType type) { + return new ExternalPage; +} + void DiscordPage::stopVirtmic() { if (m_virtmicProcess.state() == QProcess::Running) { qDebug() << "[virtmic] Stopping Virtmic"; diff --git a/src/discordpage.h b/src/discordpage.h index 152575a..f2c31dc 100644 --- a/src/discordpage.h +++ b/src/discordpage.h @@ -19,6 +19,7 @@ private: bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override; + QWebEnginePage *createWindow(QWebEnginePage::WebWindowType type) override; void javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, @@ -32,3 +33,15 @@ private Q_SLOTS: QWebEnginePage::Feature feature); void startStream(QString target, uint width, uint height, uint frameRate); }; + +// Will immediately get destroyed again but is needed for navigation to +// target="_blank" links, since QWebEnginePage::newWindowRequested is +// only available sinec Qt 6.3. +class ExternalPage : public QWebEnginePage { + Q_OBJECT + +private: + bool acceptNavigationRequest(const QUrl &url, + QWebEnginePage::NavigationType type, + bool isMainFrame) override; +};