File indexing completed on 2024-12-22 04:41:15
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "qmluserscript.h" 0019 #include "../../qmlstaticdata.h" 0020 0021 QmlUserScript::QmlUserScript(QObject *parent) 0022 : QObject(parent) 0023 , m_isNull(true) 0024 { 0025 } 0026 0027 QmlUserScript::~QmlUserScript() 0028 { 0029 QmlStaticData::instance().getUserScriptsSingleton()->remove(this); 0030 } 0031 0032 QWebEngineScript QmlUserScript::webEngineScript() const 0033 { 0034 return m_webEngineScript; 0035 } 0036 0037 void QmlUserScript::setWebEngineScript(const QWebEngineScript &script) 0038 { 0039 setNotNull(); 0040 m_webEngineScript = script; 0041 } 0042 0043 bool QmlUserScript::null() const 0044 { 0045 return m_isNull; 0046 } 0047 0048 QString QmlUserScript::name() const 0049 { 0050 return m_webEngineScript.name(); 0051 } 0052 0053 void QmlUserScript::setName(const QString &name) 0054 { 0055 setNotNull(); 0056 m_webEngineScript.setName(name); 0057 Q_EMIT nameChanged(name); 0058 aboutToUpdateUnderlyingScript(); 0059 } 0060 0061 bool QmlUserScript::runsOnSubFrames() const 0062 { 0063 return m_webEngineScript.runsOnSubFrames(); 0064 } 0065 0066 void QmlUserScript::setRunsOnSubFrames(bool runsOnSubFrames) 0067 { 0068 setNotNull(); 0069 m_webEngineScript.setRunsOnSubFrames(runsOnSubFrames); 0070 Q_EMIT runsOnSubFramesChanged(runsOnSubFrames); 0071 aboutToUpdateUnderlyingScript(); 0072 } 0073 0074 int QmlUserScript::worldId() const 0075 { 0076 return static_cast<int>(m_webEngineScript.worldId()); 0077 } 0078 0079 void QmlUserScript::setWorldId(int worldId) 0080 { 0081 setNotNull(); 0082 switch (worldId) { 0083 case QWebEngineScript::MainWorld: 0084 m_webEngineScript.setWorldId(QWebEngineScript::MainWorld); 0085 break; 0086 case QWebEngineScript::ApplicationWorld: 0087 m_webEngineScript.setWorldId(QWebEngineScript::ApplicationWorld); 0088 break; 0089 case QWebEngineScript::UserWorld: 0090 m_webEngineScript.setWorldId(QWebEngineScript::UserWorld); 0091 break; 0092 default: 0093 break; 0094 } 0095 Q_EMIT worldIdChanged(worldId); 0096 aboutToUpdateUnderlyingScript(); 0097 } 0098 0099 QString QmlUserScript::sourceCode() const 0100 { 0101 return m_webEngineScript.sourceCode(); 0102 } 0103 0104 void QmlUserScript::setSourceCode(const QString &sourceCode) 0105 { 0106 setNotNull(); 0107 m_webEngineScript.setSourceCode(sourceCode); 0108 Q_EMIT sourceCodeChanged(sourceCode); 0109 aboutToUpdateUnderlyingScript(); 0110 } 0111 0112 QmlUserScript::InjectionPoint QmlUserScript::injectionPoint() const 0113 { 0114 return static_cast<InjectionPoint>(m_webEngineScript.injectionPoint()); 0115 } 0116 0117 void QmlUserScript::setInjectionPoint(InjectionPoint injectionPoint) 0118 { 0119 setNotNull(); 0120 switch (static_cast<QWebEngineScript::InjectionPoint>(injectionPoint)) { 0121 case QWebEngineScript::DocumentCreation: 0122 m_webEngineScript.setInjectionPoint(QWebEngineScript::DocumentCreation); 0123 break; 0124 case QWebEngineScript::DocumentReady: 0125 m_webEngineScript.setInjectionPoint(QWebEngineScript::DocumentReady); 0126 break; 0127 case QWebEngineScript::Deferred: 0128 m_webEngineScript.setInjectionPoint(QWebEngineScript::Deferred); 0129 break; 0130 default: 0131 break; 0132 } 0133 Q_EMIT injectionPointChanged(injectionPoint); 0134 aboutToUpdateUnderlyingScript(); 0135 } 0136 0137 void QmlUserScript::timerEvent(QTimerEvent *e) 0138 { 0139 if (e->timerId() != m_basicTimer.timerId()) { 0140 QObject::timerEvent(e); 0141 return; 0142 } 0143 m_basicTimer.stop(); 0144 QmlStaticData::instance().getUserScriptsSingleton()->insert(this); 0145 } 0146 0147 void QmlUserScript::aboutToUpdateUnderlyingScript() 0148 { 0149 if (!m_basicTimer.isActive()) { 0150 QmlStaticData::instance().getUserScriptsSingleton()->remove(this); 0151 } 0152 // Defer updates to the next event loop 0153 m_basicTimer.start(0, this); 0154 }