File indexing completed on 2025-01-26 04:57:25
0001 /* 0002 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "webenginescript.h" 0007 using namespace WebEngineViewer; 0008 0009 QString WebEngineScript::findAllImages() 0010 { 0011 const QString source = QStringLiteral( 0012 "(function() {" 0013 "var out = [];" 0014 "var imgs = document.getElementsByTagName('img');" 0015 "for (var i = 0; i < imgs.length; ++i) {" 0016 " var e = imgs[i];" 0017 " out.push({" 0018 " src: e.src" 0019 " });" 0020 "}" 0021 "return out;" 0022 "})()"); 0023 0024 return source; 0025 } 0026 0027 QString WebEngineScript::findAllScripts() 0028 { 0029 const QString source = QStringLiteral( 0030 "(function() {" 0031 "var out = [];" 0032 "var scripts = document.getElementsByTagName('script');" 0033 "for (var i = 0; i < scripts.length; ++i) {" 0034 " var e = scripts[i];" 0035 " out.push({" 0036 " src: e.src" 0037 " });" 0038 "}" 0039 "return out;" 0040 "})()"); 0041 0042 return source; 0043 } 0044 0045 QString WebEngineScript::findAllAnchors() 0046 { 0047 const QString source = QStringLiteral( 0048 "(function() {" 0049 "var out = [];" 0050 "var anchors = document.getElementsByTagName('a');" 0051 "for (var i = 0; i < anchors.length; ++i) {" 0052 " var r = anchors[i].getBoundingClientRect();" 0053 " out.push({" 0054 " src: anchors[i].href," 0055 " title: anchors[i].title," 0056 " boudingRect: [r.top, r.left, r.width, r.height]" 0057 " });" 0058 "}" 0059 "return out;" 0060 "})()"); 0061 0062 return source; 0063 } 0064 0065 QString WebEngineScript::findAllAnchorsAndForms() 0066 { 0067 const QString source = QStringLiteral( 0068 "(function() {" 0069 "var res = [];" 0070 "var out = [];" 0071 "var anchors = document.getElementsByTagName('a');" 0072 "for (var i = 0; i < anchors.length; ++i) {" 0073 " out.push({" 0074 " src: anchors[i].href," 0075 " title: anchors[i].title," 0076 " text: anchors[i].innerText" 0077 " });" 0078 "}" 0079 "var forms = document.getElementsByTagName('form');" 0080 "res.push({" 0081 " anchors: out," 0082 " forms: forms.length" 0083 " });" 0084 "return res;" 0085 "})()"); 0086 0087 return source; 0088 } 0089 0090 QString WebEngineScript::setElementByIdVisible(const QString &elementStr, bool visibility) 0091 { 0092 if (visibility) { 0093 const QString source = QStringLiteral( 0094 "var element = document.getElementById('%1'); " 0095 "if (element) { " 0096 " element.style.removeProperty( 'display' );" 0097 "}") 0098 .arg(elementStr); 0099 return source; 0100 } else { 0101 const QString source = QStringLiteral( 0102 "var element = document.getElementById('%1'); " 0103 "if (element) { " 0104 " element.style.display = \"none\";" 0105 "}") 0106 .arg(elementStr); 0107 return source; 0108 } 0109 } 0110 0111 QString WebEngineScript::searchElementPosition(const QString &elementStr) 0112 { 0113 const QString source = QStringLiteral( 0114 "var element = document.getElementById('%1'); " 0115 "if (element) { " 0116 " var geometry = element.getBoundingClientRect(); " 0117 " [(geometry.left + window.scrollX), (geometry.top + window.scrollY)]; " 0118 "}") 0119 .arg(elementStr); 0120 return source; 0121 } 0122 0123 static QString scrollTop() 0124 { 0125 return QStringLiteral("document.documentElement.scrollTop"); 0126 } 0127 0128 QString WebEngineScript::scrollPercentage(int percent) 0129 { 0130 const QString source = QStringLiteral("var current = ") + scrollTop() 0131 + QStringLiteral( 0132 ";" 0133 "var docElement = document.documentElement;" 0134 "var height = docElement.clientHeight;" 0135 "var newPosition = current + height * %1 /100;" 0136 "window.scrollTo(window.scrollX, newPosition);") 0137 .arg(percent); 0138 return source; 0139 } 0140 0141 QString WebEngineScript::scrollUp(int pixel) 0142 { 0143 const QString source = QStringLiteral("window.scrollBy(0, %1);").arg(-pixel); 0144 return source; 0145 } 0146 0147 QString WebEngineScript::scrollDown(int pixel) 0148 { 0149 const QString source = QStringLiteral("window.scrollBy(0, %1);").arg(pixel); 0150 return source; 0151 } 0152 0153 QString WebEngineScript::scrollToPosition(const QPoint &pos) 0154 { 0155 const QString source = QStringLiteral("window.scrollTo(%1, %2); [window.scrollX, window.scrollY];").arg(pos.x()).arg(pos.y()); 0156 return source; 0157 } 0158 0159 QString WebEngineScript::removeStyleToElement(const QString &elementStr) 0160 { 0161 const QString source = QStringLiteral( 0162 "var element = document.getElementById('%1'); " 0163 "if (element) { " 0164 " element.removeAttribute(\"style\");" 0165 "}") 0166 .arg(elementStr); 0167 return source; 0168 } 0169 0170 QString WebEngineScript::setStyleToElement(const QString &elementStr, const QString &style) 0171 { 0172 const QString source = QStringLiteral( 0173 "var element = document.getElementById('%1'); " 0174 "if (element) { " 0175 " element.style = '%2';" 0176 "}") 0177 .arg(elementStr, style); 0178 return source; 0179 } 0180 0181 QString WebEngineScript::scrollToRelativePosition(qreal pos) 0182 { 0183 const QString source = QStringLiteral("window.scrollTo(window.scrollX, %1); [window.scrollX, window.scrollY];").arg(pos); 0184 return source; 0185 } 0186 0187 QString WebEngineScript::isScrolledToBottom() 0188 { 0189 return QStringLiteral( 0190 "(function() { " 0191 "var docElement = document.documentElement;" 0192 "var viewportHeight = docElement.clientHeight;" 0193 "var isAtBottom = ") 0194 + scrollTop() 0195 + QStringLiteral( 0196 " + viewportHeight >= document.body.scrollHeight;" 0197 "return Boolean(isAtBottom); " 0198 "}());"); 0199 }