opt:use acceptNavigationRequest instead of weburlinterceptor

This commit is contained in:
Xiao YiFang 2022-05-18 22:31:47 +08:00
parent 459b684e5b
commit a7ba9e4b36
10 changed files with 45 additions and 23 deletions

View file

@ -262,6 +262,7 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, Au
connect(ui.definition, SIGNAL(loadProgress(int)), this, connect(ui.definition, SIGNAL(loadProgress(int)), this,
SLOT(loadProgress(int))); SLOT(loadProgress(int)));
connect( ui.definition, SIGNAL( linkClicked( QUrl ) ), this, SLOT( linkClicked( QUrl ) ) );
connect( ui.definition->page(), SIGNAL( titleChanged( QString ) ), connect( ui.definition->page(), SIGNAL( titleChanged( QString ) ),
this, SLOT( handleTitleChanged( QString ) ) ); this, SLOT( handleTitleChanged( QString ) ) );

15
articlewebpage.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "articlewebpage.h"
ArticleWebPage::ArticleWebPage(QObject *parent)
: QWebEnginePage{parent}
{
}
bool ArticleWebPage::acceptNavigationRequest( const QUrl & url, NavigationType type, bool isMainFrame )
{
if( type == QWebEnginePage::NavigationTypeLinkClicked )
{
emit linkClicked( url );
return true;
}
return QWebEnginePage::acceptNavigationRequest( url, type, isMainFrame );
}

17
articlewebpage.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef ARTICLEWEBPAGE_H
#define ARTICLEWEBPAGE_H
#include <QWebEnginePage>
class ArticleWebPage : public QWebEnginePage
{
Q_OBJECT
public:
explicit ArticleWebPage( QObject * parent = nullptr );
signals:
void linkClicked( const QUrl & url );
protected:
virtual bool acceptNavigationRequest( const QUrl & url, NavigationType type, bool isMainFrame );
};
#endif // ARTICLEWEBPAGE_H

View file

@ -1,6 +1,7 @@
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org> /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "articlewebpage.h"
#include "articlewebview.hh" #include "articlewebview.hh"
#include <QMouseEvent> #include <QMouseEvent>
#include <QWebEngineView> #include <QWebEngineView>
@ -18,6 +19,9 @@ ArticleWebView::ArticleWebView( QWidget *parent ):
midButtonPressed( false ), midButtonPressed( false ),
selectionBySingleClick( false ) selectionBySingleClick( false )
{ {
auto page = new ArticleWebPage( this );
connect( page, &ArticleWebPage::linkClicked, this, &ArticleWebView::linkClicked );
this->setPage( page );
} }
ArticleWebView::~ArticleWebView() ArticleWebView::~ArticleWebView()

View file

@ -47,6 +47,8 @@ public:
/// word, which gets selected by the view in response to double-click. /// word, which gets selected by the view in response to double-click.
void doubleClicked( QPoint pos ); void doubleClicked( QPoint pos );
void linkClicked( const QUrl & url );
protected: protected:
QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); QWebEngineView *createWindow(QWebEnginePage::WebWindowType type);
bool event( QEvent * event ); bool event( QEvent * event );

View file

@ -225,6 +225,7 @@ DEFINES += PROGRAM_VERSION=\\\"$$VERSION\\\"
# Input # Input
HEADERS += folding.hh \ HEADERS += folding.hh \
article_inspect.h \ article_inspect.h \
articlewebpage.h \
globalbroadcaster.h \ globalbroadcaster.h \
iframeschemehandler.h \ iframeschemehandler.h \
inc_case_folding.hh \ inc_case_folding.hh \
@ -365,6 +366,7 @@ FORMS += groups.ui \
SOURCES += folding.cc \ SOURCES += folding.cc \
article_inspect.cpp \ article_inspect.cpp \
articlewebpage.cpp \
globalbroadcaster.cpp \ globalbroadcaster.cpp \
iframeschemehandler.cpp \ iframeschemehandler.cpp \
main.cc \ main.cc \

View file

@ -162,7 +162,6 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
wuri = new WebUrlRequestInterceptor(); wuri = new WebUrlRequestInterceptor();
QWebEngineProfile::defaultProfile()->setUrlRequestInterceptor( wuri ); QWebEngineProfile::defaultProfile()->setUrlRequestInterceptor( wuri );
connect( wuri, &WebUrlRequestInterceptor::linkClicked, this, &MainWindow::viewLinkClicked );
if(!cfg.preferences.hideGoldenDictHeader){ if(!cfg.preferences.hideGoldenDictHeader){
QWebEngineProfile::defaultProfile()->setHttpUserAgent(QWebEngineProfile::defaultProfile()->httpUserAgent()+" GoldenDict/webengine"); QWebEngineProfile::defaultProfile()->setHttpUserAgent(QWebEngineProfile::defaultProfile()->httpUserAgent()+" GoldenDict/webengine");
@ -3600,21 +3599,6 @@ void MainWindow::unzoom()
applyZoomFactor(); applyZoomFactor();
} }
void MainWindow::viewLinkClicked( const QUrl & url )
{
if( scanPopup.get() && scanPopup->isActiveWindow() )
{
QString word = Utils::Url::getWordFromUrl( url );
if( !word.isEmpty() )
{
scanPopup->translateWord( word );
return;
}
}
ArticleView * view = getCurrentArticleView();
view->linkClicked( url );
}
void MainWindow::applyZoomFactor() void MainWindow::applyZoomFactor()
{ {
// Always call this function synchronously to potentially disable a zoom action, // Always call this function synchronously to potentially disable a zoom action,

View file

@ -344,8 +344,6 @@ private slots:
void zoomout(); void zoomout();
void unzoom(); void unzoom();
void viewLinkClicked( const QUrl & url );
void scaleArticlesByCurrentZoomFactor(); void scaleArticlesByCurrentZoomFactor();
void doWordsZoomIn(); void doWordsZoomIn();

View file

@ -39,7 +39,7 @@ void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info)
{ {
return; return;
} }
emit linkClicked( info.requestUrl() ); // emit linkClicked( info.requestUrl() );
info.block(true); // info.block(true);
} }
} }

View file

@ -11,7 +11,6 @@ public:
void interceptRequest(QWebEngineUrlRequestInfo &info); void interceptRequest(QWebEngineUrlRequestInfo &info);
signals: signals:
void linkClicked( const QUrl & url ); void linkClicked( const QUrl & url );
}; };
#endif // WEBURLREQUESTINTERCEPTOR_H #endif // WEBURLREQUESTINTERCEPTOR_H