fix: iframe website online dictionary css does not disply correctly

This commit is contained in:
Xiao YiFang 2022-05-16 22:10:12 +08:00
parent 6ed989457d
commit b2d4342e70
5 changed files with 33 additions and 20 deletions

View file

@ -53,7 +53,7 @@ va_start(ap, msg);
// QTextCodec::setCodecForLocale( utf8Codec ); // QTextCodec::setCodecForLocale( utf8Codec );
// } // }
qDebug()<< QString().vasprintf( msg, ap ); qDebug().noquote() << QString().vasprintf( msg, ap );
// if( logFilePtr && logFilePtr->isOpen() ) // if( logFilePtr && logFilePtr->isOpen() )
// { // {

View file

@ -1,5 +1,6 @@
#include "globalbroadcaster.h" #include "globalbroadcaster.h"
#include <QGlobalStatic> #include <QGlobalStatic>
#include "utils.hh"
Q_GLOBAL_STATIC( GlobalBroadcaster, bdcaster ) Q_GLOBAL_STATIC( GlobalBroadcaster, bdcaster )
GlobalBroadcaster::GlobalBroadcaster( QObject * parent ) : QObject( parent ) GlobalBroadcaster::GlobalBroadcaster( QObject * parent ) : QObject( parent )
@ -21,6 +22,8 @@ Config::Preferences * GlobalBroadcaster::getPreference()
void GlobalBroadcaster::addWhitelist(QString url){ void GlobalBroadcaster::addWhitelist(QString url){
whitelist.push_back(url); whitelist.push_back(url);
auto baseUrl=::getHostBase(url);
whitelist.push_back(baseUrl);
} }
bool GlobalBroadcaster::existedInWhitelist(QString url){ bool GlobalBroadcaster::existedInWhitelist(QString url){

View file

@ -13,8 +13,10 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
url = QUrl( Utils::Url::queryItemValue( url, "url" ) ); url = QUrl( Utils::Url::queryItemValue( url, "url" ) );
QNetworkRequest request; QNetworkRequest request;
request.setUrl( url ); request.setUrl( url );
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy);
QNetworkReply * reply = mgr.get( request ); QNetworkReply * reply = mgr.get( request );
auto finishAction = [ = ]() -> void auto finishAction = [ = ]() -> void
{ {
// Handle reply data // Handle reply data
@ -22,11 +24,8 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
QByteArray replyData = reply->readAll(); QByteArray replyData = reply->readAll();
QString articleString; QString articleString;
QTextCodec * codec = QTextCodec::codecForHtml( replyData ); QTextCodec * codec = QTextCodec::codecForHtml( replyData, QTextCodec::codecForName( "UTF-8" ) );
if( codec ) articleString = codec->toUnicode( replyData );
articleString = codec->toUnicode( replyData );
else
articleString = QString::fromUtf8( replyData );
// Change links from relative to absolute // Change links from relative to absolute
@ -86,17 +85,10 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
articleNewString.clear(); articleNewString.clear();
} }
sptr< Dictionary::DataRequestInstant > response = new Dictionary::DataRequestInstant( true ); QBuffer * buffer = new QBuffer(requestJob);
buffer->setData(codec->fromUnicode(articleString));
auto content = articleString.toStdString(); requestJob->reply( "text/html;charset=UTF-8", buffer );
response->getData().resize( content.size() );
memcpy( &( response->getData().front() ), content.data(), content.size() );
auto contentType="text/html";
auto newReply = new ArticleResourceReply( this, request, response, contentType );
requestJob->reply( contentType, newReply );
connect( requestJob, &QObject::destroyed, newReply, &QObject::deleteLater );
}; };
connect( reply, &QNetworkReply::finished, requestJob, finishAction ); connect( reply, &QNetworkReply::finished, requestJob, finishAction );

View file

@ -55,6 +55,13 @@ inline bool isExternalLink(QUrl const &url) {
url.scheme() == "file"; url.scheme() == "file";
} }
inline bool isCssFontImage(QUrl const &url) {
auto fileName = url.fileName();
auto ext=fileName.mid(fileName.lastIndexOf("."));
QStringList extensions{".css",".woff",".woff2",".bmp" ,".jpg", ".png", ".tif",".wav", ".ogg", ".oga", ".mp3", ".mp4", ".aac", ".flac",".mid", ".wv ",".ape"} ;
return extensions.indexOf(ext)>-1;
}
inline QString escape( QString const & plain ) inline QString escape( QString const & plain )
{ {
return plain.toHtmlEscaped(); return plain.toHtmlEscaped();
@ -179,15 +186,20 @@ inline QString getHostBase( QUrl const & url )
{ {
QString host = url.host(); QString host = url.host();
return getHostBase(host);
}
inline QString getHostBase( QString const & host )
{
QStringList domains = host.split( '.' ); QStringList domains = host.split( '.' );
int left = domains.size(); int left = domains.size();
// Skip last <=3-letter domain name // Skip last <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 ) if ( left && domains[ left - 1 ].size() <= 3 )
--left; --left;
// Skip another <=3-letter domain name // Skip another <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 ) if ( left && domains[ left - 1 ].size() <= 3 )
--left; --left;

View file

@ -11,10 +11,16 @@ WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) { void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) {
if( Utils::isExternalLink( info.requestUrl() ) ) if( Utils::isExternalLink( info.requestUrl() ) )
{ {
if(!GlobalBroadcaster::instance()-> existedInWhitelist(info.requestUrl().host())) if(GlobalBroadcaster::instance()-> existedInWhitelist(info.requestUrl().host()))
{ {
info.block( true ); //whitelist url does not block
return;
} }
if(Utils::isCssFontImage(info.requestUrl())){
//let throuth the resources file.
return;
}
info.block(true);
} }
if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {