Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: open website in seperate tabs #1896

Draft
wants to merge 15 commits into
base: staged
Choose a base branch
from
20 changes: 20 additions & 0 deletions src/article_maker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,26 @@ void ArticleRequest::altSearchFinished()

for ( const auto & activeDict : activeDicts ) {
try {
// if the dictionary is website dictionary and openinNewTab is enabled, emit a signal.
if ( GlobalBroadcaster::instance()->getPreference()->openWebsiteInNewTab ) {
if ( ( activeDict->getFeatures() | Dictionary::WebSite ) == Dictionary::WebSite ) {
//replace the word,and get the actual requested url
string url = activeDict->getProperties()[ Dictionary::Property::Url ];
if ( url.empty() ) {
continue;
}

QString requestUrl = Utils::WebSite::urlReplaceWord( QString::fromStdString( url ), word );
emit GlobalBroadcaster::instance()
-> websiteDictionarySignal( QString::fromStdString( activeDict->getName() ), requestUrl );
QStringList dictIds;
dictIds << QString::fromStdString( activeDict->getId() );
ActiveDictIds hittedWord{ group.id, word, dictIds };
emit GlobalBroadcaster::instance() -> dictionaryChanges( hittedWord );
continue;
}
}

sptr< Dictionary::DataRequest > r = activeDict->getArticle(
wordStd,
altsVector,
Expand Down
2 changes: 2 additions & 0 deletions src/common/globalbroadcaster.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ signals:
void dictionaryClear( ActiveDictIds ad );

void indexingDictionary( QString );

void websiteDictionarySignal( QString, QString );
};

#endif // GLOBAL_GLOBALBROADCASTER_H
8 changes: 8 additions & 0 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@ Class load()
( preferences.namedItem( "removeInvalidIndexOnExit" ).toElement().text() == "1" );
}

if ( !preferences.namedItem( "openWebsiteInNewTab" ).isNull() ) {
c.preferences.openWebsiteInNewTab = ( preferences.namedItem( "openWebsiteInNewTab" ).toElement().text() == "1" );
}

if ( !preferences.namedItem( "maxStringsInHistory" ).isNull() ) {
c.preferences.maxStringsInHistory = preferences.namedItem( "maxStringsInHistory" ).toElement().text().toUInt();
}
Expand Down Expand Up @@ -2111,6 +2115,10 @@ void save( Class const & c )
opt.appendChild( dd.createTextNode( c.preferences.removeInvalidIndexOnExit ? "1" : "0" ) );
preferences.appendChild( opt );

opt = dd.createElement( "openWebsiteInNewTab" );
opt.appendChild( dd.createTextNode( c.preferences.openWebsiteInNewTab ? "1" : "0" ) );
preferences.appendChild( opt );

opt = dd.createElement( "maxStringsInHistory" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.maxStringsInHistory ) ) );
preferences.appendChild( opt );
Expand Down
1 change: 1 addition & 0 deletions src/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ struct Preferences
int maxNetworkCacheSize;
bool clearNetworkCacheOnExit;
bool removeInvalidIndexOnExit = false;
bool openWebsiteInNewTab = false;

qreal zoomFactor;
qreal helpZoomFactor;
Expand Down
3 changes: 2 additions & 1 deletion src/dict/dictionary.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ enum Property {
Author,
Copyright,
Description,
Email
Email,
Url,
};

DEF_EX( Ex, "Dictionary error", std::exception )
Expand Down
37 changes: 15 additions & 22 deletions src/dict/website.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class WebSiteDictionary: public Dictionary::Class
{
string name;
QByteArray urlTemplate;
bool experimentalIframe;
QString iconFilename;
bool inside_iframe;
QNetworkAccessManager & netMgr;
Expand All @@ -41,14 +40,8 @@ class WebSiteDictionary: public Dictionary::Class
name( name_ ),
iconFilename( iconFilename_ ),
inside_iframe( inside_iframe_ ),
netMgr( netMgr_ ),
experimentalIframe( false )
netMgr( netMgr_ )
{
if ( urlTemplate_.startsWith( "http://" ) || urlTemplate_.startsWith( "https://" ) ) {
experimentalIframe = true;
}
//else file:/// local dictionary file path

urlTemplate = QUrl( urlTemplate_ ).toEncoded();
dictionaryDescription = urlTemplate_;
}
Expand All @@ -60,7 +53,9 @@ class WebSiteDictionary: public Dictionary::Class

map< Property, string > getProperties() noexcept override
{
return map< Property, string >();
map< Property, string > properties;
properties.insert( { Property::Url, urlTemplate.toStdString() } );
return properties;
}

unsigned long getArticleCount() noexcept override
Expand Down Expand Up @@ -321,7 +316,7 @@ void WebSiteArticleRequest::requestFinished( QNetworkReply * r )

sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str,
vector< wstring > const & /*alts*/,
wstring const & context,
wstring const & /*context*/,
bool /*ignoreDiacritics*/ )
{
QString urlString = Utils::WebSite::urlReplaceWord( QString( urlTemplate ), QString::fromStdU32String( str ) );
Expand All @@ -335,24 +330,22 @@ sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str,
QUrl url( urlString );
GlobalBroadcaster::instance()->addWhitelist( url.host() );

QString encodeUrl;
if ( experimentalIframe ) {
encodeUrl = "ifr://localhost?url=" + QUrl::toPercentEncoding( urlString );
const QString & encodeUrl = urlString;

if ( GlobalBroadcaster::instance()->getPreference()->openWebsiteInNewTab ) {
result += string( "<div><span>this website dictionary is opened in the new tab</span></div>" );
}
else {
encodeUrl = urlString;
}

fmt::format_to( std::back_inserter( result ),
R"(<iframe id="gdexpandframe-{}" src="{}"
fmt::format_to( std::back_inserter( result ),
R"(<iframe id="gdexpandframe-{}" src="{}"
onmouseover="processIframeMouseOver('gdexpandframe-{}');"
onmouseout="processIframeMouseOut();" scrolling="no"
style="overflow:visible; width:100%; display:block; border:none;"
sandbox="allow-same-origin allow-scripts allow-popups"></iframe>)",
getId(),
encodeUrl.toStdString(),
getId() );

getId(),
encodeUrl.toStdString(),
getId() );
}
auto dr = std::make_shared< DataRequestInstant >( true );
dr->appendString( result );
return dr;
Expand Down
Loading
Loading