From d902c29bf4e8c8ffeef49b6f0ad1c6f6e7c2f25c Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Fri, 25 Oct 2024 09:43:44 +0200 Subject: [PATCH] GreaseMonkey: Add support for context menu BUG: 469855 Signed-off-by: Juraj Oravec --- src/plugins/GreaseMonkey/gm_manager.cpp | 61 ++++++++++++++++++++----- src/plugins/GreaseMonkey/gm_manager.h | 2 + src/plugins/GreaseMonkey/gm_plugin.cpp | 21 +++++++++ src/plugins/GreaseMonkey/gm_plugin.h | 2 + src/plugins/GreaseMonkey/gm_script.cpp | 3 ++ src/plugins/GreaseMonkey/gm_script.h | 2 +- 6 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/plugins/GreaseMonkey/gm_manager.cpp b/src/plugins/GreaseMonkey/gm_manager.cpp index 59dea6644..1656ba70b 100644 --- a/src/plugins/GreaseMonkey/gm_manager.cpp +++ b/src/plugins/GreaseMonkey/gm_manager.cpp @@ -156,6 +156,11 @@ QList GM_Manager::allScripts() const return m_scripts; } +QList GM_Manager::contextMenuScripts() const +{ + return m_contextMenuScripts; +} + bool GM_Manager::containsScript(const QString &fullName) const { for (GM_Script* script : std::as_const(m_scripts)) { @@ -172,8 +177,13 @@ void GM_Manager::enableScript(GM_Script* script) script->setEnabled(true); m_disabledScripts.removeOne(script->fullName()); - QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); - collection->insert(script->webScript()); + if (script->startAt() == GM_Script::ContextMenu) { + m_contextMenuScripts.append(script); + } + else { + QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); + collection->insert(script->webScript()); + } } void GM_Manager::disableScript(GM_Script* script) @@ -181,9 +191,14 @@ void GM_Manager::disableScript(GM_Script* script) script->setEnabled(false); m_disabledScripts.append(script->fullName()); - QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); - for (const auto &script : collection->find(script->fullName())) { - collection->remove(script); + if (script->startAt() == GM_Script::ContextMenu) { + m_contextMenuScripts.removeOne(script); + } + else { + QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); + for (const auto &script : collection->find(script->fullName())) { + collection->remove(script); + } } } @@ -196,8 +211,13 @@ bool GM_Manager::addScript(GM_Script* script) m_scripts.append(script); connect(script, &GM_Script::scriptChanged, this, &GM_Manager::scriptChanged); - QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); - collection->insert(script->webScript()); + if (script->startAt() == GM_Script::ContextMenu) { + m_contextMenuScripts.append(script); + } + else { + QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); + collection->insert(script->webScript()); + } Q_EMIT scriptsChanged(); return true; @@ -211,9 +231,14 @@ bool GM_Manager::removeScript(GM_Script* script, bool removeFile) m_scripts.removeOne(script); - QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); - for (const auto &script : collection->find(script->fullName())) { - collection->remove(script); + if (script->startAt() == GM_Script::ContextMenu) { + m_contextMenuScripts.removeOne(script); + } + else { + QWebEngineScriptCollection *collection = mApp->webProfile()->scripts(); + for (const auto &script : collection->find(script->fullName())) { + collection->remove(script); + } } m_disabledScripts.removeOne(script->fullName()); @@ -267,6 +292,9 @@ void GM_Manager::load() if (m_disabledScripts.contains(script->fullName())) { script->setEnabled(false); } + else if (script->startAt() == GM_Script::ContextMenu) { + m_contextMenuScripts.append(script); + } else { mApp->webProfile()->scripts()->insert(script->webScript()); } @@ -286,7 +314,18 @@ void GM_Manager::scriptChanged() for (const auto &script : collection->find(script->fullName())) { collection->remove(script); } - collection->insert(script->webScript()); + for (auto &cmScript : m_contextMenuScripts) { + if (cmScript->fullName() == script->fullName()) { + m_contextMenuScripts.removeOne(cmScript); + } + } + + if (script->startAt() == GM_Script::ContextMenu) { + m_contextMenuScripts.append(script); + } + else { + collection->insert(script->webScript()); + } } bool GM_Manager::canRunOnScheme(const QString &scheme) diff --git a/src/plugins/GreaseMonkey/gm_manager.h b/src/plugins/GreaseMonkey/gm_manager.h index 1acfbe73f..d264886b4 100644 --- a/src/plugins/GreaseMonkey/gm_manager.h +++ b/src/plugins/GreaseMonkey/gm_manager.h @@ -51,6 +51,7 @@ public: void unloadPlugin(); QList allScripts() const; + QList contextMenuScripts() const; bool containsScript(const QString &fullName) const; void enableScript(GM_Script* script); @@ -83,6 +84,7 @@ private: QStringList m_disabledScripts; GM_JSObject *m_jsObject; QList m_scripts; + QList m_contextMenuScripts; QHash m_windows; }; diff --git a/src/plugins/GreaseMonkey/gm_plugin.cpp b/src/plugins/GreaseMonkey/gm_plugin.cpp index 9e6a5c04c..4a28a042d 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.cpp +++ b/src/plugins/GreaseMonkey/gm_plugin.cpp @@ -17,12 +17,14 @@ * ============================================================ */ #include "gm_plugin.h" #include "gm_manager.h" +#include "gm_script.h" #include "browserwindow.h" #include "webpage.h" #include "pluginproxy.h" #include "mainapplication.h" #include "tabwidget.h" #include "webtab.h" +#include "webview.h" #include "../config.h" #include @@ -66,6 +68,25 @@ void GM_Plugin::showSettings(QWidget* parent) m_manager->showSettings(parent); } +void GM_Plugin::populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult& r) +{ + if (m_manager->contextMenuScripts().isEmpty()) { + return; + } + + auto* gmMenu = new QMenu(tr("GreaseMonkey")); + gmMenu->setIcon(QIcon(QSL(":gm/data/icon.svg"))); + + auto contextMenuScripts = m_manager->contextMenuScripts(); + for (const auto &script : std::as_const(contextMenuScripts)) { + QAction* action = gmMenu->addAction(script->icon(), script->name(), this, [script, view]() { + view->page()->execJavaScript(script->webScript().sourceCode(), WebPage::SafeJsWorld); + }); + } + + menu->addMenu(gmMenu); +} + bool GM_Plugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) { Q_UNUSED(page) diff --git a/src/plugins/GreaseMonkey/gm_plugin.h b/src/plugins/GreaseMonkey/gm_plugin.h index 8e999130a..9252820b9 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.h +++ b/src/plugins/GreaseMonkey/gm_plugin.h @@ -36,6 +36,8 @@ public: bool testPlugin() override; void showSettings(QWidget* parent = nullptr) override; + void populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult &r) override; + bool acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override; private: diff --git a/src/plugins/GreaseMonkey/gm_script.cpp b/src/plugins/GreaseMonkey/gm_script.cpp index 713cab170..35229a024 100644 --- a/src/plugins/GreaseMonkey/gm_script.cpp +++ b/src/plugins/GreaseMonkey/gm_script.cpp @@ -279,6 +279,9 @@ void GM_Script::parseScript() else if (value == QLatin1String("document-idle")) { m_startAt = DocumentIdle; } + else if (value == QLatin1String("context-menu")) { + m_startAt = ContextMenu; + } } else if (key == QL1S("@icon")) { m_iconUrl = QUrl(value); diff --git a/src/plugins/GreaseMonkey/gm_script.h b/src/plugins/GreaseMonkey/gm_script.h index ea4a12be0..e44e63c4b 100644 --- a/src/plugins/GreaseMonkey/gm_script.h +++ b/src/plugins/GreaseMonkey/gm_script.h @@ -34,7 +34,7 @@ class GM_Script : public QObject public: explicit GM_Script(GM_Manager* manager, const QString &filePath); - enum StartAt { DocumentStart, DocumentEnd, DocumentIdle }; + enum StartAt { DocumentStart, DocumentEnd, DocumentIdle, ContextMenu }; bool isValid() const; QString name() const;