2023-04-17 20:12:27 +00:00
|
|
|
#include "iframeschemehandler.hh"
|
2022-05-13 16:00:23 +00:00
|
|
|
|
|
|
|
#include <QTextCodec>
|
|
|
|
|
|
|
|
IframeSchemeHandler::IframeSchemeHandler( QObject * parent ):
|
|
|
|
QWebEngineUrlSchemeHandler( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void IframeSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
|
|
|
{
|
|
|
|
QUrl url = requestJob->requestUrl();
|
|
|
|
|
|
|
|
// website dictionary iframe url
|
2022-09-14 12:01:57 +00:00
|
|
|
url = QUrl( Utils::Url::queryItemValue( url, "url" ) );
|
2022-05-13 16:00:23 +00:00
|
|
|
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
|
|
|
|
2023-03-31 06:05:17 +00:00
|
|
|
auto finishAction = [ = ]() {
|
|
|
|
QByteArray contentType = "text/html";
|
|
|
|
QString codecName;
|
|
|
|
const auto ctHeader = reply->header( QNetworkRequest::ContentTypeHeader );
|
|
|
|
if ( ctHeader.isValid() ) {
|
|
|
|
contentType = ctHeader.toByteArray();
|
|
|
|
const auto ct = ctHeader.toString();
|
|
|
|
const auto index = ct.indexOf( "charset=" );
|
|
|
|
if ( index > -1 ) {
|
|
|
|
codecName = ct.mid( index + 8 );
|
|
|
|
}
|
|
|
|
}
|
2023-07-09 10:18:40 +00:00
|
|
|
auto buffer = new QBuffer( requestJob );
|
2022-09-14 12:01:01 +00:00
|
|
|
|
2022-05-13 16:00:23 +00:00
|
|
|
QByteArray replyData = reply->readAll();
|
|
|
|
QString articleString;
|
|
|
|
|
2022-12-18 13:30:35 +00:00
|
|
|
QTextCodec * codec = QTextCodec::codecForUtfText( replyData, QTextCodec::codecForName( codecName.toUtf8() ) );
|
|
|
|
if ( codec ) {
|
|
|
|
articleString = codec->toUnicode( replyData );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2022-12-18 13:30:35 +00:00
|
|
|
else {
|
|
|
|
articleString = QString::fromUtf8( replyData );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2022-09-14 12:01:01 +00:00
|
|
|
// Handle reply data
|
|
|
|
// 404 response may have response body.
|
|
|
|
if ( reply->error() != QNetworkReply::NoError && articleString.isEmpty() ) {
|
2022-09-15 23:54:09 +00:00
|
|
|
if ( reply->error() == QNetworkReply::ContentNotFoundError ) {
|
2022-12-18 13:30:35 +00:00
|
|
|
buffer->deleteLater();
|
2022-09-15 23:54:09 +00:00
|
|
|
//work around to fix QTBUG-106573
|
|
|
|
requestJob->redirect( url );
|
|
|
|
return;
|
|
|
|
}
|
2022-09-14 12:01:01 +00:00
|
|
|
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
|
|
|
|
|
|
|
// Change links from relative to absolute
|
|
|
|
|
|
|
|
QString root = reply->url().scheme() + "://" + reply->url().host();
|
2023-09-17 02:38:29 +00:00
|
|
|
|
2023-11-13 02:37:22 +00:00
|
|
|
if ( reply->url().port() != 80 && reply->url().port() != 443 && reply->url().port() != -1 ) {
|
2023-09-17 02:38:29 +00:00
|
|
|
root = root + ":" + QString::number( reply->url().port() );
|
|
|
|
}
|
2022-05-13 16:00:23 +00:00
|
|
|
QString base = root + reply->url().path();
|
|
|
|
|
2023-09-17 02:38:29 +00:00
|
|
|
|
2023-08-03 13:29:36 +00:00
|
|
|
QRegularExpression baseTag( R"EOF(<base\s+href=["'](.*?)["'].*?>)EOF",
|
2023-07-09 10:18:40 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption
|
|
|
|
| QRegularExpression::DotMatchesEverythingOption );
|
|
|
|
|
2023-08-03 13:29:36 +00:00
|
|
|
|
|
|
|
if ( const auto match = baseTag.match( articleString ); match.hasMatch() ) {
|
|
|
|
base = reply->url().resolved( match.captured( 1 ) ).url();
|
|
|
|
}
|
|
|
|
|
2023-07-09 10:18:40 +00:00
|
|
|
QString baseTagHtml = QString( R"(<base href="%1">)" ).arg( base );
|
|
|
|
|
|
|
|
QString depressionFocus =
|
|
|
|
R"(<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-09-02 13:27:25 +00:00
|
|
|
|
2023-07-09 10:18:40 +00:00
|
|
|
articleString.remove( baseTag );
|
2023-08-03 13:29:36 +00:00
|
|
|
|
2023-07-13 10:57:22 +00:00
|
|
|
articleString.replace( "window.location", "window.location;_window_location" );
|
2022-07-16 01:13:33 +00:00
|
|
|
|
2023-07-09 10:18:40 +00:00
|
|
|
QRegularExpression headTag( R"(<head\b.*?>)",
|
2022-08-10 11:49:31 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption
|
|
|
|
| QRegularExpression::DotMatchesEverythingOption );
|
2023-08-03 13:29:36 +00:00
|
|
|
auto match = headTag.match( articleString );
|
2023-07-09 10:18:40 +00:00
|
|
|
if ( match.hasMatch() ) {
|
2022-07-16 01:13:33 +00:00
|
|
|
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
|
|
|
|
2023-03-31 06:05:17 +00:00
|
|
|
buffer->setData( articleString.toUtf8() );
|
|
|
|
|
2023-07-09 03:24:28 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
|
2023-07-08 05:26:11 +00:00
|
|
|
requestJob->reply( "text/html; charset=utf-8", buffer );
|
2023-07-09 03:24:28 +00:00
|
|
|
#else
|
|
|
|
#if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC )
|
|
|
|
requestJob->reply( contentType, buffer );
|
|
|
|
#else
|
|
|
|
requestJob->reply( "text/html", buffer );
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-05-13 16:00:23 +00:00
|
|
|
};
|
|
|
|
connect( reply, &QNetworkReply::finished, requestJob, finishAction );
|
|
|
|
|
|
|
|
connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater );
|
|
|
|
}
|