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 );
// }
qDebug()<< QString().vasprintf( msg, ap );
qDebug().noquote() << QString().vasprintf( msg, ap );
// if( logFilePtr && logFilePtr->isOpen() )
// {

View file

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

View file

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

View file

@ -55,6 +55,13 @@ inline bool isExternalLink(QUrl const &url) {
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 )
{
return plain.toHtmlEscaped();
@ -179,15 +186,20 @@ inline QString getHostBase( QUrl const & url )
{
QString host = url.host();
return getHostBase(host);
}
inline QString getHostBase( QString const & host )
{
QStringList domains = host.split( '.' );
int left = domains.size();
// Skip last <=3-letter domain name
// Skip last <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 )
--left;
// Skip another <=3-letter domain name
// Skip another <=3-letter domain name
if ( left && domains[ left - 1 ].size() <= 3 )
--left;

View file

@ -11,10 +11,16 @@ WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) {
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) {