1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */ * ============================================================ */
#include "qmluserscript.h" #include "qmluserscript.h"
#include "../../qmlstaticdata.h"
QmlUserScript::QmlUserScript(QObject *parent) QmlUserScript::QmlUserScript(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
} }
QmlUserScript::~QmlUserScript()
{
QmlStaticData::instance().getUserScriptsSingleton()->remove(this);
}
QWebEngineScript QmlUserScript::webEngineScript() const QWebEngineScript QmlUserScript::webEngineScript() const
{ {
return m_webEngineScript; return m_webEngineScript;
@ -46,6 +52,7 @@ void QmlUserScript::setName(const QString &name)
{ {
m_webEngineScript.setName(name); m_webEngineScript.setName(name);
emit nameChanged(name); emit nameChanged(name);
aboutToUpdateUnderlyingScript();
} }
bool QmlUserScript::runsOnSubFrames() const bool QmlUserScript::runsOnSubFrames() const
@ -57,6 +64,7 @@ void QmlUserScript::setRunsOnSubFrames(bool runsOnSubFrames)
{ {
m_webEngineScript.setRunsOnSubFrames(runsOnSubFrames); m_webEngineScript.setRunsOnSubFrames(runsOnSubFrames);
emit runsOnSubFramesChanged(runsOnSubFrames); emit runsOnSubFramesChanged(runsOnSubFrames);
aboutToUpdateUnderlyingScript();
} }
int QmlUserScript::worldId() const int QmlUserScript::worldId() const
@ -80,6 +88,7 @@ void QmlUserScript::setWorldId(int worldId)
break; break;
} }
emit worldIdChanged(worldId); emit worldIdChanged(worldId);
aboutToUpdateUnderlyingScript();
} }
QString QmlUserScript::sourceCode() const QString QmlUserScript::sourceCode() const
@ -91,6 +100,7 @@ void QmlUserScript::setSourceCode(const QString &sourceCode)
{ {
m_webEngineScript.setSourceCode(sourceCode); m_webEngineScript.setSourceCode(sourceCode);
emit sourceCodeChanged(sourceCode); emit sourceCodeChanged(sourceCode);
aboutToUpdateUnderlyingScript();
} }
QmlUserScript::InjectionPoint QmlUserScript::injectionPoint() const QmlUserScript::InjectionPoint QmlUserScript::injectionPoint() const
@ -114,4 +124,24 @@ void QmlUserScript::setInjectionPoint(InjectionPoint injectionPoint)
break; break;
} }
emit injectionPointChanged(injectionPoint); 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 "qzcommon.h"
#include <QObject> #include <QObject>
#include <QBasicTimer>
#include <QWebEngineScript> #include <QWebEngineScript>
/** /**
@ -73,6 +74,7 @@ public:
Q_ENUM(ScriptWorldId) Q_ENUM(ScriptWorldId)
explicit QmlUserScript(QObject *parent = nullptr); explicit QmlUserScript(QObject *parent = nullptr);
~QmlUserScript();
QWebEngineScript webEngineScript() const; QWebEngineScript webEngineScript() const;
void setWebEngineScript(const QWebEngineScript &script); void setWebEngineScript(const QWebEngineScript &script);
Q_SIGNALS: Q_SIGNALS:
@ -98,6 +100,7 @@ Q_SIGNALS:
void injectionPointChanged(int injectionPoint); void injectionPointChanged(int injectionPoint);
private: private:
QWebEngineScript m_webEngineScript; QWebEngineScript m_webEngineScript;
QBasicTimer m_basicTimer;
bool null() const; bool null() const;
QString name() const; QString name() const;
@ -110,4 +113,7 @@ private:
void setSourceCode(const QString &sourceCode); void setSourceCode(const QString &sourceCode);
InjectionPoint injectionPoint() const; InjectionPoint injectionPoint() const;
void setInjectionPoint(InjectionPoint injectionPoint); 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 int QmlUserScripts::count() const
{ {
return mApp->webProfile()->scripts()->count(); return mApp->webProfile()->scripts()->count();
@ -85,30 +77,6 @@ QList<QObject*> QmlUserScripts::findScripts(const QString &name) const
return toQObjectList(list); 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 void QmlUserScripts::remove(QObject *object) const
{ {
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object); QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
@ -124,3 +92,13 @@ QList<QObject*> QmlUserScripts::toList() const
QList<QWebEngineScript> list = mApp->webProfile()->scripts()->toList(); QList<QWebEngineScript> list = mApp->webProfile()->scripts()->toList();
return toQObjectList(list); 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) Q_PROPERTY(bool empty READ empty CONSTANT)
public: public:
explicit QmlUserScripts(QObject *parent = nullptr); explicit QmlUserScripts(QObject *parent = nullptr);
~QmlUserScripts();
/** /**
* @brief Checks if the script is in collection * @brief Checks if the script is in collection
* @param object of type QmlUserScript * @param object of type QmlUserScript
@ -58,16 +57,6 @@ public:
* @return list of objects, each of type QmlUserScript, representing the script of given name * @return list of objects, each of type QmlUserScript, representing the script of given name
*/ */
Q_INVOKABLE QList<QObject*> findScripts(const QString &name) const; 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 * @brief Removes a script from collection
* @param object of type QmlUserScript * @param object of type QmlUserScript
@ -78,11 +67,12 @@ public:
* @return list of objects, each of type QmlUserScript * @return list of objects, each of type QmlUserScript
*/ */
Q_INVOKABLE QList<QObject*> toList() const; Q_INVOKABLE QList<QObject*> toList() const;
void insert(QObject *object);
private: private:
int count() const; int count() const;
int size() const; int size() const;
bool empty() const; bool empty() const;
QList<QWebEngineScript> m_webEngineScripts;
QList<QObject*> toQObjectList(const QList<QWebEngineScript> &list) const; QList<QObject*> toQObjectList(const QList<QWebEngineScript> &list) const;
}; };

View File

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