1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

Created UserScript API

This commit is contained in:
Anmol Gautam 2018-07-03 19:47:46 +05:30
parent 1a4d53fb4c
commit d01609cb05
6 changed files with 359 additions and 0 deletions

View File

@ -183,6 +183,8 @@ set(SRCS ${SRCS}
plugins/qml/api/events/qmlmouseevent.cpp
plugins/qml/api/events/qmlwheelevent.cpp
plugins/qml/api/events/qmlkeyevent.cpp
plugins/qml/api/userscript/qmluserscript.cpp
plugins/qml/api/userscript/qmluserscripts.cpp
popupwindow/popuplocationbar.cpp
popupwindow/popupstatusbarmessage.cpp
popupwindow/popupwebview.cpp

View File

@ -0,0 +1,117 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "qmluserscript.h"
QmlUserScript::QmlUserScript(QObject *parent)
: QObject(parent)
{
}
QWebEngineScript QmlUserScript::webEngineScript() const
{
return m_webEngineScript;
}
void QmlUserScript::setWebEngineScript(const QWebEngineScript &script)
{
m_webEngineScript = script;
}
bool QmlUserScript::null() const
{
return m_webEngineScript.isNull();
}
QString QmlUserScript::name() const
{
return m_webEngineScript.name();
}
void QmlUserScript::setName(const QString &name)
{
m_webEngineScript.setName(name);
emit nameChanged(name);
}
bool QmlUserScript::runsOnSubFrames() const
{
return m_webEngineScript.runsOnSubFrames();
}
void QmlUserScript::setRunsOnSubFrames(bool runsOnSubFrames)
{
m_webEngineScript.setRunsOnSubFrames(runsOnSubFrames);
emit runsOnSubFramesChanged(runsOnSubFrames);
}
int QmlUserScript::worldId() const
{
return (int)m_webEngineScript.worldId();
}
void QmlUserScript::setWorldId(int worldId)
{
switch (worldId) {
case QWebEngineScript::MainWorld:
m_webEngineScript.setWorldId(QWebEngineScript::MainWorld);
break;
case QWebEngineScript::ApplicationWorld:
m_webEngineScript.setWorldId(QWebEngineScript::ApplicationWorld);
break;
case QWebEngineScript::UserWorld:
m_webEngineScript.setWorldId(QWebEngineScript::UserWorld);
break;
default:
break;
}
emit worldIdChanged(worldId);
}
QString QmlUserScript::sourceCode() const
{
return m_webEngineScript.sourceCode();
}
void QmlUserScript::setSourceCode(const QString &sourceCode)
{
m_webEngineScript.setSourceCode(sourceCode);
emit sourceCodeChanged(sourceCode);
}
int QmlUserScript::injectionPoint() const
{
return (int)m_webEngineScript.injectionPoint();
}
void QmlUserScript::setInjectionPoint(int injectionPoint)
{
switch (injectionPoint) {
case QWebEngineScript::DocumentCreation:
m_webEngineScript.setInjectionPoint(QWebEngineScript::DocumentCreation);
break;
case QWebEngineScript::DocumentReady:
m_webEngineScript.setInjectionPoint(QWebEngineScript::DocumentReady);
break;
case QWebEngineScript::Deferred:
m_webEngineScript.setInjectionPoint(QWebEngineScript::Deferred);
break;
default:
break;
}
emit injectionPointChanged(injectionPoint);
}

View File

@ -0,0 +1,69 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include <QObject>
#include <QWebEngineScript>
class QmlUserScript : public QObject
{
Q_OBJECT
Q_PROPERTY(bool null READ null CONSTANT)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(bool runsOnSubFrames READ runsOnSubFrames WRITE setRunsOnSubFrames NOTIFY runsOnSubFramesChanged)
Q_PROPERTY(int worldId READ worldId WRITE setWorldId NOTIFY worldIdChanged)
Q_PROPERTY(QString sourceCode READ sourceCode WRITE setSourceCode NOTIFY sourceCodeChanged)
Q_PROPERTY(int injectionPoint READ injectionPoint WRITE setInjectionPoint NOTIFY injectionPointChanged)
public:
enum InjectionPoint {
DocumentCreation = QWebEngineScript::DocumentCreation,
DocumentReady = QWebEngineScript::DocumentReady,
Deferred = QWebEngineScript::Deferred
};
enum ScriptWorldId {
MainWorld = QWebEngineScript::MainWorld,
ApplicationWorld = QWebEngineScript::ApplicationWorld,
UserWorld = QWebEngineScript::UserWorld
};
Q_ENUMS(InjectionPoint)
Q_ENUMS(ScriptWorldId)
explicit QmlUserScript(QObject *parent = nullptr);
QWebEngineScript webEngineScript() const;
void setWebEngineScript(const QWebEngineScript &script);
Q_SIGNALS:
void nameChanged(const QString &name);
void runsOnSubFramesChanged(bool runsOnSubFrames);
void worldIdChanged(int worldId);
void sourceCodeChanged(const QString &sourceCode);
void injectionPointChanged(int injectionPoint);
private:
QWebEngineScript m_webEngineScript;
bool null() const;
QString name() const;
void setName(const QString &name);
bool runsOnSubFrames() const;
void setRunsOnSubFrames(bool runsOnSubFrames);
int worldId() const;
void setWorldId(int worldId);
QString sourceCode() const;
void setSourceCode(const QString &sourceCode);
int injectionPoint() const;
void setInjectionPoint(int injectionPoint);
};

View File

@ -0,0 +1,114 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "qmluserscripts.h"
#include "mainapplication.h"
#include <QWebEngineProfile>
#include <QWebEngineScriptCollection>
QmlUserScripts::QmlUserScripts(QObject *parent)
: QObject(parent)
{
}
int QmlUserScripts::count() const
{
return mApp->webProfile()->scripts()->count();
}
int QmlUserScripts::size() const
{
return mApp->webProfile()->scripts()->size();
}
bool QmlUserScripts::empty() const
{
return mApp->webProfile()->scripts()->isEmpty();
}
QList<QObject *> QmlUserScripts::toQObjectList(QList<QWebEngineScript> list) const
{
QList<QObject *> userScriptList;
for (const QWebEngineScript &script : list) {
QmlUserScript *userScript = new QmlUserScript();
userScript->setWebEngineScript(script);
userScriptList.append(userScript);
}
return userScriptList;
}
bool QmlUserScripts::contains(QObject *object) const
{
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
return false;
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
return mApp->webProfile()->scripts()->contains(webEngineScript);
}
QmlUserScript *QmlUserScripts::findScript(const QString &name) const
{
QWebEngineScript webEngineScript = mApp->webProfile()->scripts()->findScript(name);
QmlUserScript *qmlUserScript = new QmlUserScript();
qmlUserScript->setWebEngineScript(webEngineScript);
return qmlUserScript;
}
QList<QObject*> QmlUserScripts::findScripts(const QString &name) const
{
QList<QWebEngineScript> list = mApp->webProfile()->scripts()->findScripts(name);
return toQObjectList(list);
}
void QmlUserScripts::insert(QObject *object) const
{
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
return;
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->insert(webEngineScript);
}
void QmlUserScripts::insert(const QList<QObject *> &list) const
{
for (QObject *object : list) {
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
continue;
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->insert(webEngineScript);
}
}
void QmlUserScripts::remove(QObject *object) const
{
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
return;
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->remove(webEngineScript);
}
QList<QObject *> QmlUserScripts::toList() const
{
QList<QWebEngineScript> list = mApp->webProfile()->scripts()->toList();
return toQObjectList(list);
}

View File

@ -0,0 +1,44 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include "qmluserscript.h"
#include <QObject>
class QmlUserScripts : public QObject
{
Q_OBJECT
Q_PROPERTY(int count READ count CONSTANT)
Q_PROPERTY(int size READ size CONSTANT)
Q_PROPERTY(bool empty READ empty CONSTANT)
public:
explicit QmlUserScripts(QObject *parent = nullptr);
Q_INVOKABLE bool contains(QObject *object) const;
Q_INVOKABLE QmlUserScript *findScript(const QString &name) const;
Q_INVOKABLE QList<QObject *> findScripts(const QString &name) const;
Q_INVOKABLE void insert(QObject *object) const;
Q_INVOKABLE void insert(const QList<QObject*> &list) const;
Q_INVOKABLE void remove(QObject *object) const;
Q_INVOKABLE QList<QObject *> toList() const;
private:
int count() const;
int size() const;
bool empty() const;
QList<QObject *> toQObjectList(QList<QWebEngineScript> list) const;
};

View File

@ -42,6 +42,8 @@
#include "api/events/qmlqzobjects.h"
#include "api/events/qmlmouseevent.h"
#include "api/events/qmlwheelevent.h"
#include "api/userscript/qmluserscript.h"
#include "api/userscript/qmluserscripts.h"
#ifdef LibIntl_FOUND
#include "qml/api/i18n/qmli18n.h"
@ -180,4 +182,15 @@ void QmlPlugins::registerQmlTypes()
return object;
});
#endif
// UserScripts
qmlRegisterType<QmlUserScript>("org.kde.falkon", 1, 0, "UserScript");
qmlRegisterSingletonType<QmlUserScripts>("org.kde.falkon", 1, 0, "UserScripts", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
auto *object = new QmlUserScripts();
return object;
});
}