aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-10-13 14:01:01 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-10-13 14:01:01 +0200
commit29ba3288d0d342e79d14676a02af555c6d43c3e6 (patch)
treeb4201b61782df21cf22fa8efad8619d1e9f2adb5 /src/wallet
parentUpdate readme (diff)
downloadsmolbote-29ba3288d0d342e79d14676a02af555c6d43c3e6.tar.xz
unstable: KWallet integration
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/wallet.cpp44
-rw-r--r--src/wallet/wallet.h18
2 files changed, 62 insertions, 0 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
new file mode 100644
index 0000000..c795cf2
--- /dev/null
+++ b/src/wallet/wallet.cpp
@@ -0,0 +1,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
+}
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
new file mode 100644
index 0000000..d2fdb71
--- /dev/null
+++ b/src/wallet/wallet.h
@@ -0,0 +1,18 @@
+/*
+ * 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
+ */
+
+#ifndef SMOLBOTE_WALLET_H
+#define SMOLBOTE_WALLET_H
+
+class QWebEngineView;
+
+namespace Wallet {
+ void autocompleteForm(QWebEngineView *view);
+}
+
+#endif // SMOLBOTE_WALLET_H