From 97ba8c6af3168b7e56c7db4c190a7cd5fc67e016 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Fri, 2 Oct 2015 14:13:21 +0200 Subject: [PATCH] Bring back PIM plugin --- src/lib/tools/scripts.cpp | 1 - src/plugins/PIM/PIM_handler.cpp | 111 ++++++++++++++++++-------------- src/plugins/PIM/PIM_handler.h | 9 ++- src/plugins/PIM/PIM_plugin.cpp | 4 +- src/plugins/PIM/PIM_plugin.h | 2 +- src/plugins/plugins.pro | 1 - 6 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src/lib/tools/scripts.cpp b/src/lib/tools/scripts.cpp index c26ef3b85..1422f4101 100644 --- a/src/lib/tools/scripts.cpp +++ b/src/lib/tools/scripts.cpp @@ -272,7 +272,6 @@ QString Scripts::getFormData(const QPoint &pos) " var input = fe.elements[i];" " res.inputs.push([input.name, input.value]);" "}" - "console.log(res);" "return res;" "})()"); diff --git a/src/plugins/PIM/PIM_handler.cpp b/src/plugins/PIM/PIM_handler.cpp index dee231877..9dbdbc226 100644 --- a/src/plugins/PIM/PIM_handler.cpp +++ b/src/plugins/PIM/PIM_handler.cpp @@ -20,11 +20,10 @@ #include "PIM_settings.h" #include "webview.h" #include "webpage.h" +#include "webhittestresult.h" #include #include -#include -#include #include #include #include @@ -99,10 +98,10 @@ void PIM_Handler::showSettings(QWidget* parent) m_settings.data()->raise(); } -void PIM_Handler::populateWebViewMenu(QMenu* menu, WebView* view, const QWebHitTestResult &hitTest) +void PIM_Handler::populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult &hitTest) { m_view = view; - m_element = hitTest.element(); + m_clickedPos = hitTest.pos(); if (!hitTest.isContentEditable()) { return; @@ -152,20 +151,23 @@ bool PIM_Handler::keyPress(WebView* view, QKeyEvent* event) return false; } - const QWebElement document = view->page()->mainFrame()->documentElement(); - const QWebElementCollection elements = document.findAll("input[type=\"text\"]"); + QString source = QL1S("var inputs = document.getElementsByTagName('input');" + "var table = %1;" + "for (var i = 0; i < inputs.length; ++i) {" + " var input = inputs[i];" + " if (input.type != 'text' || input.name == '')" + " continue;" + " for (var key in table) {" + " if (!table.hasOwnProperty(key))" + " continue;" + " if (key == input.name || input.name.indexOf(key) != -1) {" + " input.value = table[key];" + " break;" + " }" + " }" + "}"); - foreach (QWebElement element, elements) { - const QString name = element.attribute("name"); - if (name.isEmpty()) { - continue; - } - - PI_Type match = nameMatch(name); - if (match != PI_Invalid) { - element.evaluateJavaScript(QString("this.value = \"%1\"").arg(m_allInfo[match])); - } - } + view->page()->runJavaScript(source.arg(matchingJsTable())); return true; } @@ -182,14 +184,24 @@ void PIM_Handler::webPageCreated(WebPage* page) void PIM_Handler::pimInsert() { - QAction* action = qobject_cast(sender()); - if (m_element.isNull() || !action) { + if (!m_view || m_clickedPos.isNull()) + return; + + QAction* action = qobject_cast(sender()); + if (!action) return; - } QString info = action->data().toString(); info.replace(QLatin1Char('"'), QLatin1String("\\\"")); - m_element.evaluateJavaScript(QString("var newVal = this.value.substring(0, this.selectionStart) + \"%1\" + this.value.substring(this.selectionEnd); this.value = newVal;").arg(info)); + + QString source = QL1S("var e = document.elementFromPoint(%1, %2);" + "if (e) {" + " var v = e.value.substring(0, e.selectionStart);" + " v += \"%3\" + e.value.substring(e.selectionEnd);" + " e.value = v;" + "}"); + source = source.arg(QString::number(m_clickedPos.x()), QString::number(m_clickedPos.y()), info); + m_view->page()->runJavaScript(source); } void PIM_Handler::pageLoadFinished() @@ -203,37 +215,42 @@ void PIM_Handler::pageLoadFinished() loadSettings(); } - const QWebElement document = page->mainFrame()->documentElement(); - const QWebElementCollection elements = document.findAll("input[type=\"text\"]"); + QString source = QL1S("var inputs = document.getElementsByTagName('input');" + "var table = %1;" + "for (var i = 0; i < inputs.length; ++i) {" + " var input = inputs[i];" + " if (input.type != 'text' || input.name == '')" + " continue;" + " for (var key in table) {" + " if (!table.hasOwnProperty(key) || table[key] == '')" + " continue;" + " if (key == input.name || input.name.indexOf(key) != -1) {" + " input.style['-webkit-appearance'] = 'none';" + " input.style['-webkit-box-shadow'] = 'inset 0 0 2px 1px #EEE000';" + " break;" + " }" + " }" + "}"); - foreach (QWebElement element, elements) { - const QString name = element.attribute("name"); - if (name.isEmpty()) { - continue; - } - - PI_Type match = nameMatch(name); - if (match != PI_Invalid) { - element.setStyleProperty("-webkit-appearance", "none"); - element.setStyleProperty("-webkit-box-shadow", "inset 0 0 2px 1px #EEE000"); - } - } + page->runJavaScript(source.arg(matchingJsTable())); } -PIM_Handler::PI_Type PIM_Handler::nameMatch(const QString &name) +QString PIM_Handler::matchingJsTable() const { - for (int i = 0; i < PI_Max; ++i) { - if (!m_allInfo[PI_Type(i)].isEmpty()) { - foreach (const QString &n, m_infoMatches[PI_Type(i)]) { - if (name == n) { - return PI_Type(i); - } - if (name.contains(n)) { - return PI_Type(i); - } - } + QString values; + + QHashIterator i(m_infoMatches); + while (i.hasNext()) { + i.next(); + foreach (const QString &value, i.value()) { + QString key = m_allInfo.value(i.key()); + key.replace(QL1C('"'), QL1S("\\\"")); + values.append(QSL("\"%1\":\"%2\",").arg(value, key)); } } - return PI_Invalid; + if (!values.isEmpty()) + values = values.left(values.size() - 1); + + return QSL("{ %1 }").arg(values); } diff --git a/src/plugins/PIM/PIM_handler.h b/src/plugins/PIM/PIM_handler.h index fa8dc1537..c977ab1de 100644 --- a/src/plugins/PIM/PIM_handler.h +++ b/src/plugins/PIM/PIM_handler.h @@ -20,15 +20,14 @@ #define PIM_HANDLER_H #include -#include #include -#include #include #include #include class WebView; class WebPage; +class WebHitTestResult; class PIM_Settings; @@ -38,7 +37,7 @@ class PIM_Handler : public QObject public: explicit PIM_Handler(const QString &sPath, QObject* parent = 0); - void populateWebViewMenu(QMenu* menu, WebView* view, const QWebHitTestResult &hitTest); + void populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult &hitTest); bool keyPress(WebView* view, QKeyEvent* event); void unloadPlugin(); @@ -75,7 +74,7 @@ private: PI_Invalid = 128 }; - PI_Type nameMatch(const QString &name); + QString matchingJsTable() const; QHash m_allInfo; QHash m_infoMatches; @@ -83,7 +82,7 @@ private: QPointer m_settings; QPointer m_view; - QWebElement m_element; + QPoint m_clickedPos; QString m_settingsFile; bool m_loaded; diff --git a/src/plugins/PIM/PIM_plugin.cpp b/src/plugins/PIM/PIM_plugin.cpp index 7715dff6d..ae2ceaa06 100644 --- a/src/plugins/PIM/PIM_plugin.cpp +++ b/src/plugins/PIM/PIM_plugin.cpp @@ -38,7 +38,7 @@ PluginSpec PIM_Plugin::pluginSpec() spec.name = "PIM"; spec.info = "Personal Information Manager"; spec.description = "Adds ability for Qupzilla to store some personal data"; - spec.version = "0.1.2"; + spec.version = "0.2.0"; spec.author = QString::fromUtf8("Mladen Pejaković "); spec.icon = QPixmap(":/PIM/data/PIM.png"); spec.hasSettings = true; @@ -81,7 +81,7 @@ void PIM_Plugin::showSettings(QWidget* parent) m_handler->showSettings(parent); } -void PIM_Plugin::populateWebViewMenu(QMenu* menu, WebView* view, const QWebHitTestResult &r) +void PIM_Plugin::populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult &r) { m_handler->populateWebViewMenu(menu, view, r); } diff --git a/src/plugins/PIM/PIM_plugin.h b/src/plugins/PIM/PIM_plugin.h index 9c8e0f335..c5c1a9192 100644 --- a/src/plugins/PIM/PIM_plugin.h +++ b/src/plugins/PIM/PIM_plugin.h @@ -45,7 +45,7 @@ public: QTranslator* getTranslator(const QString &locale); void showSettings(QWidget* parent = 0); - void populateWebViewMenu(QMenu* menu, WebView* view, const QWebHitTestResult &r); + void populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult &r); bool keyPress(const Qz::ObjectName &type, QObject* obj, QKeyEvent* event); private: diff --git a/src/plugins/plugins.pro b/src/plugins/plugins.pro index 8b9d370d8..941d031c2 100644 --- a/src/plugins/plugins.pro +++ b/src/plugins/plugins.pro @@ -44,5 +44,4 @@ isEqual(QT_MAJOR_VERSION, 5): !qtHaveModule(KWallet): disablePlugin(KWalletPassw disablePlugin(AccessKeysNavigation) disablePlugin(CopyTitle) disablePlugin(MailHandle) -disablePlugin(PIM) disablePlugin(Videoner)