从QWebView接收表单信息

发布于 2024-12-02 14:34:49 字数 339 浏览 1 评论 0原文

我目前正在尝试从 QWebView 接收一些信息,但不幸的是失败了。

如何找出用户在表单中更改了哪些字段?可能有一些我不感兴趣的内容,包括隐藏的内容,我只想要有关用户更改了哪些内容的信息。 (一种方法是使用evaluateJavaScript()列出所有表单和输入并稍后检查它们,但这很丑陋并且对解决第二个问题没有帮助)

另外我想了解有关表单本身的信息。名称、方法和行动是什么?

QWebPage 目前仅提供了类型为 NavigationTypeFormSubscribed 的acceptNavigationRequest() 的重写,这对我没有帮助,因为它没有给我任何这些信息。

感谢您的帮助!

I'm currently trying to receive some information from QWebView but unfortunately fail.

How do I find out which fields the user did change in a form? There may be several including hidden ones which I'm not interested in, I only want information about which ones were altered by the user. (One method for this would be to list all forms and inputs with evaluateJavaScript() and check them later, but this is ugly and doesn't help with the second problem)

Also I want to know information about the form itself. What was the name, method and action?

QWebPage currently only provides a override of acceptNavigationRequest() with type NavigationTypeFormSubmitted which doesn't help me as it doesn't give me any of these information.

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2024-12-09 14:34:49

好吧,在阅读了这里的一些“相关”问题后,我最终发现唯一的方法可能是 JavaScript。

我的解决方案如下。更改网站上的数据后,m_changedInputs 包含有关哪些表单和哪些输入已更改的信息。

CustomWebPage::CustomWebPage(QWidget *parent)
: QWebPage(parent)
{
    connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
}

...

void CustomWebPage::onLoadFinished(bool ok)
{
    // Do nothing on fail
    if ( !ok )
        return;

    // Clear all cached data
    m_changedInputs.clear();

    // Get the main frame
    QWebFrame* frame = mainFrame();
    frame->addToJavaScriptWindowObject("pluginCreator", this);

    // Find the form
    QWebElementCollection forms = frame->findAllElements("form");

    // Iterate the forms
    foreach(QWebElement form, forms) {
        // Determine the name of the form
        QString formName = form.attribute("name");

        // Find the forms' input elements
        QWebElementCollection inputs = form.findAll("input");

        // Iterate the inputs
        foreach(QWebElement input, inputs) {
            input.setAttribute("onchange", QString("pluginCreator.onInputChanged(\"%1\", this.name);").arg(formName));
        }
    }
}

void CustomWebPage::onInputChanged(const QString& formName, const QString& inputName)
{
    qDebug() << "Form (" << formName << ") data changed:" << inputName;

    // Make sure we only have each input once. A QSet would also do the trick.
    QStringList& inputNames = m_changedInputs[formName];
    if ( !inputNames.contains(inputName) )
        inputNames.append(inputName);
}

Ok, after reading some of the "Related" questions here I eventually found that the only way may be JavaScript.

My solution is below. After changing data on the website, m_changedInputs contains information about which forms and which inputs have been changed.

CustomWebPage::CustomWebPage(QWidget *parent)
: QWebPage(parent)
{
    connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
}

...

void CustomWebPage::onLoadFinished(bool ok)
{
    // Do nothing on fail
    if ( !ok )
        return;

    // Clear all cached data
    m_changedInputs.clear();

    // Get the main frame
    QWebFrame* frame = mainFrame();
    frame->addToJavaScriptWindowObject("pluginCreator", this);

    // Find the form
    QWebElementCollection forms = frame->findAllElements("form");

    // Iterate the forms
    foreach(QWebElement form, forms) {
        // Determine the name of the form
        QString formName = form.attribute("name");

        // Find the forms' input elements
        QWebElementCollection inputs = form.findAll("input");

        // Iterate the inputs
        foreach(QWebElement input, inputs) {
            input.setAttribute("onchange", QString("pluginCreator.onInputChanged(\"%1\", this.name);").arg(formName));
        }
    }
}

void CustomWebPage::onInputChanged(const QString& formName, const QString& inputName)
{
    qDebug() << "Form (" << formName << ") data changed:" << inputName;

    // Make sure we only have each input once. A QSet would also do the trick.
    QStringList& inputNames = m_changedInputs[formName];
    if ( !inputNames.contains(inputName) )
        inputNames.append(inputName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文