Commit e9a8b35d authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Add script to search specific element

parent c224dfec
......@@ -16,15 +16,59 @@
*/
#include "testwebenginescrolladdattachment.h"
#include "webengine/webenginescript.h"
#include <KActionCollection>
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWebEngineSettings>
#include <viewer/webengine/mailwebengineview.h>
template<typename Arg, typename R, typename C>
struct InvokeWrapper {
R *receiver;
void (C::*memberFunction)(Arg);
void operator()(Arg result)
{
(receiver->*memberFunction)(result);
}
};
template<typename Arg, typename R, typename C>
InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFunction)(Arg))
{
InvokeWrapper<Arg, R, C> wrapper = {receiver, memberFunction};
return wrapper;
}
TestWebEngineScrollAddAttachment::TestWebEngineScrollAddAttachment(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *vboxLayout = new QVBoxLayout(this);
mTestWebEngine = new MessageViewer::MailWebEngineView(new KActionCollection(this), this);
mTestWebEngine->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
vboxLayout->addWidget(mTestWebEngine);
mTestWebEngine->load(QUrl(QStringLiteral("http://www.kde.org")));
QPushButton *scrollToButton = new QPushButton(QStringLiteral("Scroll to Attachment"), this);
vboxLayout->addWidget(scrollToButton);
connect(scrollToButton, &QPushButton::clicked, this, &TestWebEngineScrollAddAttachment::slotScrollToAttachment);
}
void TestWebEngineScrollAddAttachment::handleScrollToAnchor(const QVariant &result)
{
qDebug() << " result "<<result;
}
void TestWebEngineScrollAddAttachment::slotScrollToAttachment()
{
mTestWebEngine->page()->runJavaScript(MessageViewer::WebEngineScript::searchElementPosition(QStringLiteral("#footer_text")), invoke(this, &TestWebEngineScrollAddAttachment::handleScrollToAnchor));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
......
......@@ -19,12 +19,21 @@
#define TESTWEBENGINESCROLLADDATTACHMENT_H
#include <QWidget>
namespace MessageViewer
{
class MailWebEngineView;
}
class TestWebEngineScrollAddAttachment : public QWidget
{
Q_OBJECT
public:
explicit TestWebEngineScrollAddAttachment(QWidget *parent = Q_NULLPTR);
private Q_SLOTS:
void slotScrollToAttachment();
void handleScrollToAnchor(const QVariant &result);
private:
MessageViewer::MailWebEngineView *mTestWebEngine;
};
#endif // TESTWEBENGINESCROLLADDATTACHMENT_H
......@@ -15,6 +15,7 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "webenginescript.h"
#include <QDebug>
using namespace MessageViewer;
......@@ -93,3 +94,15 @@ QString WebEngineScript::findAllAnchorsAndForms()
return source;
}
QString WebEngineScript::searchElementPosition(const QString &elementStr)
{
const QString source = QString::fromLatin1("var element = document.querySelector('%1'); "
"if (element) { "
" var geometry = element.getBoundingClientRect(); "
" [(geometry.left + window.scrollX), (geometry.top + window.scrollY)]; "
"}").arg(elementStr);
qDebug()<<" source "<<source;
return source;
}
......@@ -28,6 +28,7 @@ MESSAGEVIEWER_EXPORT QString findAllImages();
MESSAGEVIEWER_EXPORT QString findAllScripts();
MESSAGEVIEWER_EXPORT QString findAllAnchors();
MESSAGEVIEWER_EXPORT QString findAllAnchorsAndForms();
MESSAGEVIEWER_EXPORT QString searchElementPosition(const QString &elementStr);
}
}
#endif // WEBENGINESCRIPT_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment