mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-14 19:12:11 +01:00
GreaseMonkey: Add support for context menu
BUG: 469855 Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
parent
e3cf9424cc
commit
d902c29bf4
|
@ -156,6 +156,11 @@ QList<GM_Script*> GM_Manager::allScripts() const
|
||||||
return m_scripts;
|
return m_scripts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<GM_Script*> GM_Manager::contextMenuScripts() const
|
||||||
|
{
|
||||||
|
return m_contextMenuScripts;
|
||||||
|
}
|
||||||
|
|
||||||
bool GM_Manager::containsScript(const QString &fullName) const
|
bool GM_Manager::containsScript(const QString &fullName) const
|
||||||
{
|
{
|
||||||
for (GM_Script* script : std::as_const(m_scripts)) {
|
for (GM_Script* script : std::as_const(m_scripts)) {
|
||||||
|
@ -172,8 +177,13 @@ void GM_Manager::enableScript(GM_Script* script)
|
||||||
script->setEnabled(true);
|
script->setEnabled(true);
|
||||||
m_disabledScripts.removeOne(script->fullName());
|
m_disabledScripts.removeOne(script->fullName());
|
||||||
|
|
||||||
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
if (script->startAt() == GM_Script::ContextMenu) {
|
||||||
collection->insert(script->webScript());
|
m_contextMenuScripts.append(script);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
||||||
|
collection->insert(script->webScript());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GM_Manager::disableScript(GM_Script* script)
|
void GM_Manager::disableScript(GM_Script* script)
|
||||||
|
@ -181,9 +191,14 @@ void GM_Manager::disableScript(GM_Script* script)
|
||||||
script->setEnabled(false);
|
script->setEnabled(false);
|
||||||
m_disabledScripts.append(script->fullName());
|
m_disabledScripts.append(script->fullName());
|
||||||
|
|
||||||
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
if (script->startAt() == GM_Script::ContextMenu) {
|
||||||
for (const auto &script : collection->find(script->fullName())) {
|
m_contextMenuScripts.removeOne(script);
|
||||||
collection->remove(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);
|
m_scripts.append(script);
|
||||||
connect(script, &GM_Script::scriptChanged, this, &GM_Manager::scriptChanged);
|
connect(script, &GM_Script::scriptChanged, this, &GM_Manager::scriptChanged);
|
||||||
|
|
||||||
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
if (script->startAt() == GM_Script::ContextMenu) {
|
||||||
collection->insert(script->webScript());
|
m_contextMenuScripts.append(script);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
||||||
|
collection->insert(script->webScript());
|
||||||
|
}
|
||||||
|
|
||||||
Q_EMIT scriptsChanged();
|
Q_EMIT scriptsChanged();
|
||||||
return true;
|
return true;
|
||||||
|
@ -211,9 +231,14 @@ bool GM_Manager::removeScript(GM_Script* script, bool removeFile)
|
||||||
|
|
||||||
m_scripts.removeOne(script);
|
m_scripts.removeOne(script);
|
||||||
|
|
||||||
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
if (script->startAt() == GM_Script::ContextMenu) {
|
||||||
for (const auto &script : collection->find(script->fullName())) {
|
m_contextMenuScripts.removeOne(script);
|
||||||
collection->remove(script);
|
}
|
||||||
|
else {
|
||||||
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
||||||
|
for (const auto &script : collection->find(script->fullName())) {
|
||||||
|
collection->remove(script);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_disabledScripts.removeOne(script->fullName());
|
m_disabledScripts.removeOne(script->fullName());
|
||||||
|
@ -267,6 +292,9 @@ void GM_Manager::load()
|
||||||
if (m_disabledScripts.contains(script->fullName())) {
|
if (m_disabledScripts.contains(script->fullName())) {
|
||||||
script->setEnabled(false);
|
script->setEnabled(false);
|
||||||
}
|
}
|
||||||
|
else if (script->startAt() == GM_Script::ContextMenu) {
|
||||||
|
m_contextMenuScripts.append(script);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
mApp->webProfile()->scripts()->insert(script->webScript());
|
mApp->webProfile()->scripts()->insert(script->webScript());
|
||||||
}
|
}
|
||||||
|
@ -286,7 +314,18 @@ void GM_Manager::scriptChanged()
|
||||||
for (const auto &script : collection->find(script->fullName())) {
|
for (const auto &script : collection->find(script->fullName())) {
|
||||||
collection->remove(script);
|
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)
|
bool GM_Manager::canRunOnScheme(const QString &scheme)
|
||||||
|
|
|
@ -51,6 +51,7 @@ public:
|
||||||
void unloadPlugin();
|
void unloadPlugin();
|
||||||
|
|
||||||
QList<GM_Script*> allScripts() const;
|
QList<GM_Script*> allScripts() const;
|
||||||
|
QList<GM_Script*> contextMenuScripts() const;
|
||||||
bool containsScript(const QString &fullName) const;
|
bool containsScript(const QString &fullName) const;
|
||||||
|
|
||||||
void enableScript(GM_Script* script);
|
void enableScript(GM_Script* script);
|
||||||
|
@ -83,6 +84,7 @@ private:
|
||||||
QStringList m_disabledScripts;
|
QStringList m_disabledScripts;
|
||||||
GM_JSObject *m_jsObject;
|
GM_JSObject *m_jsObject;
|
||||||
QList<GM_Script*> m_scripts;
|
QList<GM_Script*> m_scripts;
|
||||||
|
QList<GM_Script*> m_contextMenuScripts;
|
||||||
|
|
||||||
QHash<BrowserWindow*, GM_Icon*> m_windows;
|
QHash<BrowserWindow*, GM_Icon*> m_windows;
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,12 +17,14 @@
|
||||||
* ============================================================ */
|
* ============================================================ */
|
||||||
#include "gm_plugin.h"
|
#include "gm_plugin.h"
|
||||||
#include "gm_manager.h"
|
#include "gm_manager.h"
|
||||||
|
#include "gm_script.h"
|
||||||
#include "browserwindow.h"
|
#include "browserwindow.h"
|
||||||
#include "webpage.h"
|
#include "webpage.h"
|
||||||
#include "pluginproxy.h"
|
#include "pluginproxy.h"
|
||||||
#include "mainapplication.h"
|
#include "mainapplication.h"
|
||||||
#include "tabwidget.h"
|
#include "tabwidget.h"
|
||||||
#include "webtab.h"
|
#include "webtab.h"
|
||||||
|
#include "webview.h"
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
|
|
||||||
#include <QtWebEngineWidgetsVersion>
|
#include <QtWebEngineWidgetsVersion>
|
||||||
|
@ -66,6 +68,25 @@ void GM_Plugin::showSettings(QWidget* parent)
|
||||||
m_manager->showSettings(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)
|
bool GM_Plugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
|
||||||
{
|
{
|
||||||
Q_UNUSED(page)
|
Q_UNUSED(page)
|
||||||
|
|
|
@ -36,6 +36,8 @@ public:
|
||||||
bool testPlugin() override;
|
bool testPlugin() override;
|
||||||
void showSettings(QWidget* parent = nullptr) 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;
|
bool acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -279,6 +279,9 @@ void GM_Script::parseScript()
|
||||||
else if (value == QLatin1String("document-idle")) {
|
else if (value == QLatin1String("document-idle")) {
|
||||||
m_startAt = DocumentIdle;
|
m_startAt = DocumentIdle;
|
||||||
}
|
}
|
||||||
|
else if (value == QLatin1String("context-menu")) {
|
||||||
|
m_startAt = ContextMenu;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (key == QL1S("@icon")) {
|
else if (key == QL1S("@icon")) {
|
||||||
m_iconUrl = QUrl(value);
|
m_iconUrl = QUrl(value);
|
||||||
|
|
|
@ -34,7 +34,7 @@ class GM_Script : public QObject
|
||||||
public:
|
public:
|
||||||
explicit GM_Script(GM_Manager* manager, const QString &filePath);
|
explicit GM_Script(GM_Manager* manager, const QString &filePath);
|
||||||
|
|
||||||
enum StartAt { DocumentStart, DocumentEnd, DocumentIdle };
|
enum StartAt { DocumentStart, DocumentEnd, DocumentIdle, ContextMenu };
|
||||||
|
|
||||||
bool isValid() const;
|
bool isValid() const;
|
||||||
QString name() const;
|
QString name() const;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user