From d3b1271e6d9eceda120ef3a7609cfa9f5c242cb8 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Mon, 28 Oct 2024 21:05:11 +0100 Subject: [PATCH] GreaseMonkey: Add ex/in-clude match to ContextMenu Signed-off-by: Juraj Oravec --- src/plugins/GreaseMonkey/gm_plugin.cpp | 21 +++++++++++++--- src/plugins/GreaseMonkey/gm_script.cpp | 34 ++++++++++++++++++++++++++ src/plugins/GreaseMonkey/gm_script.h | 2 ++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/plugins/GreaseMonkey/gm_plugin.cpp b/src/plugins/GreaseMonkey/gm_plugin.cpp index 4a28a042d..5700e844d 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.cpp +++ b/src/plugins/GreaseMonkey/gm_plugin.cpp @@ -70,16 +70,31 @@ void GM_Plugin::showSettings(QWidget* parent) void GM_Plugin::populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult& r) { + Q_UNUSED(r) + if (m_manager->contextMenuScripts().isEmpty()) { return; } + const QUrl url = view->url(); + QList matchingScripts; + + auto contextMenuScripts = m_manager->contextMenuScripts(); + for (const auto &script : std::as_const(contextMenuScripts)) { + if (script->match(url)) { + matchingScripts.append(script); + } + } + + if (matchingScripts.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]() { + for (const auto &script : std::as_const(matchingScripts)) { + gmMenu->addAction(script->icon(), script->name(), this, [script, view]() { view->page()->execJavaScript(script->webScript().sourceCode(), WebPage::SafeJsWorld); }); } diff --git a/src/plugins/GreaseMonkey/gm_script.cpp b/src/plugins/GreaseMonkey/gm_script.cpp index 35229a024..f1217b00d 100644 --- a/src/plugins/GreaseMonkey/gm_script.cpp +++ b/src/plugins/GreaseMonkey/gm_script.cpp @@ -31,6 +31,7 @@ #include #include #include +#include GM_Script::GM_Script(GM_Manager* manager, const QString &filePath) : QObject(manager) @@ -175,6 +176,39 @@ void GM_Script::updateScript() downloadRequires(); } +bool GM_Script::match(const QUrl& url) const +{ + QString urlString = url.toString(); + + for (const auto &exclude : std::as_const(m_exclude)) { + QString wildcardExp = QRegularExpression::wildcardToRegularExpression( + exclude, + QRegularExpression::UnanchoredWildcardConversion + ); + QRegularExpression re(wildcardExp, + QRegularExpression::CaseInsensitiveOption); + + if (re.match(urlString).hasMatch()) { + return false; + } + } + + for (const auto &include : std::as_const(m_include)) { + QString wildcardExp = QRegularExpression::wildcardToRegularExpression( + include, + QRegularExpression::UnanchoredWildcardConversion + ); + QRegularExpression re(wildcardExp, + QRegularExpression::CaseInsensitiveOption); + + if (re.match(urlString).hasMatch()) { + return true; + } + } + + return false; +} + void GM_Script::watchedFileChanged(const QString &file) { if (m_fileName == file) { diff --git a/src/plugins/GreaseMonkey/gm_script.h b/src/plugins/GreaseMonkey/gm_script.h index e44e63c4b..a05094970 100644 --- a/src/plugins/GreaseMonkey/gm_script.h +++ b/src/plugins/GreaseMonkey/gm_script.h @@ -68,6 +68,8 @@ public: bool isUpdating(); void updateScript(); + bool match(const QUrl &url) const; + Q_SIGNALS: void scriptChanged(); void updatingChanged(bool updating);