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

GreaseMonkey: Add support for context menu

BUG: 469855

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2024-10-25 09:43:44 +02:00
parent e3cf9424cc
commit d902c29bf4
6 changed files with 79 additions and 12 deletions

View File

@ -156,6 +156,11 @@ QList<GM_Script*> GM_Manager::allScripts() const
return m_scripts;
}
QList<GM_Script*> 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)

View File

@ -51,6 +51,7 @@ public:
void unloadPlugin();
QList<GM_Script*> allScripts() const;
QList<GM_Script*> 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<GM_Script*> m_scripts;
QList<GM_Script*> m_contextMenuScripts;
QHash<BrowserWindow*, GM_Icon*> m_windows;
};

View File

@ -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 <QtWebEngineWidgetsVersion>
@ -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)

View File

@ -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:

View File

@ -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);

View File

@ -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;