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

QmlUserScript: Automatically install/uninstall underlying script

This commit is contained in:
David Rosca 2019-04-20 11:52:28 +02:00
parent a8d43ed0c8
commit 8e4328b03c
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
5 changed files with 48 additions and 45 deletions

View File

@ -16,12 +16,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "qmluserscript.h"
#include "../../qmlstaticdata.h"
QmlUserScript::QmlUserScript(QObject *parent)
: QObject(parent)
{
}
QmlUserScript::~QmlUserScript()
{
QmlStaticData::instance().getUserScriptsSingleton()->remove(this);
}
QWebEngineScript QmlUserScript::webEngineScript() const
{
return m_webEngineScript;
@ -46,6 +52,7 @@ void QmlUserScript::setName(const QString &name)
{
m_webEngineScript.setName(name);
emit nameChanged(name);
aboutToUpdateUnderlyingScript();
}
bool QmlUserScript::runsOnSubFrames() const
@ -57,6 +64,7 @@ void QmlUserScript::setRunsOnSubFrames(bool runsOnSubFrames)
{
m_webEngineScript.setRunsOnSubFrames(runsOnSubFrames);
emit runsOnSubFramesChanged(runsOnSubFrames);
aboutToUpdateUnderlyingScript();
}
int QmlUserScript::worldId() const
@ -80,6 +88,7 @@ void QmlUserScript::setWorldId(int worldId)
break;
}
emit worldIdChanged(worldId);
aboutToUpdateUnderlyingScript();
}
QString QmlUserScript::sourceCode() const
@ -91,6 +100,7 @@ void QmlUserScript::setSourceCode(const QString &sourceCode)
{
m_webEngineScript.setSourceCode(sourceCode);
emit sourceCodeChanged(sourceCode);
aboutToUpdateUnderlyingScript();
}
QmlUserScript::InjectionPoint QmlUserScript::injectionPoint() const
@ -114,4 +124,24 @@ void QmlUserScript::setInjectionPoint(InjectionPoint injectionPoint)
break;
}
emit injectionPointChanged(injectionPoint);
aboutToUpdateUnderlyingScript();
}
void QmlUserScript::timerEvent(QTimerEvent *e)
{
if (e->timerId() != m_basicTimer.timerId()) {
QObject::timerEvent(e);
return;
}
m_basicTimer.stop();
QmlStaticData::instance().getUserScriptsSingleton()->insert(this);
}
void QmlUserScript::aboutToUpdateUnderlyingScript()
{
if (!m_basicTimer.isActive()) {
QmlStaticData::instance().getUserScriptsSingleton()->remove(this);
}
// Defer updates to the next event loop
m_basicTimer.start(0, this);
}

View File

@ -20,6 +20,7 @@
#include "qzcommon.h"
#include <QObject>
#include <QBasicTimer>
#include <QWebEngineScript>
/**
@ -73,6 +74,7 @@ public:
Q_ENUM(ScriptWorldId)
explicit QmlUserScript(QObject *parent = nullptr);
~QmlUserScript();
QWebEngineScript webEngineScript() const;
void setWebEngineScript(const QWebEngineScript &script);
Q_SIGNALS:
@ -98,6 +100,7 @@ Q_SIGNALS:
void injectionPointChanged(int injectionPoint);
private:
QWebEngineScript m_webEngineScript;
QBasicTimer m_basicTimer;
bool null() const;
QString name() const;
@ -110,4 +113,7 @@ private:
void setSourceCode(const QString &sourceCode);
InjectionPoint injectionPoint() const;
void setInjectionPoint(InjectionPoint injectionPoint);
void timerEvent(QTimerEvent *e) override;
void aboutToUpdateUnderlyingScript();
};

View File

@ -26,14 +26,6 @@ QmlUserScripts::QmlUserScripts(QObject *parent)
{
}
QmlUserScripts::~QmlUserScripts()
{
// remove scripts added by the plugin
for (const QWebEngineScript &webEngineScript : qAsConst(m_webEngineScripts)) {
mApp->webProfile()->scripts()->remove(webEngineScript);
}
}
int QmlUserScripts::count() const
{
return mApp->webProfile()->scripts()->count();
@ -85,30 +77,6 @@ QList<QObject*> QmlUserScripts::findScripts(const QString &name) const
return toQObjectList(list);
}
void QmlUserScripts::insert(QObject *object)
{
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
return;
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->insert(webEngineScript);
m_webEngineScripts.append(webEngineScript);
}
void QmlUserScripts::insert(const QList<QObject*> &list)
{
for (QObject *object : list) {
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
continue;
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->insert(webEngineScript);
m_webEngineScripts.append(webEngineScript);
}
}
void QmlUserScripts::remove(QObject *object) const
{
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
@ -124,3 +92,13 @@ QList<QObject*> QmlUserScripts::toList() const
QList<QWebEngineScript> list = mApp->webProfile()->scripts()->toList();
return toQObjectList(list);
}
void QmlUserScripts::insert(QObject *object)
{
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
return;
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->insert(webEngineScript);
}

View File

@ -40,7 +40,6 @@ class FALKON_EXPORT QmlUserScripts : public QObject
Q_PROPERTY(bool empty READ empty CONSTANT)
public:
explicit QmlUserScripts(QObject *parent = nullptr);
~QmlUserScripts();
/**
* @brief Checks if the script is in collection
* @param object of type QmlUserScript
@ -58,16 +57,6 @@ public:
* @return list of objects, each of type QmlUserScript, representing the script of given name
*/
Q_INVOKABLE QList<QObject*> findScripts(const QString &name) const;
/**
* @brief Inserts a script into collection
* @param object of type QmlUserScript
*/
Q_INVOKABLE void insert(QObject *object);
/**
* @brief Inserts a list of scripts into collection
* @param list of objects, each of type QmlUserScript
*/
Q_INVOKABLE void insert(const QList<QObject*> &list);
/**
* @brief Removes a script from collection
* @param object of type QmlUserScript
@ -78,11 +67,12 @@ public:
* @return list of objects, each of type QmlUserScript
*/
Q_INVOKABLE QList<QObject*> toList() const;
void insert(QObject *object);
private:
int count() const;
int size() const;
bool empty() const;
QList<QWebEngineScript> m_webEngineScripts;
QList<QObject*> toQObjectList(const QList<QWebEngineScript> &list) const;
};

View File

@ -25,7 +25,6 @@ Falkon.PluginInterface {
id: 'helloQmlObject',
object: helloQmlObject
})
Falkon.UserScripts.insert(testingHelloQmlUserScript)
}
testPlugin: function() {