2022-05-13 16:00:23 +00:00
# include "iframeschemehandler.h"
# include <QTextCodec>
IframeSchemeHandler : : IframeSchemeHandler ( QObject * parent ) : QWebEngineUrlSchemeHandler ( parent ) {
}
void IframeSchemeHandler : : requestStarted ( QWebEngineUrlRequestJob * requestJob )
{
QUrl url = requestJob - > requestUrl ( ) ;
// website dictionary iframe url
url = QUrl ( Utils : : Url : : queryItemValue ( url , " url " ) ) ;
QNetworkRequest request ;
request . setUrl ( url ) ;
2022-05-16 14:10:12 +00:00
request . setAttribute ( QNetworkRequest : : RedirectPolicyAttribute , QNetworkRequest : : RedirectPolicy : : NoLessSafeRedirectPolicy ) ;
2022-05-13 16:00:23 +00:00
QNetworkReply * reply = mgr . get ( request ) ;
2022-05-16 14:10:12 +00:00
2022-05-13 16:00:23 +00:00
auto finishAction = [ = ] ( ) - > void
{
2022-07-09 02:37:40 +00:00
QByteArray contentType = " text/html;charset=UTF-8 " ;
2022-05-13 16:00:23 +00:00
2022-07-09 02:37:40 +00:00
QBuffer * buffer = new QBuffer ( requestJob ) ;
// Handle reply data
if ( reply - > error ( ) ! = QNetworkReply : : NoError )
{
QString emptyHtml = QString ( " <html><body>%1</body></html> " ) . arg ( reply - > errorString ( ) ) ;
buffer - > setData ( emptyHtml . toUtf8 ( ) ) ;
requestJob - > reply ( contentType , buffer ) ;
return ;
}
2022-05-13 16:00:23 +00:00
QByteArray replyData = reply - > readAll ( ) ;
QString articleString ;
2022-05-16 14:10:12 +00:00
QTextCodec * codec = QTextCodec : : codecForHtml ( replyData , QTextCodec : : codecForName ( " UTF-8 " ) ) ;
articleString = codec - > toUnicode ( replyData ) ;
2022-05-13 16:00:23 +00:00
// Change links from relative to absolute
QString root = reply - > url ( ) . scheme ( ) + " :// " + reply - > url ( ) . host ( ) ;
QString base = root + reply - > url ( ) . path ( ) ;
2022-07-16 01:13:33 +00:00
QRegularExpression baseTag ( " <base \\ s+.*?> " ,
2022-07-08 14:14:15 +00:00
QRegularExpression : : CaseInsensitiveOption | QRegularExpression : : DotMatchesEverythingOption ) ;
QString baseTagHtml = " <base href= \" " + base + " \" > " ;
2022-09-02 13:27:25 +00:00
2022-09-04 11:03:27 +00:00
QString depressionFocus = " <script type= \" application/javascript \" > HTMLElement.prototype.focus=function(){console.log( \" focus() has been disabled. \" );}</script> "
" <script type= \" text/javascript \" src= \" qrc:///scripts/iframeResizer.contentWindow.min.js \" ></script> "
" <script type= \" text/javascript \" src= \" qrc:///scripts/iframe-defer.js \" ></script> " ;
2022-07-08 14:14:15 +00:00
// remove existed base tag
2022-07-16 01:13:33 +00:00
articleString . remove ( baseTag ) ;
2022-07-17 10:22:39 +00:00
QRegularExpression headTag ( " <head \\ b.*?> " ,
2022-08-10 11:49:31 +00:00
QRegularExpression : : CaseInsensitiveOption
| QRegularExpression : : DotMatchesEverythingOption ) ;
2022-07-16 01:13:33 +00:00
auto match = headTag . match ( articleString , 0 ) ;
if ( match . hasMatch ( ) )
{
articleString . insert ( match . capturedEnd ( ) , baseTagHtml ) ;
2022-09-02 13:27:25 +00:00
articleString . insert ( match . capturedEnd ( ) , depressionFocus ) ;
2022-07-16 01:13:33 +00:00
}
2022-08-10 11:49:31 +00:00
else
{
// the html contain no head element
// just insert at the beginning of the html ,and leave it at the mercy of browser(chrome webengine)
articleString . insert ( 0 , baseTagHtml ) ;
2022-09-02 13:27:25 +00:00
articleString . insert ( 0 , depressionFocus ) ;
2022-08-10 11:49:31 +00:00
}
2022-05-13 16:00:23 +00:00
2022-07-09 02:37:40 +00:00
buffer - > setData ( codec - > fromUnicode ( articleString ) ) ;
2022-05-17 13:02:49 +00:00
requestJob - > reply ( contentType , buffer ) ;
2022-05-13 16:00:23 +00:00
} ;
connect ( reply , & QNetworkReply : : finished , requestJob , finishAction ) ;
connect ( requestJob , & QObject : : destroyed , reply , & QObject : : deleteLater ) ;
}