diff --git a/src/lib/tools/scripts.cpp b/src/lib/tools/scripts.cpp index c466f7ac0..13cadc961 100644 --- a/src/lib/tools/scripts.cpp +++ b/src/lib/tools/scripts.cpp @@ -332,3 +332,19 @@ QString Scripts::getFormData(const QPointF &pos) return source.arg(pos.x()).arg(pos.y()); } + +QString Scripts::scrollToAnchor(const QString &anchor) +{ + QString source = QL1S("(function() {" + "var e = document.getElementById(\"%1\");" + "if (!e) {" + " var els = document.querySelectorAll(\"[name='%1']\");" + " if (els.length)" + " e = els[0];" + "}" + "if (e)" + " e.scrollIntoView();" + "})()"); + + return source.arg(anchor); +} diff --git a/src/lib/tools/scripts.h b/src/lib/tools/scripts.h index 0d34f7684..f2b3bd258 100644 --- a/src/lib/tools/scripts.h +++ b/src/lib/tools/scripts.h @@ -40,6 +40,7 @@ public: static QString getAllImages(); static QString getAllMetaAttributes(); static QString getFormData(const QPointF &pos); + static QString scrollToAnchor(const QString &anchor); }; #endif // SCRIPTS_H diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index 8e2f536f5..54cb72579 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -41,6 +41,7 @@ #include "ui_jsalert.h" #include "ui_jsprompt.h" #include "passwordmanager.h" +#include "scripts.h" #include @@ -98,8 +99,7 @@ WebPage::WebPage(QObject* parent) 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)); + runJavaScript(Scripts::scrollToAnchor(fragment)); } disconnect(m_contentsResizedConnection); });