mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-27 15:24:05 +00:00
ac8e79358c
* fix: website dictionary base tag * [autofix.ci] apply automated fixes * fix: request header origin --------- Co-authored-by: YiFang Xiao <yifang.xiao@noreply.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
58 lines
2 KiB
C++
58 lines
2 KiB
C++
#include "weburlrequestinterceptor.hh"
|
|
#include <QDebug>
|
|
#include "utils.hh"
|
|
#include "globalbroadcaster.hh"
|
|
|
|
WebUrlRequestInterceptor::WebUrlRequestInterceptor( QObject * p ):
|
|
QWebEngineUrlRequestInterceptor( p )
|
|
{
|
|
}
|
|
void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo & info )
|
|
{
|
|
info.setHttpHeader( "origin", info.requestUrl().url().toUtf8() );
|
|
info.setHttpHeader( "referer", info.requestUrl().url().toUtf8() );
|
|
if ( GlobalBroadcaster::instance()->getPreference()->disallowContentFromOtherSites
|
|
&& Utils::isExternalLink( info.requestUrl() ) ) {
|
|
//file:// link ,pass
|
|
if ( info.requestUrl().scheme() == "file" ) {
|
|
return;
|
|
}
|
|
auto hostBase = Utils::Url::getHostBase( info.requestUrl().host() );
|
|
if ( GlobalBroadcaster::instance()->existedInWhitelist( hostBase ) ) {
|
|
//whitelist url does not block
|
|
return;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
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 );
|
|
}
|
|
}
|