Skip to content

Commit

Permalink
WebEngine-specific: share SelectWordBySingleClick script
Browse files Browse the repository at this point in the history
The selectWordBySingleClick preference value is common to all pages.
Instead of inserting and updating a script that defines the
gdSelectWordBySingleClick JavaScript variable in each article web page's
script collection, insert and update it in the shared web engine
profile's script collection.

Name the shared script "ProfilePreferences" to facilitate extending it
by injecting other shared preferences in the future.
  • Loading branch information
vedgy committed Dec 7, 2022
1 parent a64d1bb commit d725e2a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
52 changes: 33 additions & 19 deletions articleview.cc
Expand Up @@ -32,6 +32,7 @@
#include <QWebEngineContextMenuData>
#include <QWebEngineFindTextResult>
#include <QWebEngineHistory>
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>

Expand Down Expand Up @@ -352,20 +353,25 @@ QString variableDeclarationFromAssignmentScript( QString const & assignmentScrip
return QLatin1String( "let " ) + assignmentScript;
}

QString selectWordBySingleClickScriptName()
QString profilePreferencesScriptName()
{
return QStringLiteral( "SelectWordBySingleClick" );
return QStringLiteral( "ProfilePreferences" );
}

QString selectWordBySingleClickAssignmentScript( bool selectWordBySingleClick )
{
return QLatin1String( "gdSelectWordBySingleClick = %1;" ).arg( javaScriptBool( selectWordBySingleClick ) );
}

QWebEngineScript createSelectWordBySingleClickScript()
bool profilePreferencesChanged( Config::Preferences const & oldPreferences, Config::Preferences const & newPreferences )
{
return createScript( selectWordBySingleClickScriptName(),
variableDeclarationFromAssignmentScript( selectWordBySingleClickAssignmentScript( false ) ) );
return oldPreferences.selectWordBySingleClick != newPreferences.selectWordBySingleClick;
}

QString profilePreferencesScriptSourceCode( Config::Preferences const & preferences )
{
return variableDeclarationFromAssignmentScript( selectWordBySingleClickAssignmentScript(
preferences.selectWordBySingleClick ) );
}

/// QUrl::StripTrailingSlash does not remove a slash if it is the only character in the path.
Expand Down Expand Up @@ -545,7 +551,6 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
webChannel->registerObject( QStringLiteral( "gdArticleView" ), jsProxy );

webPage->scripts().insert( createPageReloadingScript() );
webPage->scripts().insert( createSelectWordBySingleClickScript() );

connect( webPage, &QWebEnginePage::findTextFinished, this, &ArticleView::findTextFinished );

Expand Down Expand Up @@ -1226,10 +1231,9 @@ void ArticleView::updateCurrentArticleFromCurrentFrame( QWebFrame * frame )
#endif // USE_QTWEBKIT

