From 486b8f4c7d05cb4e842e43344c86246c68fa911c Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 15 Mar 2017 13:11:27 +0100 Subject: [PATCH] Workaround QWebEnginePage not scrolling to anchors when opened in background tab Closes #2242 --- src/lib/webengine/webpage.cpp | 10 ++++++++++ src/lib/webengine/webpage.h | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index 59a4a9fe5..7ba62df0d 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -90,6 +90,16 @@ WebPage::WebPage(QObject* parent) connect(this, &QWebEnginePage::proxyAuthenticationRequired, this, [this](const QUrl &, QAuthenticator *auth, const QString &proxyHost) { mApp->networkManager()->proxyAuthentication(proxyHost, auth, view()); }); + + // Workaround QWebEnginePage not scrolling to anchors when opened in background tab + m_contentsResizedConnection = connect(this, &QWebEnginePage::contentsSizeChanged, this, [this]() { + const QString fragment = url().fragment(); + if (!fragment.isEmpty()) { + const QString src = QSL("var els = document.querySelectorAll(\"[name='%1']\"); if (els.length) els[0].scrollIntoView();"); + runJavaScript(src.arg(fragment)); + } + disconnect(m_contentsResizedConnection); + }); } WebPage::~WebPage() diff --git a/src/lib/webengine/webpage.h b/src/lib/webengine/webpage.h index dff71ba4c..9168332c2 100644 --- a/src/lib/webengine/webpage.h +++ b/src/lib/webengine/webpage.h @@ -1,6 +1,6 @@ /* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2016 David Rosca +* QupZilla - Qt web browser +* Copyright (C) 2010-2017 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -115,6 +115,8 @@ private: bool m_blockAlerts; bool m_secureStatus; bool m_adjustingScheduled; + + QMetaObject::Connection m_contentsResizedConnection; }; #endif // WEBPAGE_H