#include "iframeschemehandler.h" #include IframeSchemeHandler::IframeSchemeHandler(QObject * parent):QWebEngineUrlSchemeHandler(parent){ } void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) { QUrl url = requestJob->requestUrl(); // website dictionary iframe url auto websiteUrl=QUrl::fromPercentEncoding( Utils::Url::queryItemValue( url, "url" ).toUtf8()); url = QUrl( websiteUrl ); QNetworkRequest request; request.setUrl( url ); request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy); QNetworkReply * reply = mgr.get( request ); auto finishAction = [ = ]() -> void { QByteArray contentType = "text/html"; QBuffer * buffer = new QBuffer( requestJob ); // Handle reply data QByteArray replyData = reply->readAll(); QString articleString; QTextCodec * codec = QTextCodec::codecForHtml( replyData, QTextCodec::codecForName( "UTF-8" ) ); articleString = codec->toUnicode( replyData ); // Change links from relative to absolute QString root = reply->url().scheme() + "://" + reply->url().host(); QString base = root + reply->url().path(); QRegularExpression baseTag( "", QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption ); QString baseTagHtml = ""; QString depressionFocus ="" "" ""; // remove existed base tag articleString.remove( baseTag ) ; QRegularExpression headTag( "", QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption ); auto match = headTag.match( articleString, 0 ); if( match.hasMatch() ) { articleString.insert( match.capturedEnd(), baseTagHtml ); articleString.insert( match.capturedEnd(), depressionFocus ); } 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 ); articleString.insert( 0, depressionFocus ); } buffer->setData(codec->fromUnicode(articleString)); requestJob->reply(contentType , buffer ); }; connect( reply, &QNetworkReply::finished, requestJob, finishAction ); connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater ); }