Falkon Develop
Cross-platform Qt-based web browser
qmluserscript.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmluserscript.h"
19#include "../../qmlstaticdata.h"
20
22 : QObject(parent)
23 , m_isNull(true)
24{
25}
26
28{
30}
31
32QWebEngineScript QmlUserScript::webEngineScript() const
33{
34 return m_webEngineScript;
35}
36
37void QmlUserScript::setWebEngineScript(const QWebEngineScript &script)
38{
39 setNotNull();
40 m_webEngineScript = script;
41}
42
43bool QmlUserScript::null() const
44{
45 return m_isNull;
46}
47
48QString QmlUserScript::name() const
49{
50 return m_webEngineScript.name();
51}
52
53void QmlUserScript::setName(const QString &name)
54{
55 setNotNull();
56 m_webEngineScript.setName(name);
57 Q_EMIT nameChanged(name);
58 aboutToUpdateUnderlyingScript();
59}
60
62{
63 return m_webEngineScript.runsOnSubFrames();
64}
65
66void QmlUserScript::setRunsOnSubFrames(bool runsOnSubFrames)
67{
68 setNotNull();
69 m_webEngineScript.setRunsOnSubFrames(runsOnSubFrames);
71 aboutToUpdateUnderlyingScript();
72}
73
74int QmlUserScript::worldId() const
75{
76 return static_cast<int>(m_webEngineScript.worldId());
77}
78
79void QmlUserScript::setWorldId(int worldId)
80{
81 setNotNull();
82 switch (worldId) {
83 case QWebEngineScript::MainWorld:
84 m_webEngineScript.setWorldId(QWebEngineScript::MainWorld);
85 break;
86 case QWebEngineScript::ApplicationWorld:
87 m_webEngineScript.setWorldId(QWebEngineScript::ApplicationWorld);
88 break;
89 case QWebEngineScript::UserWorld:
90 m_webEngineScript.setWorldId(QWebEngineScript::UserWorld);
91 break;
92 default:
93 break;
94 }
95 Q_EMIT worldIdChanged(worldId);
96 aboutToUpdateUnderlyingScript();
97}
98
99QString QmlUserScript::sourceCode() const
100{
101 return m_webEngineScript.sourceCode();
102}
103
104void QmlUserScript::setSourceCode(const QString &sourceCode)
105{
106 setNotNull();
107 m_webEngineScript.setSourceCode(sourceCode);
109 aboutToUpdateUnderlyingScript();
110}
111
113{
114 return static_cast<InjectionPoint>(m_webEngineScript.injectionPoint());
115}
116
117void QmlUserScript::setInjectionPoint(InjectionPoint injectionPoint)
118{
119 setNotNull();
120 switch (static_cast<QWebEngineScript::InjectionPoint>(injectionPoint)) {
121 case QWebEngineScript::DocumentCreation:
122 m_webEngineScript.setInjectionPoint(QWebEngineScript::DocumentCreation);
123 break;
124 case QWebEngineScript::DocumentReady:
125 m_webEngineScript.setInjectionPoint(QWebEngineScript::DocumentReady);
126 break;
127 case QWebEngineScript::Deferred:
128 m_webEngineScript.setInjectionPoint(QWebEngineScript::Deferred);
129 break;
130 default:
131 break;
132 }
134 aboutToUpdateUnderlyingScript();
135}
136
137void QmlUserScript::timerEvent(QTimerEvent *e)
138{
139 if (e->timerId() != m_basicTimer.timerId()) {
140 QObject::timerEvent(e);
141 return;
142 }
143 m_basicTimer.stop();
145}
146
147void QmlUserScript::aboutToUpdateUnderlyingScript()
148{
149 if (!m_basicTimer.isActive()) {
151 }
152 // Defer updates to the next event loop
153 m_basicTimer.start(0, this);
154}
QmlUserScripts * getUserScriptsSingleton()
static QmlStaticData & instance()
InjectionPoint injectionPoint
Injection point of the UserScript.
Definition: qmluserscript.h:55
void nameChanged(const QString &name)
The signal emitted when the script name is changed.
bool runsOnSubFrames
Checks if the UserScript runs on sub frames.
Definition: qmluserscript.h:43
bool null
Checks if the UserScript is null.
Definition: qmluserscript.h:35
void sourceCodeChanged(const QString &sourceCode)
The signal emitted when source code of the script is changed.
int worldId
WorldId of the UserScript.
Definition: qmluserscript.h:47
void runsOnSubFramesChanged(bool runsOnSubFrames)
The signal emitted when runsOnSubFrame property of the script is changed.
QString sourceCode
Source code of the UserScript.
Definition: qmluserscript.h:51
void injectionPointChanged(int injectionPoint)
The signal emitted when injectionPoint property of the script is changed.
QWebEngineScript webEngineScript() const
QmlUserScript(QObject *parent=nullptr)
void worldIdChanged(int worldId)
The signal emitted when worldId property of the script is changed.
void setWebEngineScript(const QWebEngineScript &script)
QString name
Name of the UserScript.
Definition: qmluserscript.h:39
InjectionPoint
The enum exposing QWebEngineScript::InjectionPoint.
Definition: qmluserscript.h:60
~QmlUserScript() override
Q_INVOKABLE void remove(QObject *object) const
Removes a script from collection.
void insert(QObject *object)