From 01b981476042d00522a2c6efaa55025776a1d10a Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:32:21 +0800 Subject: [PATCH] opt: rewrite the logic of determine the existence of audio link (#1860) * opt: rewrite the logic of determine the existence of audio link --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/audiolink.cc | 37 +++---------------------------------- src/audiolink.hh | 1 - src/dict/dsl.cc | 2 +- src/dict/epwing_book.cc | 2 +- src/dict/forvo.cc | 2 +- src/dict/gls.cc | 8 +++++--- src/dict/lingualibre.cc | 2 +- src/dict/lsa.cc | 4 ++-- src/dict/mdx.cc | 5 ++--- src/dict/mediawiki.cc | 2 +- src/dict/programs.cc | 2 +- src/dict/sounddir.cc | 4 ++-- src/dict/stardict.cc | 5 +++-- src/dict/voiceengines.cc | 2 +- src/dict/xdxf2html.cc | 4 +--- src/dict/zipsounds.cc | 4 ++-- src/pronounceengine.cc | 4 ++-- src/scripts/gd-builtin.js | 10 ---------- src/ui/articleview.cc | 39 ++++++++++++++++----------------------- src/ui/articleview.hh | 5 +++++ src/ui/mainwindow.cc | 3 ++- src/ui/scanpopup.cc | 1 + 22 files changed, 53 insertions(+), 95 deletions(-) diff --git a/src/audiolink.cc b/src/audiolink.cc index 1e71f9d5..8093ccfb 100644 --- a/src/audiolink.cc +++ b/src/audiolink.cc @@ -11,40 +11,9 @@ std::string addAudioLink( std::string const & url, std::string const & dictionar std::string addAudioLink( QString const & url, std::string const & dictionaryId ) { - if ( url.isEmpty() || url.length() < 2 ) { + if ( url.isEmpty() ) { return {}; } - GlobalBroadcaster::instance()->pronounce_engine.sendAudio( dictionaryId, url.mid( 1, url.length() - 2 ) ); - - return std::string( "" ); -} - -std::string makeAudioLinkScript( std::string const & url, std::string const & dictionaryId ) -{ - /// Convert "'" to "\'" - this char broke autoplay of audiolinks - - std::string ref; - bool escaped = false; - for ( const char ch : url ) { - if ( escaped ) { - ref += ch; - escaped = false; - continue; - } - if ( ch == '\'' ) { - ref += '\\'; - } - ref += ch; - escaped = ( ch == '\\' ); - } - - const std::string audioLinkForDict = QString::fromStdString( R"( -if(!gdAudioMap.has('%1')){ - gdAudioMap.set('%1',%2); -} -)" ) - .arg( QString::fromStdString( dictionaryId ), QString::fromStdString( url ) ) - .toStdString(); - return "gdAudioLinks.first = gdAudioLinks.first || " + ref + ";" + audioLinkForDict; + GlobalBroadcaster::instance()->pronounce_engine.sendAudio( dictionaryId, url ); + return ""; } diff --git a/src/audiolink.hh b/src/audiolink.hh index 314932a6..2d0e1bcc 100644 --- a/src/audiolink.hh +++ b/src/audiolink.hh @@ -15,6 +15,5 @@ /// The dictionary id is used to make active dictionary feature work. std::string addAudioLink( std::string const & url, std::string const & dictionaryId ); std::string addAudioLink( QString const & url, std::string const & dictionaryId ); -std::string makeAudioLinkScript( std::string const & url, std::string const & dictionaryId ); #endif diff --git a/src/dict/dsl.cc b/src/dict/dsl.cc index ee001748..42f44a4c 100644 --- a/src/dict/dsl.cc +++ b/src/dict/dsl.cc @@ -861,7 +861,7 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node ) string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; diff --git a/src/dict/epwing_book.cc b/src/dict/epwing_book.cc index f7bf1f40..31a2eae9 100644 --- a/src/dict/epwing_book.cc +++ b/src/dict/epwing_book.cc @@ -1569,7 +1569,7 @@ QByteArray EpwingBook::handleWave( EB_Hook_Code code, const unsigned int * argv url.setPath( Utils::Url::ensureLeadingSlash( name ) ); string ref = string( "\"" ) + url.toEncoded().data() + "\""; - QByteArray result = addAudioLink( ref, dictID.toUtf8().data() ).c_str(); + QByteArray result = addAudioLink( url.toEncoded(), dictID.toUtf8().data() ).c_str(); result += QByteArray( ""; diff --git a/src/dict/forvo.cc b/src/dict/forvo.cc index df862ea1..4150ca20 100644 --- a/src/dict/forvo.cc +++ b/src/dict/forvo.cc @@ -260,7 +260,7 @@ void ForvoArticleRequest::requestFinished( QNetworkReply * r ) string ref = string( "\"" ) + url.toEncoded().data() + "\""; - articleBody += addAudioLink( ref, dictionaryId ).c_str(); + articleBody += addAudioLink( url.toEncoded(), dictionaryId ).c_str(); bool isMale = ( item.namedItem( "sex" ).toElement().text().toLower() != "f" ); diff --git a/src/dict/gls.cc b/src/dict/gls.cc index 901cadee..1a3d1397 100644 --- a/src/dict/gls.cc +++ b/src/dict/gls.cc @@ -789,9 +789,11 @@ QString & GlsDictionary::filterResource( QString & article ) articleNewText += match.captured(); } else { - std::string href = "\"gdau://" + getId() + "/" + src.toUtf8().data() + "\""; - QString newTag = QString::fromUtf8( - ( addAudioLink( href, getId() ) + "" ).c_str() ); + std::string audioLink = "gdau://" + getId() + "/" + src.toUtf8().data(); + std::string href = "\"" + audioLink + "\""; + + QString newTag = QString::fromUtf8( + ( addAudioLink( audioLink, getId() ) + "" ).c_str() ); newTag += match.captured( 4 ); if ( match.captured( 4 ).indexOf( "Play)"; diff --git a/src/dict/lingualibre.cc b/src/dict/lingualibre.cc index d6446d6b..8168e284 100644 --- a/src/dict/lingualibre.cc +++ b/src/dict/lingualibre.cc @@ -357,7 +357,7 @@ void LinguaArticleRequest::requestFinished( QNetworkReply * r ) string title = pageJsonObj[ "title" ].toString().toHtmlEscaped().toStdString(); string audiolink = pageJsonObj[ "imageinfo" ].toArray().at( 0 ).toObject()[ "url" ].toString().toHtmlEscaped().toStdString(); - articleBody += addAudioLink( "\"" + audiolink + "\"", dictionaryId ); + articleBody += addAudioLink( audiolink, dictionaryId ); articleBody += R"()"; diff --git a/src/dict/lsa.cc b/src/dict/lsa.cc index fd073f41..72c878fb 100644 --- a/src/dict/lsa.cc +++ b/src/dict/lsa.cc @@ -277,7 +277,7 @@ sptr< Dictionary::DataRequest > LsaDictionary::getArticle( wstring const & word, string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; result += "" + i->second + ""; @@ -294,7 +294,7 @@ sptr< Dictionary::DataRequest > LsaDictionary::getArticle( wstring const & word, string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; result += "" + i->second + ""; diff --git a/src/dict/mdx.cc b/src/dict/mdx.cc index 46b705aa..2e2fb572 100644 --- a/src/dict/mdx.cc +++ b/src/dict/mdx.cc @@ -922,9 +922,8 @@ void MdxDictionary::replaceLinks( QString & id, QString & article ) // sounds and audio link script QString newTxt = match.captured( 1 ) + match.captured( 2 ) + "gdau://" + id + "/" + match.captured( 3 ) + match.captured( 2 ) + R"( onclick="return false;" )"; - newLink = - QString::fromUtf8( - addAudioLink( "\"gdau://" + getId() + "/" + match.captured( 3 ).toUtf8().data() + "\"", getId() ).c_str() ) + newLink = QString::fromUtf8( + addAudioLink( "gdau://" + getId() + "/" + match.captured( 3 ).toUtf8().data(), getId() ).c_str() ) + newLink.replace( match.capturedStart(), match.capturedLength(), newTxt ); } diff --git a/src/dict/mediawiki.cc b/src/dict/mediawiki.cc index 216f0df4..d5b057cb 100644 --- a/src/dict/mediawiki.cc +++ b/src/dict/mediawiki.cc @@ -608,7 +608,7 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r ) if ( ref.startsWith( "//" ) ) { ref = wikiUrl.scheme() + ":" + ref; } - auto script = addAudioLink( "\"" + ref + "\"", this->dictPtr->getId() ); + auto script = addAudioLink( ref, this->dictPtr->getId() ); QString audio_url = QString::fromStdString( script ) + "Play)"; articleNewString += audio_url; diff --git a/src/dict/programs.cc b/src/dict/programs.cc index ef04f92d..b3272b1b 100644 --- a/src/dict/programs.cc +++ b/src/dict/programs.cc @@ -95,7 +95,7 @@ ProgramsDictionary::getArticle( wstring const & word, vector< wstring > const &, string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; result += "" + Html::escape( wordUtf8 ) + ""; diff --git a/src/dict/sounddir.cc b/src/dict/sounddir.cc index 0a71de23..43572bb4 100644 --- a/src/dict/sounddir.cc +++ b/src/dict/sounddir.cc @@ -229,7 +229,7 @@ sptr< Dictionary::DataRequest > SoundDirDictionary::getArticle( wstring const & string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; result += "" + _displayName + ""; @@ -277,7 +277,7 @@ sptr< Dictionary::DataRequest > SoundDirDictionary::getArticle( wstring const & string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; result += "" + _displayName + ""; diff --git a/src/dict/stardict.cc b/src/dict/stardict.cc index f873d54a..f24e8872 100644 --- a/src/dict/stardict.cc +++ b/src/dict/stardict.cc @@ -543,8 +543,9 @@ string StardictDictionary::handleResource( char type, char const * resource, siz articleNewText += match.captured(); } else { - std::string href = "\"gdau://" + getId() + "/" + src.toUtf8().data() + "\""; - std::string newTag = addAudioLink( href, getId() ) + ""; + std::string audioLink = "gdau://" + getId() + "/" + src.toUtf8().data(); + std::string href = "\"" + audioLink + "\""; + std::string newTag = addAudioLink( audioLink, getId() ) + ""; newTag += match.captured( 4 ).toUtf8().constData(); if ( match.captured( 4 ).indexOf( "Play)"; diff --git a/src/dict/voiceengines.cc b/src/dict/voiceengines.cc index 0870f1bd..8e988f09 100644 --- a/src/dict/voiceengines.cc +++ b/src/dict/voiceengines.cc @@ -98,7 +98,7 @@ VoiceEnginesDictionary::getArticle( wstring const & word, vector< wstring > cons string encodedUrl = url.toEncoded().data(); string ref = string( "\"" ) + encodedUrl + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( encodedUrl, getId() ); result += "Play)"; result += "" + Html::escape( wordUtf8 ) + ""; diff --git a/src/dict/xdxf2html.cc b/src/dict/xdxf2html.cc index 24aaab64..9a37cb63 100644 --- a/src/dict/xdxf2html.cc +++ b/src/dict/xdxf2html.cc @@ -661,9 +661,7 @@ string convert( string const & in, el_script.setAttribute( "type", "text/javascript" ); parent.replaceChild( el_script, el ); - QDomText el_txt = dd.createTextNode( - makeAudioLinkScript( string( "\"" ) + url.toEncoded().data() + "\"", dictPtr->getId() ).c_str() ); - el_script.appendChild( el_txt ); + addAudioLink( string( "\"" ) + url.toEncoded().data() + "\"", dictPtr->getId() ); QDomElement el_span = dd.createElement( "span" ); el_span.setAttribute( "class", "xdxf_wav" ); diff --git a/src/dict/zipsounds.cc b/src/dict/zipsounds.cc index 6c33d955..9d3a1231 100644 --- a/src/dict/zipsounds.cc +++ b/src/dict/zipsounds.cc @@ -263,7 +263,7 @@ sptr< Dictionary::DataRequest > ZipSoundsDictionary::getArticle( wstring const & string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; result += "" + Html::escape( displayedName ) + ""; @@ -305,7 +305,7 @@ sptr< Dictionary::DataRequest > ZipSoundsDictionary::getArticle( wstring const & string ref = string( "\"" ) + url.toEncoded().data() + "\""; - result += addAudioLink( ref, getId() ); + result += addAudioLink( url.toEncoded(), getId() ); result += "Play)"; result += "" + Html::escape( displayedName ) + ""; diff --git a/src/pronounceengine.cc b/src/pronounceengine.cc index 83e56a30..1391de7c 100644 --- a/src/pronounceengine.cc +++ b/src/pronounceengine.cc @@ -8,7 +8,6 @@ PronounceEngine::PronounceEngine( QObject * parent ): { } - void PronounceEngine::reset() { QMutexLocker _( &mutex ); @@ -48,6 +47,7 @@ void PronounceEngine::finishDictionary( std::string dictId ) } state = PronounceState::OCCUPIED; } - emit emitAudio( dictAudioMap[ dictId ].first() ); + auto link = dictAudioMap[ dictId ].first(); + emit emitAudio( link ); } } diff --git a/src/scripts/gd-builtin.js b/src/scripts/gd-builtin.js index 530649fd..edc6913d 100644 --- a/src/scripts/gd-builtin.js +++ b/src/scripts/gd-builtin.js @@ -1,12 +1,3 @@ -// seperate from cpp code. -var gdAudioLinks = { - first: null, - current: null, -}; - -//store dictionary audio link. -var gdAudioMap = new Map(); - function gdMakeArticleActive(newId, noEvent) { const gdCurrentArticle = document.querySelector(".gdactivearticle").attributes.id; @@ -16,7 +7,6 @@ function gdMakeArticleActive(newId, noEvent) { .classList.remove("gdactivearticle"); const newFormId = "gdfrom-" + newId; document.querySelector(`#${newFormId}`).classList.add("gdactivearticle"); - gdAudioLinks.current = newId; if (!noEvent) articleview.onJsActiveArticleChanged("gdfrom-" + newId); } } diff --git a/src/ui/articleview.cc b/src/ui/articleview.cc index f1ebb2ea..cf895c6f 100644 --- a/src/ui/articleview.cc +++ b/src/ui/articleview.cc @@ -277,6 +277,16 @@ unsigned ArticleView::getCurrentGroupId() return currentGroupId; } +void ArticleView::setAudioLink( QString audioLink ) +{ + audioLink_ = audioLink; +} + +QString ArticleView::getAudioLink() const +{ + return audioLink_; +} + ArticleView::~ArticleView() { cleanupTemp(); @@ -302,6 +312,7 @@ void ArticleView::showDefinition( QString const & word, currentActiveDictIds.clear(); // first, let's stop the player audioPlayer->stop(); + audioLink_.clear(); QUrl req; Contexts contexts( contexts_ ); @@ -376,6 +387,7 @@ void ArticleView::showDefinition( QString const & word, currentActiveDictIds.clear(); // first, let's stop the player audioPlayer->stop(); + audioLink_.clear(); QUrl req; @@ -1306,33 +1318,14 @@ void ArticleView::reload() void ArticleView::hasSound( const std::function< void( bool ) > & callback ) { - webview->page()->runJavaScript( R"(if(typeof(gdAudioLinks)!="undefined") gdAudioLinks.first)", - [ callback ]( const QVariant & v ) { - bool has = false; - if ( v.type() == QVariant::String ) { - has = !v.toString().isEmpty(); - } - callback( has ); - } ); + callback( !audioLink_.isEmpty() ); } -//use webengine javascript to playsound void ArticleView::playSound() { - QString variable = R"( (function(){ var link=gdAudioMap.get(gdAudioLinks.current); - if(link==undefined){ - link=gdAudioLinks.first; - } - return link;})(); )"; - - webview->page()->runJavaScript( variable, [ this ]( const QVariant & result ) { - if ( result.typeId() == qMetaTypeId< QString >() ) { - QString soundScript = result.toString(); - if ( !soundScript.isEmpty() ) { - openLink( QUrl::fromEncoded( soundScript.toUtf8() ), webview->url() ); - } - } - } ); + if ( !audioLink_.isEmpty() ) { + playAudio( QUrl::fromEncoded( audioLink_.toUtf8() ) ); + } } void ArticleView::stopSound() diff --git a/src/ui/articleview.hh b/src/ui/articleview.hh index 3f469aed..58d5b5fa 100644 --- a/src/ui/articleview.hh +++ b/src/ui/articleview.hh @@ -71,6 +71,8 @@ class ArticleView: public QWidget //current active dictionary id; QString activeDictId; + QString audioLink_; + /// Search in results of full-text search QString firstAvailableText; QStringList uniqueMatches; @@ -99,6 +101,9 @@ public: void setCurrentGroupId( unsigned currengGrgId ); unsigned getCurrentGroupId(); + void setAudioLink( QString audioLink ); + QString getAudioLink() const; + virtual QSize minimumSizeHint() const; void clearContent(); diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index 9d66c742..572bac63 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -717,10 +717,11 @@ MainWindow::MainWindow( Config::Class & cfg_ ): &PronounceEngine::emitAudio, this, [ this ]( auto audioUrl ) { + auto view = getCurrentArticleView(); + view->setAudioLink( audioUrl ); if ( !isActiveWindow() ) { return; } - auto view = getCurrentArticleView(); if ( ( cfg.preferences.pronounceOnLoadMain ) && view != nullptr ) { view->playAudio( QUrl::fromEncoded( audioUrl.toUtf8() ) ); diff --git a/src/ui/scanpopup.cc b/src/ui/scanpopup.cc index b7f2d332..a5d61d82 100644 --- a/src/ui/scanpopup.cc +++ b/src/ui/scanpopup.cc @@ -165,6 +165,7 @@ ScanPopup::ScanPopup( QWidget * parent, &PronounceEngine::emitAudio, this, [ this ]( auto audioUrl ) { + definition->setAudioLink( audioUrl ); if ( !isActiveWindow() ) { return; }