Messagelib

webenginescript.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "webenginescript.h"
7using namespace WebEngineViewer;
8
9QString WebEngineScript::findAllImages()
10{
11 const QString source = QStringLiteral(
12 "(function() {"
13 "var out = [];"
14 "var imgs = document.getElementsByTagName('img');"
15 "for (var i = 0; i < imgs.length; ++i) {"
16 " var e = imgs[i];"
17 " out.push({"
18 " src: e.src"
19 " });"
20 "}"
21 "return out;"
22 "})()");
23
24 return source;
25}
26
27QString WebEngineScript::findAllScripts()
28{
29 const QString source = QStringLiteral(
30 "(function() {"
31 "var out = [];"
32 "var scripts = document.getElementsByTagName('script');"
33 "for (var i = 0; i < scripts.length; ++i) {"
34 " var e = scripts[i];"
35 " out.push({"
36 " src: e.src"
37 " });"
38 "}"
39 "return out;"
40 "})()");
41
42 return source;
43}
44
45QString WebEngineScript::findAllAnchors()
46{
47 const QString source = QStringLiteral(
48 "(function() {"
49 "var out = [];"
50 "var anchors = document.getElementsByTagName('a');"
51 "for (var i = 0; i < anchors.length; ++i) {"
52 " var r = anchors[i].getBoundingClientRect();"
53 " out.push({"
54 " src: anchors[i].href,"
55 " title: anchors[i].title,"
56 " boudingRect: [r.top, r.left, r.width, r.height]"
57 " });"
58 "}"
59 "return out;"
60 "})()");
61
62 return source;
63}
64
65QString WebEngineScript::findAllAnchorsAndForms()
66{
67 const QString source = QStringLiteral(
68 "(function() {"
69 "var res = [];"
70 "var out = [];"
71 "var anchors = document.getElementsByTagName('a');"
72 "for (var i = 0; i < anchors.length; ++i) {"
73 " out.push({"
74 " src: anchors[i].href,"
75 " title: anchors[i].title,"
76 " text: anchors[i].innerText"
77 " });"
78 "}"
79 "var forms = document.getElementsByTagName('form');"
80 "res.push({"
81 " anchors: out,"
82 " forms: forms.length"
83 " });"
84 "return res;"
85 "})()");
86
87 return source;
88}
89
90QString WebEngineScript::setElementByIdVisible(const QString &elementStr, bool visibility)
91{
92 if (visibility) {
93 const QString source = QStringLiteral(
94 "var element = document.getElementById('%1'); "
95 "if (element) { "
96 " element.style.removeProperty( 'display' );"
97 "}")
98 .arg(elementStr);
99 return source;
100 } else {
101 const QString source = QStringLiteral(
102 "var element = document.getElementById('%1'); "
103 "if (element) { "
104 " element.style.display = \"none\";"
105 "}")
106 .arg(elementStr);
107 return source;
108 }
109}
110
111QString WebEngineScript::searchElementPosition(const QString &elementStr)
112{
113 const QString source = QStringLiteral(
114 "var element = document.getElementById('%1'); "
115 "if (element) { "
116 " var geometry = element.getBoundingClientRect(); "
117 " [(geometry.left + window.scrollX), (geometry.top + window.scrollY)]; "
118 "}")
119 .arg(elementStr);
120 return source;
121}
122
123static QString scrollTop()
124{
125 return QStringLiteral("document.documentElement.scrollTop");
126}
127
128QString WebEngineScript::scrollPercentage(int percent)
129{
130 const QString source = QStringLiteral("var current = ") + scrollTop()
131 + QStringLiteral(
132 ";"
133 "var docElement = document.documentElement;"
134 "var height = docElement.clientHeight;"
135 "var newPosition = current + height * %1 /100;"
136 "window.scrollTo(window.scrollX, newPosition);")
137 .arg(percent);
138 return source;
139}
140
141QString WebEngineScript::scrollUp(int pixel)
142{
143 const QString source = QStringLiteral("window.scrollBy(0, %1);").arg(-pixel);
144 return source;
145}
146
147QString WebEngineScript::scrollDown(int pixel)
148{
149 const QString source = QStringLiteral("window.scrollBy(0, %1);").arg(pixel);
150 return source;
151}
152
153QString WebEngineScript::scrollToPosition(const QPoint &pos)
154{
155 const QString source = QStringLiteral("window.scrollTo(%1, %2); [window.scrollX, window.scrollY];").arg(pos.x()).arg(pos.y());
156 return source;
157}
158
159QString WebEngineScript::removeStyleToElement(const QString &elementStr)
160{
161 const QString source = QStringLiteral(
162 "var element = document.getElementById('%1'); "
163 "if (element) { "
164 " element.removeAttribute(\"style\");"
165 "}")
166 .arg(elementStr);
167 return source;
168}
169
170QString WebEngineScript::setStyleToElement(const QString &elementStr, const QString &style)
171{
172 const QString source = QStringLiteral(
173 "var element = document.getElementById('%1'); "
174 "if (element) { "
175 " element.style = '%2';"
176 "}")
177 .arg(elementStr, style);
178 return source;
179}
180
181QString WebEngineScript::scrollToRelativePosition(qreal pos)
182{
183 const QString source = QStringLiteral("window.scrollTo(window.scrollX, %1); [window.scrollX, window.scrollY];").arg(pos);
184 return source;
185}
186
187QString WebEngineScript::isScrolledToBottom()
188{
189 return QStringLiteral(
190 "(function() { "
191 "var docElement = document.documentElement;"
192 "var viewportHeight = docElement.clientHeight;"
193 "var isAtBottom = ")
194 + scrollTop()
195 + QStringLiteral(
196 " + viewportHeight >= document.body.scrollHeight;"
197 "return Boolean(isAtBottom); "
198 "}());");
199}
int x() const const
int y() const const
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.