aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/wallet.cpp
blob: c795cf2a7eabe76aaf1f99f3072455429cf00b75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "wallet.h"
#include <QWebEngineView>

#ifdef PLASMA
#include <kwallet.h>
#endif

void Wallet::autocompleteForm(QWebEngineView *view)
{
#ifdef PLASMA
    const auto findFormFunction = QLatin1Literal("index = undefined; for(var i = 0; i < document.forms.length; ++i) { if(document.forms[i].autocomplete) { index = i } }; index");
    view->page()->runJavaScript(findFormFunction, [view](const QVariant &v) {
        if(!v.isNull()) {
            auto autofillFunction = QString("inputs = document.forms[%1].getElementsByTagName('input');"
                                            "for(var i = 0; i < inputs.length; ++i) {"
                                            "  if(inputs[i].type == 'email') { inputs[i].value='%2' }"
                                            "  if(inputs[i].type == 'password') { inputs[i].value='%3' }"
                                            "}");

            auto *wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(), view->window()->winId());
            if(wallet) {
                wallet->setFolder("smolbote");
                QMap<QString, QString> map;
                wallet->readMap(view->url().host(), map);
                qDebug() << map;

                const auto username = map.firstKey();
                QString password;
                wallet->readPassword(map.value(username), password);
                view->page()->runJavaScript(autofillFunction.arg(v.toString(), username, password));
            }
            delete wallet;
        }
    });
#endif
}