single click to open external link in external browser

This commit is contained in:
yifang 2021-12-29 23:28:26 +08:00
parent 09063ecc5a
commit a1c0cfa042
4 changed files with 20 additions and 5 deletions

View file

@ -340,6 +340,7 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
QWebEngineSettings * settings = ui.definition->page()->settings(); QWebEngineSettings * settings = ui.definition->page()->settings();
settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true ); settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true ); settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::ErrorPageEnabled, false);
// Load the default blank page instantly, so there would be no flicker. // Load the default blank page instantly, so there would be no flicker.
QString contentType; QString contentType;
@ -1123,7 +1124,7 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref,
QString const & scrollTo, QString const & scrollTo,
Contexts const & contexts_ ) Contexts const & contexts_ )
{ {
qDebug() << "clicked url:" << url; qDebug() << "open link url:" << url;
Contexts contexts( contexts_ ); Contexts contexts( contexts_ );

View file

@ -1298,6 +1298,7 @@ void MainWindow::applyWebSettings()
defaultSettings->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false); defaultSettings->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
defaultSettings->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true ); defaultSettings->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true );
defaultSettings->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true ); defaultSettings->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true );
defaultSettings->setAttribute( QWebEngineSettings::WebAttribute::ErrorPageEnabled, false);
} }
void MainWindow::setupNetworkCache( int maxSize ) void MainWindow::setupNetworkCache( int maxSize )

View file

@ -26,6 +26,13 @@ inline QString rstrip(const QString& str) {
return ""; return "";
} }
inline bool isExternalLink( QUrl const & url )
{
return url.scheme() == "http" || url.scheme() == "https" ||
url.scheme() == "ftp" || url.scheme() == "mailto" ||
url.scheme() == "file";
}
inline QString escape( QString const & plain ) inline QString escape( QString const & plain )
{ {
return plain.toHtmlEscaped(); return plain.toHtmlEscaped();

View file

@ -1,13 +1,19 @@
#include "weburlrequestinterceptor.h" #include "weburlrequestinterceptor.h"
#include <QDebug> #include <QDebug>
#include "utils.hh"
WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p) WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
:QWebEngineUrlRequestInterceptor(p) :QWebEngineUrlRequestInterceptor(p)
{ {
} }
void WebUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { void WebUrlRequestInterceptor::interceptRequest(
if(QWebEngineUrlRequestInfo::NavigationTypeLink==info.navigationType ()&&info.resourceType ()==QWebEngineUrlRequestInfo::ResourceTypeMainFrame) QWebEngineUrlRequestInfo &info) {
emit linkClicked(info.requestUrl ()); if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() &&
info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
emit linkClicked(info.requestUrl());
if (Utils::isExternalLink(info.requestUrl())) {
info.block(true);
}
}
} }