mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-15 03:22:11 +01:00
GreaseMonkey: Add ex/in-clude match to ContextMenu
Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
parent
d902c29bf4
commit
d3b1271e6d
|
@ -70,16 +70,31 @@ void GM_Plugin::showSettings(QWidget* parent)
|
||||||
|
|
||||||
void GM_Plugin::populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult& r)
|
void GM_Plugin::populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult& r)
|
||||||
{
|
{
|
||||||
|
Q_UNUSED(r)
|
||||||
|
|
||||||
if (m_manager->contextMenuScripts().isEmpty()) {
|
if (m_manager->contextMenuScripts().isEmpty()) {
|
||||||
return;
|
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"));
|
auto* gmMenu = new QMenu(tr("GreaseMonkey"));
|
||||||
gmMenu->setIcon(QIcon(QSL(":gm/data/icon.svg")));
|
gmMenu->setIcon(QIcon(QSL(":gm/data/icon.svg")));
|
||||||
|
|
||||||
auto contextMenuScripts = m_manager->contextMenuScripts();
|
for (const auto &script : std::as_const(matchingScripts)) {
|
||||||
for (const auto &script : std::as_const(contextMenuScripts)) {
|
gmMenu->addAction(script->icon(), script->name(), this, [script, view]() {
|
||||||
QAction* action = gmMenu->addAction(script->icon(), script->name(), this, [script, view]() {
|
|
||||||
view->page()->execJavaScript(script->webScript().sourceCode(), WebPage::SafeJsWorld);
|
view->page()->execJavaScript(script->webScript().sourceCode(), WebPage::SafeJsWorld);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
|
||||||
GM_Script::GM_Script(GM_Manager* manager, const QString &filePath)
|
GM_Script::GM_Script(GM_Manager* manager, const QString &filePath)
|
||||||
: QObject(manager)
|
: QObject(manager)
|
||||||
|
@ -175,6 +176,39 @@ void GM_Script::updateScript()
|
||||||
downloadRequires();
|
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)
|
void GM_Script::watchedFileChanged(const QString &file)
|
||||||
{
|
{
|
||||||
if (m_fileName == file) {
|
if (m_fileName == file) {
|
||||||
|
|
|
@ -68,6 +68,8 @@ public:
|
||||||
bool isUpdating();
|
bool isUpdating();
|
||||||
void updateScript();
|
void updateScript();
|
||||||
|
|
||||||
|
bool match(const QUrl &url) const;
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void scriptChanged();
|
void scriptChanged();
|
||||||
void updatingChanged(bool updating);
|
void updatingChanged(bool updating);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user