1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-19 18:26:34 +01:00

GreaseMonkey: Add ex/in-clude match to ContextMenu

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2024-10-28 21:05:11 +01:00
parent d902c29bf4
commit d3b1271e6d
3 changed files with 54 additions and 3 deletions

View File

@ -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<GM_Script*> 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);
});
}

View File

@ -31,6 +31,7 @@
#include <QCryptographicHash>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QRegularExpression>
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) {

View File

@ -68,6 +68,8 @@ public:
bool isUpdating();
void updateScript();
bool match(const QUrl &url) const;
Q_SIGNALS:
void scriptChanged();
void updatingChanged(bool updating);