Skip to content
Snippets Groups Projects
Commit a547aadf authored by Morten Sorvig's avatar Morten Sorvig
Browse files

Add support for getting the query string and search parameters

qtwebutils::queryString() -> QString
   
  Returns the query string as a QString: 
      "?u=123&v=abc"

qtwebutils::searchParameters() -> QHash<QString, QString>

  Returns the search parameters in a QHash: 
      "u" ->  "123"
      "v" ->  "abc"




parent 7e917fb2
No related branches found
No related tags found
No related merge requests found
...@@ -160,6 +160,41 @@ void qtwebutils::closeBrowserWindow(emscripten::val window) ...@@ -160,6 +160,41 @@ void qtwebutils::closeBrowserWindow(emscripten::val window)
window.call<void>("close"); window.call<void>("close");
} }
/*!
Returns the query string for the current location of the html document.
For example, if the location is "example.com?q=123" then this function
returns "q=123".
*/
QString qtwebutils::queryString()
{
return QString::fromEcmaString(emscripten::val::global("window")["location"]["search"]);
}
/*!
Returns the seach parameters for the current location of the html document.
For example, if the location is "example.com?u=123&v=abc" then this function
returns a QHash containing the key-values ("u", "123") and ("v", "abc").
*/
QHash<QString, QString> qtwebutils::searchParameters()
{
using emscripten::val;
val search = val::global("window")["location"]["search"];
val urlSearchParams = val::global("URLSearchParams").new_(search);
val iterator = urlSearchParams.call<val>("entries");
QHash<QString, QString> params;
emscripten::val entry = iterator.call<val>("next");
while (!entry["done"].as<bool>()) {
emscripten::val pair = entry["value"];
params.insert(QString::fromEcmaString(pair[0]), QString::fromEcmaString(pair[1]));
entry = iterator.call<val>("next");
}
return params;
}
QtWebUtils::QtWebUtils(QObject *parent) QtWebUtils::QtWebUtils(QObject *parent)
:QObject(parent) :QObject(parent)
{ {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QSize> #include <QtCore/QSize>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QHash>
#include <QtQml/QQmlEngine> #include <QtQml/QQmlEngine>
...@@ -27,6 +28,9 @@ namespace qtwebutils { ...@@ -27,6 +28,9 @@ namespace qtwebutils {
emscripten::val openBrowserWindow(const QString& source, const QString &name, QSize size, OpenWindowMode openMode); emscripten::val openBrowserWindow(const QString& source, const QString &name, QSize size, OpenWindowMode openMode);
void closeBrowserWindow(emscripten::val window); void closeBrowserWindow(emscripten::val window);
QString queryString();
QHash<QString, QString> searchParameters();
} }
class QtWebUtils : public QObject class QtWebUtils : public QObject
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment