opt: website dictionary render html

This commit is contained in:
xiaoyifang 2024-11-02 11:37:22 +08:00 committed by YiFang Xiao
parent 6edfd15962
commit 9e99389cc3

View file

@ -24,7 +24,6 @@ class WebSiteDictionary: public Dictionary::Class
{ {
string name; string name;
QByteArray urlTemplate; QByteArray urlTemplate;
bool experimentalIframe;
QString iconFilename; QString iconFilename;
bool inside_iframe; bool inside_iframe;
QNetworkAccessManager & netMgr; QNetworkAccessManager & netMgr;
@ -41,12 +40,8 @@ public:
name( name_ ), name( name_ ),
iconFilename( iconFilename_ ), iconFilename( iconFilename_ ),
inside_iframe( inside_iframe_ ), inside_iframe( inside_iframe_ ),
netMgr( netMgr_ ), netMgr( netMgr_ )
experimentalIframe( false )
{ {
if ( urlTemplate_.startsWith( "http://" ) || urlTemplate_.startsWith( "https://" ) ) {
experimentalIframe = true;
}
//else file:/// local dictionary file path //else file:/// local dictionary file path
urlTemplate = QUrl( urlTemplate_ ).toEncoded(); urlTemplate = QUrl( urlTemplate_ ).toEncoded();
@ -335,15 +330,13 @@ sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str,
QUrl url( urlString ); QUrl url( urlString );
GlobalBroadcaster::instance()->addWhitelist( url.host() ); GlobalBroadcaster::instance()->addWhitelist( url.host() );
QString encodeUrl; QString encodeUrl=encodeUrl = urlString;
if ( experimentalIframe ) {
encodeUrl = "ifr://localhost?url=" + QUrl::toPercentEncoding( urlString );
}
else {
encodeUrl = urlString;
}
fmt::format_to( std::back_inserter( result ), if ( GlobalBroadcaster::instance()->getPreference()->openWebSiteInNewTab ){
result += string("<div><span>this website dictionary is opened in the new tab</span></div>");
}
else{
fmt::format_to( std::back_inserter( result ),
R"(<iframe id="gdexpandframe-{}" src="{}" R"(<iframe id="gdexpandframe-{}" src="{}"
onmouseover="processIframeMouseOver('gdexpandframe-{}');" onmouseover="processIframeMouseOver('gdexpandframe-{}');"
onmouseout="processIframeMouseOut();" scrolling="no" onmouseout="processIframeMouseOut();" scrolling="no"
@ -352,7 +345,7 @@ sandbox="allow-same-origin allow-scripts allow-popups"></iframe>)",
getId(), getId(),
encodeUrl.toStdString(), encodeUrl.toStdString(),
getId() ); getId() );
}
auto dr = std::make_shared< DataRequestInstant >( true ); auto dr = std::make_shared< DataRequestInstant >( true );
dr->appendString( result ); dr->appendString( result );
return dr; return dr;