goldendict-ng/weburlrequestinterceptor.cc

59 lines
2 KiB
C++
Raw Normal View History

2021-12-11 16:34:37 +00:00
#include "weburlrequestinterceptor.h"
#include <QDebug>
#include "utils.hh"
#include "globalbroadcaster.h"
2021-12-11 16:34:37 +00:00
2023-04-01 01:15:07 +00:00
WebUrlRequestInterceptor::WebUrlRequestInterceptor( QObject * p ):
QWebEngineUrlRequestInterceptor( p )
2021-12-11 16:34:37 +00:00
{
}
2023-04-01 01:15:07 +00:00
void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo & info )
{
info.setHttpHeader( "origin", "*" );
info.setHttpHeader( "referer", info.requestUrl().url().toUtf8() );
if ( GlobalBroadcaster::instance()->getPreference()->disallowContentFromOtherSites
&& Utils::isExternalLink( info.requestUrl() ) ) {
//file:// link ,pass
2023-04-01 01:15:07 +00:00
if ( info.requestUrl().scheme() == "file" ) {
return;
}
2022-10-15 05:58:24 +00:00
auto hostBase = Utils::Url::getHostBase( info.requestUrl().host() );
2023-04-01 01:15:07 +00:00
if ( GlobalBroadcaster::instance()->existedInWhitelist( hostBase ) ) {
//whitelist url does not block
return;
}
2023-04-01 01:15:07 +00:00
if ( info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage
|| info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeFontResource
|| info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeStylesheet
|| info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMedia
|| Utils::isHtmlResources( info.requestUrl() ) ) {
//let throuth the resources file.
return;
}
// block external links
{
info.block( true );
return;
}
}
2021-12-31 14:11:55 +00:00
if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
//workaround to fix devtool "Switch devtool to chinese" interface was blocked.
if( info.requestUrl().scheme() == "devtools" )
{
return;
}
emit linkClicked( info.requestUrl() );
info.block(true);
}
//window.location=audio link
if( Utils::Url::isAudioUrl(info.requestUrl()) && info.navigationType()==QWebEngineUrlRequestInfo::NavigationTypeRedirect )
{
qDebug() << "blocked audio url from page redirect" << info.requestUrl().url();
info.block( true );
}
2021-12-11 16:34:37 +00:00
}