File: weburlrequestinterceptor.cpp

package info (click to toggle)
goldendict-webengine 23.02.05-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,148 kB
  • sloc: cpp: 58,537; javascript: 9,942; ansic: 9,242; xml: 41; makefile: 15; sh: 9
file content (57 lines) | stat: -rw-r--r-- 1,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "weburlrequestinterceptor.h"
#include <QDebug>
#include "utils.hh"
#include "globalbroadcaster.h"

WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
  :QWebEngineUrlRequestInterceptor(p)
{

}
void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) {
  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 );
  }
}