javascript - Qt5-QML : automatic authentication username and password with on a third party device

标签 javascript qt qml qt5 qwebengineview

在理解了如何将 JavaScript 函数集成到 Qt-QML 之后,我现在正尝试将我在 my previous post 中学到的知识应用到我实验室中的一个真实世界的小例子中。

我正在尝试使用实验室中的第三方设备通过 Wi-Fi 访问我的机器人。

我有一个设备提供的登录表单,如下面的打印屏幕所示。地址是,例如,https://123.456.789.123:7878(这是我编的,但这是为了理解这个想法):

log-in

使用 检查器工具 浏览 html 页面后,我到达了 log in 按钮类,如下所示:

inspector-tool

问题 我现在正尝试自动填充log-in 的密码并自动访问它。登录后我应该可以看到下面的页面,但我没有。

after-logging-in

我应该提一下,我重用了 Quick Nanao Browser Example,因为它在需要绕过需要认证的窗口时效果很好。

例子

我在 Quick Nanao Browser Example 上做了很多工作,以使其更简短,并准确地关注问题所在以及我如何尝试解决它。​​

我使用的代码片段如下:

main.cpp

#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QtWebEngine/qtwebengineglobal.h>
static QUrl startupUrl()
{
    QUrl ret;
    QStringList args(qApp->arguments());
    args.takeFirst();
    for (const QString &arg : qAsConst(args)) {
        if (arg.startsWith(QLatin1Char('-')))
             continue;
        ret = Utils::fromUserInput(arg);
        if (ret.isValid())
            return ret;
    }
    return QUrl(QStringLiteral("https://123.456.789.123:7878"));
}

int main(int argc, char **argv)
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QtWebEngine::initialize();
    Application app(argc, argv);
    QQmlApplicationEngine appEngine;
    Utils utils;
    appEngine.rootContext()->setContextProperty("utils", &utils);
    appEngine.load(QUrl("qrc:/ApplicationRoot.qml"));
    if (!appEngine.rootObjects().isEmpty())
        QMetaObject::invokeMethod(appEngine.rootObjects().first(), "load", Q_ARG(QVariant, startupUrl()));
    else
        qFatal("Failed to load sources");
    return app.exec();
}

ApplicationRoot.qml

import QtQuick 2.1
import QtWebEngine 1.9
QtObject {
    id: root
    property string script_password: "
        setTimeout(function(){
            var input_password = document.getElementsByName('password')[0];
            input_password.value = '%1';
            var button = document.getElementById('passwordNext');
            button.click();
            }, 100);
            ".arg(password)

    property QtObject defaultProfile: WebEngineProfile {}
    property QtObject otrProfile: WebEngineProfile {
        offTheRecord: true
    }
    property Component browserWindowComponent: BrowserWindow {
        applicationRoot: root
        onClosing: destroy()
    }    
    function createWindow(profile) {...}
    function createDialog(profile) {...}
    function load(url) {...}
}

BrowserWindow.qml

import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.0
import QtQuick.Window 2.1
import QtWebEngine 1.9

ApplicationWindow {
    id: browserWindow

    property string password: "abcdefghilmno"

    property QtObject applicationRoot
    property Item currentWebView: tabs.currentIndex < tabs.count ? tabs.getTab(tabs.currentIndex).item : null
    property int previousVisibility: Window.Windowed
    width: 1300
    height: 900
    visible: true
    title: currentWebView && currentWebView.title
    Component.onCompleted: flags = flags | Qt.WindowFullscreenButtonHint
    toolBar: ToolBar {...}
    // Necessary to accept server's certificate
    TabView {...}
    // Showing Manager Window
    WebEngineView {
        id: devToolsView
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        onNewViewRequested: function(request) {
            var tab = tabs.createEmptyTab(currentWebView.profile);
            tabs.currentIndex = tabs.count - 1;
            request.openIn(tab.item);
        }
    }
    // By-Passing the Server's Certificate using sslDialog
    MessageDialog {...}
    DownloadView {
        id: downloadView
        visible: false
        anchors.fill: parent
    }
    function onDownloadRequested(download) {
        downloadView.visible = true;
        downloadView.append(download);
        download.accept();
    }
}

非常感谢您就如何解决这个问题以及如何继续前进提供一些指导。

最佳答案

下一次有必要展示您尝试过的方法,因为说它不起作用并且使用我以前的代码是不够的,每次登录都取决于每个系统,因此没有通用的解决方案。

从您展示的小 HTML 中您可以推断出:

  • 用户名输入的 ID 为“用户名”。
  • 密码输入的id为“password”。
  • 当按下按钮时,调用进行验证的函数“LSubmit()”。

从我用我的路由器的实验来看,DOM的创建总是有延迟,此外输入“/”的url如果不存在则在末尾添加

综合以上,解决方案是:

import QtQuick 2.13
import QtQuick.Controls 2.13
import QtWebEngine 1.9

ApplicationWindow{
    id: root
    width: 640
    height: 480
    visible: true

    property string username: "USERNAME"
    property string password: "PASSWORD"

    QtObject{
        id: internals
        property string login_script: "
            console.log('start');
            var input_username = document.getElementById('username');
            console.log('username');
            console.log(input_username);
            input_username.value = '%1';
            var input_password = document.getElementById('password');
            console.log('password');
            console.log(input_password);
            input_password.value = '%2';
            console.log('clicked');
            LSubmit();
            console.log('end');
        ".arg(username).arg(password);
    }

    Timer {
        id: timer
        interval: 1000; repeat: false
        onTriggered: view.runJavaScript(internals.login_script)
    }

    WebEngineView {
        id: view
        anchors.fill: parent
        onUrlChanged: {
            console.log(url)
            if(url == Qt.resolvedUrl("https://123.456.789.123:7878/")){
                timer.running = true   
            }
        }
        onCertificateError: function(error) {
            error.ignoreCertificateError();
        }
        Component.onCompleted: view.url = "https://123.456.789.123:7878"
    }
}

关于javascript - Qt5-QML : automatic authentication username and password with on a third party device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58921220/

相关文章:

javascript - jQuery .each() 没有按预期迭代字符串数组

Qt4 或 Qt5 (mingw) 和 openCV 2.4.6

qt - 在菜单项中嵌入文本框

c++ - Qt 应用程序在退出时崩溃,操作系统应用 "fault tolerant heap shim"

javascript - 绑定(bind)在 Knockout.js 中的组件内部不起作用

javascript - jquery click事件没有删除父元素

javascript - 带有中心文本的圆形不工作 React Native iOS

c++ - 使用 QOpenGLFramebufferObject 时深度测试不起作用

javascript - QML 在 Javascript 设置的属性上返回 NaN

c++ - 单击按钮后动画 QML 矩形的颜色