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 #pragma once
0019 
0020 #include "qzcommon.h"
0021 
0022 #include <QObject>
0023 #include <QBasicTimer>
0024 #include <QWebEngineScript>
0025 
0026 /**
0027  * @brief The class exposing QWebEngineScript to QML
0028  */
0029 class FALKON_EXPORT QmlUserScript : public QObject
0030 {
0031     Q_OBJECT
0032     /**
0033      * @brief Checks if the UserScript is null
0034      */
0035     Q_PROPERTY(bool null READ null CONSTANT)
0036     /**
0037      * @brief Name of the UserScript
0038      */
0039     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
0040     /**
0041      * @brief Checks if the UserScript runs on sub frames
0042      */
0043     Q_PROPERTY(bool runsOnSubFrames READ runsOnSubFrames WRITE setRunsOnSubFrames NOTIFY runsOnSubFramesChanged)
0044     /**
0045      * @brief WorldId of the UserScript
0046      */
0047     Q_PROPERTY(int worldId READ worldId WRITE setWorldId NOTIFY worldIdChanged)
0048     /**
0049      * @brief Source code of the UserScript
0050      */
0051     Q_PROPERTY(QString sourceCode READ sourceCode WRITE setSourceCode NOTIFY sourceCodeChanged)
0052     /**
0053      * @brief Injection point of the UserScript
0054      */
0055     Q_PROPERTY(InjectionPoint injectionPoint READ injectionPoint WRITE setInjectionPoint NOTIFY injectionPointChanged)
0056 public:
0057     /**
0058      * @brief The enum exposing QWebEngineScript::InjectionPoint
0059      */
0060     enum InjectionPoint {
0061         DocumentCreation = QWebEngineScript::DocumentCreation, //!< Represents QWebEngineScript::DocumentCreation
0062         DocumentReady = QWebEngineScript::DocumentReady,       //!< Represents QWebEngineScript::DocumentReady,
0063         Deferred = QWebEngineScript::Deferred                  //!< Represents QWebEngineScript::Deferred
0064     };
0065     /**
0066      * @brief The enum wrapping QWebEngineScript::ScriptWorldId
0067      */
0068     enum ScriptWorldId {
0069         MainWorld = QWebEngineScript::MainWorld,               //!< Represents QWebEngineScript::MainWorld
0070         ApplicationWorld = QWebEngineScript::ApplicationWorld, //!< Represents QWebEngineScript::ApplicationWorld
0071         UserWorld = QWebEngineScript::UserWorld                //!< Represents QWebEngineScript::UserWorld
0072     };
0073     Q_ENUM(InjectionPoint)
0074     Q_ENUM(ScriptWorldId)
0075 
0076     explicit QmlUserScript(QObject *parent = nullptr);
0077     ~QmlUserScript() override;
0078     QWebEngineScript webEngineScript() const;
0079     void setWebEngineScript(const QWebEngineScript &script);
0080 Q_SIGNALS:
0081     /**
0082      * @brief The signal emitted when the script name is changed
0083      */
0084     void nameChanged(const QString &name);
0085     /**
0086      * @brief The signal emitted when runsOnSubFrame property of the script is changed
0087      */
0088     void runsOnSubFramesChanged(bool runsOnSubFrames);
0089     /**
0090      * @brief The signal emitted when worldId property of the script is changed
0091      */
0092     void worldIdChanged(int worldId);
0093     /**
0094      * @brief The signal emitted when source code of the script is changed
0095      */
0096     void sourceCodeChanged(const QString &sourceCode);
0097     /**
0098      * @brief The signal emitted when injectionPoint property of the script is changed
0099      */
0100     void injectionPointChanged(int injectionPoint);
0101 private:
0102     QWebEngineScript m_webEngineScript;
0103     QBasicTimer m_basicTimer;
0104     bool m_isNull;
0105 
0106     bool null() const;
0107     QString name() const;
0108     void setName(const QString &name);
0109     bool runsOnSubFrames() const;
0110     void setRunsOnSubFrames(bool runsOnSubFrames);
0111     int worldId() const;
0112     void setWorldId(int worldId);
0113     QString sourceCode() const;
0114     void setSourceCode(const QString &sourceCode);
0115     InjectionPoint injectionPoint() const;
0116     void setInjectionPoint(InjectionPoint injectionPoint);
0117 
0118     void timerEvent(QTimerEvent *e) override;
0119     void aboutToUpdateUnderlyingScript();
0120 
0121     void setNotNull()
0122     {
0123         // TODO QT6: This is a really hacky way of keeping track of whether the script has been initialized - and is it even necessary?
0124         m_isNull = false;
0125     }
0126 };