File indexing completed on 2025-01-26 04:57:24
0001 /* 0002 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "testwebenginescript.h" 0008 #include "webenginescript.h" 0009 0010 #include <QApplication> 0011 #include <QComboBox> 0012 #include <QHBoxLayout> 0013 #include <QPushButton> 0014 #include <QTextEdit> 0015 #include <QWebEngineSettings> 0016 0017 template<typename Arg, typename R, typename C> 0018 struct InvokeWrapper { 0019 R *receiver; 0020 void (C::*memberFunction)(Arg); 0021 void operator()(Arg result) 0022 { 0023 (receiver->*memberFunction)(result); 0024 } 0025 }; 0026 0027 template<typename Arg, typename R, typename C> 0028 0029 InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFunction)(Arg)) 0030 { 0031 InvokeWrapper<Arg, R, C> wrapper = {receiver, memberFunction}; 0032 return wrapper; 0033 } 0034 0035 TestWebEngineScriptView::TestWebEngineScriptView(QWidget *parent) 0036 : QWebEngineView(parent) 0037 { 0038 settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true); 0039 } 0040 0041 TestWebEngineScriptPage::TestWebEngineScriptPage(QObject *parent) 0042 : QWebEnginePage(parent) 0043 { 0044 settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true); 0045 } 0046 0047 void TestWebEngineScriptPage::javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level, 0048 const QString &message, 0049 int lineNumber, 0050 const QString &sourceID) 0051 { 0052 Q_UNUSED(level) 0053 Q_UNUSED(lineNumber) 0054 Q_UNUSED(sourceID) 0055 qDebug() << "JAVASCRIPT MESSAGE : " << message; 0056 // TODO improve it. 0057 Q_EMIT showConsoleMessage(message); 0058 } 0059 0060 TestWebEngineScript::TestWebEngineScript(QWidget *parent) 0061 : QWidget(parent) 0062 { 0063 auto vboxLayout = new QVBoxLayout(this); 0064 mTestWebEngine = new TestWebEngineScriptView(this); 0065 auto page = new TestWebEngineScriptPage(this); 0066 mTestWebEngine->setPage(page); 0067 vboxLayout->addWidget(mTestWebEngine); 0068 mTestWebEngine->load(QUrl(QStringLiteral("http://www.kde.org"))); 0069 0070 mTestScriptWidget = new TestScriptWidget(this); 0071 vboxLayout->addWidget(mTestScriptWidget); 0072 connect(mTestScriptWidget, &TestScriptWidget::executeScript, this, &TestWebEngineScript::slotExecuteScript); 0073 connect(page, &TestWebEngineScriptPage::showConsoleMessage, this, &TestWebEngineScript::slotShowConsoleMessage); 0074 } 0075 0076 TestWebEngineScript::~TestWebEngineScript() = default; 0077 0078 void TestWebEngineScript::slotShowConsoleMessage(const QString &msg) 0079 { 0080 Q_UNUSED(msg) 0081 } 0082 0083 void TestWebEngineScript::handleScript(const QVariant &res) 0084 { 0085 qDebug() << " res" << res; 0086 // TODO 0087 // mTestScriptWidget->setResult(); 0088 } 0089 0090 void TestWebEngineScript::slotExecuteScript() 0091 { 0092 const QString script = mTestScriptWidget->script(); 0093 if (!script.isEmpty()) { 0094 qDebug() << " script" << script; 0095 mTestWebEngine->page()->runJavaScript(script, invoke(this, &TestWebEngineScript::handleScript)); 0096 } 0097 } 0098 0099 int main(int argc, char *argv[]) 0100 { 0101 QApplication app(argc, argv); 0102 auto testWebEngine = new TestWebEngineScript; 0103 testWebEngine->show(); 0104 const int ret = app.exec(); 0105 return ret; 0106 } 0107 0108 TestScriptWidget::TestScriptWidget(QWidget *parent) 0109 : QWidget(parent) 0110 { 0111 auto layout = new QHBoxLayout(this); 0112 0113 auto vScriptLayout = new QVBoxLayout; 0114 layout->addLayout(vScriptLayout); 0115 0116 mScriptCombo = new QComboBox; 0117 vScriptLayout->addWidget(mScriptCombo); 0118 fillScriptCombo(mScriptCombo); 0119 connect(mScriptCombo, &QComboBox::currentIndexChanged, this, &TestScriptWidget::slotCurrentIndexChanged); 0120 0121 mScriptEdit = new QTextEdit; 0122 mScriptEdit->setAcceptRichText(false); 0123 vScriptLayout->addWidget(mScriptEdit); 0124 0125 auto vboxLayout = new QVBoxLayout; 0126 layout->addLayout(vboxLayout); 0127 0128 mResultEdit = new QTextEdit; 0129 mResultEdit->setReadOnly(true); 0130 vboxLayout->addWidget(mResultEdit); 0131 auto button = new QPushButton(QStringLiteral("Execute Script"), this); 0132 connect(button, &QPushButton::clicked, this, &TestScriptWidget::executeScript); 0133 vboxLayout->addWidget(button); 0134 } 0135 0136 void TestScriptWidget::slotCurrentIndexChanged(int index) 0137 { 0138 if (index != -1) { 0139 const QString str = mScriptCombo->currentData().toString(); 0140 mScriptEdit->setText(str); 0141 } 0142 } 0143 0144 void TestScriptWidget::fillScriptCombo(QComboBox *scriptCombo) 0145 { 0146 scriptCombo->addItem(QStringLiteral("find all images"), WebEngineViewer::WebEngineScript::findAllImages()); 0147 scriptCombo->addItem(QStringLiteral("find all scripts"), WebEngineViewer::WebEngineScript::findAllScripts()); 0148 scriptCombo->addItem(QStringLiteral("find all anchors"), WebEngineViewer::WebEngineScript::findAllAnchors()); 0149 scriptCombo->addItem(QStringLiteral("find all anchors and forms"), WebEngineViewer::WebEngineScript::findAllAnchorsAndForms()); 0150 scriptCombo->addItem(QStringLiteral("search element position"), WebEngineViewer::WebEngineScript::searchElementPosition(QStringLiteral("elements"))); 0151 scriptCombo->addItem(QStringLiteral("scroll to position"), WebEngineViewer::WebEngineScript::scrollToPosition(QPoint())); 0152 scriptCombo->addItem(QStringLiteral("scroll down"), WebEngineViewer::WebEngineScript::scrollDown(0)); 0153 scriptCombo->addItem(QStringLiteral("scroll up"), WebEngineViewer::WebEngineScript::scrollUp(0)); 0154 scriptCombo->addItem(QStringLiteral("scroll percentage"), WebEngineViewer::WebEngineScript::scrollPercentage(0)); 0155 scriptCombo->addItem(QStringLiteral("Test is bottom"), WebEngineViewer::WebEngineScript::isScrolledToBottom()); 0156 #if 0 0157 WebEngineViewer::WebEngineScript::setElementByIdVisible(const QString &elementStr, bool visibility); 0158 WebEngineViewer::WebEngineScript::setStyleToElement(const QString &elementStr, const QString &style); 0159 WebEngineViewer::WebEngineScript::scrollToRelativePosition(int pos); 0160 WebEngineViewer::WebEngineScript::removeStyleToElement(const QString &element); 0161 WebEngineViewer::WebEngineScript::replaceInnerHtml(const QString &field, const QString &html, bool doShow); 0162 #endif 0163 // TODO 0164 } 0165 0166 void TestScriptWidget::setResult(const QString &res) 0167 { 0168 mResultEdit->setText(res); 0169 } 0170 0171 QString TestScriptWidget::script() const 0172 { 0173 return mScriptEdit->toPlainText(); 0174 } 0175 0176 #include "moc_testwebenginescript.cpp"