[webkit-qt] QtWebKit QWebForm API extension

Lindsay Mathieson lindsay.mathieson at gmail.com
Fri Nov 2 19:59:31 PDT 2012


Dawit, please excuse me if I'm missing something obvious, but is there any reason why we can't walk the DOM using QWebElement to find and fill in the INPUT elements?

I've done the following in rekonq, which fills in my forms for me and avoids the javascript execution bug I found earlier. The code is primitive and not optimal, but it works.



void WebPage::loadFinished(bool ok)
{
  ...
        //wallet()->fillFormData(mainFrame());
        QScopedPointer<KWallet::Wallet> w;
        WId wid = 0;
        QWidget* widget = topLevelWindow(this);
        if (widget)
            wid = widget->winId();        
        QString walletName = KWallet::Wallet::NetworkWallet();
        w.reset(KWallet::Wallet::openWallet(walletName, wid, KWallet::Wallet::Synchronous));
        w->setFolder(KWallet::Wallet::FormDataFolder());
        
        walkFrame(w.operator->(), mainFrame());
}

void walkFrame(KWallet::Wallet *wallet, QWebFrame *frame)
{
    walkElement(wallet, frame, "", frame->documentElement());
    
    foreach(QWebFrame *childFrame, frame->childFrames())
    {
        walkFrame(wallet, childFrame);
    }
}
  

void walkElement(KWallet::Wallet *wallet, QWebFrame *frame, QString formName, QWebElement elem)
{
    elem = elem.firstChild();
    while (! elem.isNull())
    {
        if (elem.tagName().compare("FORM", Qt::CaseInsensitive) == 0)
            formName = elem.attribute("name");
        else if (elem.tagName().compare("INPUT", Qt::CaseInsensitive) == 0)
        {
            QString inputType = elem.attribute("type").toLower();
            if (inputType == "text" || inputType == "password")
            {
                // Fill it from Wallet
                QUrl url = (frame->url().isEmpty() ? frame->baseUrl().resolved(frame->url()) : frame->url());
                QString key = url.toString(QUrl::RemoveQuery|QUrl::RemoveFragment);
                key += QL1C('#');
                key += formName;
                
                QMap<QString, QString> cachedValues;
                wallet->readMap(key, cachedValues);
                QString name = elem.attribute("name");
                QString value = cachedValues.value(name);
                elem.setAttribute("value", value);
            }
        }
        walkElement(wallet, frame, formName, elem);
        elem = elem.nextSibling();
    }
}



-- 
Lindsay Mathieson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20121103/b3368e11/attachment.sig>


More information about the webkit-qt mailing list