8#include "webenginemanagescript.h"
9#include "webhittestresult.h"
10#include <QWebEnginePage>
12using namespace WebEngineViewer;
13template<
typename Arg,
typename R,
typename C>
15struct InvokeWrapperWebhittest {
17 void (C::*memberFunction)(Arg);
18 void operator()(Arg result)
20 (receiver->*memberFunction)(result);
24template<
typename Arg,
typename R,
typename C>
26InvokeWrapperWebhittest<Arg, R, C> invoke(R *receiver,
void (C::*memberFunction)(Arg))
28 InvokeWrapperWebhittest<Arg, R, C> wrapper = {receiver, memberFunction};
32class WebEngineViewer::WebHitTestPrivate
35 WebHitTestPrivate(
const QPoint &pos,
const QPoint &zoomedPos)
36 : m_zoomedPos(zoomedPos)
46WebHitTest::WebHitTest(QWebEnginePage *page,
const QPoint &zoomedPos,
const QPoint &pos,
QObject *parent)
48 , d(new WebHitTestPrivate(pos, zoomedPos))
50 QString source = QStringLiteral(
52 "var e = document.elementFromPoint(%1, %2);"
55 "function isMediaElement(e) {"
56 " return e.tagName.toLowerCase() == 'audio' || e.tagName.toLowerCase() == 'video';"
58 "function isEditableElement(e) {"
59 " if (e.isContentEditable)"
61 " if (e.tagName.toLowerCase() == 'input' || e.tagName.toLowerCase() == 'textarea')"
62 " return e.getAttribute('readonly') != 'readonly';"
65 "function isSelected(e) {"
66 " var selection = window.getSelection();"
67 " if (selection.type != 'Range')"
69 " return window.getSelection().containsNode(e, true);"
71 "function attributeStr(e, a) {"
72 " return e.getAttribute(a) || '';"
75 " alternateText: e.getAttribute('alt'),"
78 " contentEditable: isEditableElement(e),"
79 " contentSelected: isSelected(e),"
83 " tagName: e.tagName.toLowerCase()"
85 "var r = e.getBoundingClientRect();"
86 "res.boundingRect = [r.top, r.left, r.width, r.height];"
87 "if (e.tagName.toLowerCase() == 'img')"
88 " res.imageUrl = attributeStr(e, 'src').trim();"
89 "if (e.tagName.toLowerCase() == 'a') {"
90 " res.linkTitle = e.text;"
91 " res.linkUrl = attributeStr(e, 'href').trim();"
94 " if (res.linkTitle == '' && e.tagName.toLowerCase() == 'a')"
95 " res.linkTitle = e.text;"
96 " if (res.linkUrl == '' && e.tagName.toLowerCase() == 'a')"
97 " res.linkUrl = attributeStr(e, 'href').trim();"
98 " if (res.mediaUrl == '' && isMediaElement(e)) {"
99 " res.mediaUrl = e.currentSrc;"
100 " res.mediaPaused = e.paused;"
101 " res.mediaMuted = e.muted;"
103 " e = e.parentElement;"
108 const QString &js = source.
arg(d->m_zoomedPos.x()).
arg(d->m_zoomedPos.y());
109 d->m_pageUrl = page->url();
110 page->runJavaScript(js, WebEngineViewer::WebEngineManageScript::scriptWordId(), invoke(
this, &WebHitTest::handleHitTest));
113WebHitTest::~WebHitTest() =
default;
115void WebHitTest::handleHitTest(
const QVariant &result)
118 Q_EMIT finished(webHitResult);
122#include "moc_webhittest.cpp"
The WebHitTestResult class.
QString arg(Args &&... args) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by
doxygen 1.12.0 written
by
Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.