Allow navigation to external links

This commit is contained in:
Malte Jürgens 2022-07-27 14:53:16 +02:00
parent 77300ed178
commit 58ecbbc6f4
No known key found for this signature in database
GPG Key ID: D29FBD5F93C0CFC3
2 changed files with 26 additions and 0 deletions

View File

@ -26,6 +26,7 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) {
settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true); settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture,
false); false);
settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
setUrl(QUrl("https://discord.com/app")); setUrl(QUrl("https://discord.com/app"));
@ -85,6 +86,18 @@ bool DiscordPage::acceptNavigationRequest(const QUrl &url,
return true; 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() { void DiscordPage::stopVirtmic() {
if (m_virtmicProcess.state() == QProcess::Running) { if (m_virtmicProcess.state() == QProcess::Running) {
qDebug() << "[virtmic] Stopping Virtmic"; qDebug() << "[virtmic] Stopping Virtmic";

View File

@ -19,6 +19,7 @@ private:
bool acceptNavigationRequest(const QUrl &url, bool acceptNavigationRequest(const QUrl &url,
QWebEnginePage::NavigationType type, QWebEnginePage::NavigationType type,
bool isMainFrame) override; bool isMainFrame) override;
QWebEnginePage *createWindow(QWebEnginePage::WebWindowType type) override;
void void
javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level, javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level,
const QString &message, int lineNumber, const QString &message, int lineNumber,
@ -32,3 +33,15 @@ private Q_SLOTS:
QWebEnginePage::Feature feature); QWebEnginePage::Feature feature);
void startStream(QString target, uint width, uint height, uint frameRate); 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;
};