#ifndef USE_QTWEBKIT
void ArticleView::updateSourceCodeOfInjectedScript( QString const & name, QString const & sourceCode )
void ArticleView::updateSourceCodeOfInjectedScript( QWebEngineScriptCollection & scripts,
QString const & name, QString const & sourceCode )
{
auto & scripts = ui.definition->page()->scripts();

auto script = scripts.findScript( name );
Q_ASSERT( !script.isNull() );

Expand All @@ -1240,17 +1244,9 @@ void ArticleView::updateSourceCodeOfInjectedScript( QString const & name, QStrin

void ArticleView::updateInjectedPageReloadingScript( bool scrollToCurrentArticle )
{
updateSourceCodeOfInjectedScript( pageReloadingScriptName(),
updateSourceCodeOfInjectedScript( ui.definition->page()->scripts(), pageReloadingScriptName(),
pageReloadingScriptSourceCode( currentArticle, scrollToCurrentArticle ) );
}

void ArticleView::updateInjectedSelectWordBySingleClickScript( bool selectWordBySingleClick )
{
QString const assignmentScript = selectWordBySingleClickAssignmentScript( selectWordBySingleClick );
ui.definition->page()->runJavaScript( assignmentScript );
updateSourceCodeOfInjectedScript( selectWordBySingleClickScriptName(),
variableDeclarationFromAssignmentScript( assignmentScript ) );
}
#endif

void ArticleView::saveHistoryUserData()
Expand Down Expand Up @@ -2211,6 +2207,24 @@ bool ArticleView::canGoForward() const
return ui.definition->history()->canGoForward();
}

#ifndef USE_QTWEBKIT
void ArticleView::initProfilePreferences( QWebEngineProfile & profile, Config::Preferences const & preferences )
{
Q_ASSERT( profile.scripts()->findScript( profilePreferencesScriptName() ).isNull() );
auto const script = createScript( profilePreferencesScriptName(), profilePreferencesScriptSourceCode( preferences ) );
profile.scripts()->insert( script );
}

void ArticleView::updateProfilePreferences( QWebEngineProfile & profile, Config::Preferences const & oldPreferences,
Config::Preferences const & newPreferences )
{
if( !profilePreferencesChanged( oldPreferences, newPreferences ) )
return;
updateSourceCodeOfInjectedScript( *profile.scripts(), profilePreferencesScriptName(),
profilePreferencesScriptSourceCode( newPreferences ) );
}
#endif

void ArticleView::setSelectionBySingleClick( bool set )
{
#ifdef USE_QTWEBKIT
Expand All @@ -2224,7 +2238,7 @@ void ArticleView::setSelectionBySingleClick( bool set )
else
lastLeftMouseButtonPressEvent.reset();

updateInjectedSelectWordBySingleClickScript( set );
ui.definition->page()->runJavaScript( selectWordBySingleClickAssignmentScript( set ) );
#endif
}

Expand Down
13 changes: 9 additions & 4 deletions articleview.hh
Expand Up @@ -25,6 +25,7 @@

class QWebEngineFindTextResult;
class QWebEngineProfile;
class QWebEngineScriptCollection;
#endif

class ArticleViewJsProxy;
Expand Down Expand Up @@ -204,6 +205,12 @@ public:
bool canGoBack() const;
bool canGoForward() const;

#ifndef USE_QTWEBKIT
static void initProfilePreferences( QWebEngineProfile & profile, Config::Preferences const & preferences );
static void updateProfilePreferences( QWebEngineProfile & profile, Config::Preferences const & oldPreferences,
Config::Preferences const & newPreferences );
#endif

/// Called when preference changes
void setSelectionBySingleClick( bool set );

Expand Down Expand Up @@ -478,14 +485,12 @@ private:
#ifdef USE_QTWEBKIT
void initCurrentArticleAndScroll();
#else
void updateSourceCodeOfInjectedScript( QString const & name, QString const & sourceCode );
static void updateSourceCodeOfInjectedScript( QWebEngineScriptCollection & scripts,
QString const & name, QString const & sourceCode );

/// Injects into JavaScript the current article ID and whether to scroll to it when reloading page.
/// Should be called before an action that can be interpreted as page reloading by JavaScript code.
void updateInjectedPageReloadingScript( bool scrollToCurrentArticle );

/// Updates selectWordBySingleClick option value on the current page and injects the new value into future pages.
void updateInjectedSelectWordBySingleClickScript( bool selectWordBySingleClick );
#endif

/// Saves current article and scroll position for the current history item.
Expand Down
5 changes: 5 additions & 0 deletions mainwindow.cc
Expand Up @@ -899,6 +899,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
setWebEngineProfilePaths( *webEngineProfile );
}
setupWebEngineProfile( *webEngineProfile, articleNetMgr );
ArticleView::initProfilePreferences( *webEngineProfile, cfg.preferences );
#endif

applyWebSettings();
Expand Down Expand Up @@ -2404,6 +2405,10 @@ void MainWindow::editPreferences()
if( cfg.preferences.helpLanguage != p.helpLanguage )
closeGDHelp();

#ifndef USE_QTWEBKIT
ArticleView::updateProfilePreferences( *webEngineProfile, cfg.preferences, p );
#endif

for( int x = 0; x < ui.tabWidget->count(); ++x )
{
ArticleView & view =
Expand Down

0 comments on commit d725e2a

Please sign in to comment.