From 9c7bfb1c94c6cc566c8f6fc16985faad1aa7d584 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 27 May 2015 19:48:40 +0200 Subject: [PATCH] GreaseMonkey: Fix removing/updating userscripts --- src/plugins/GreaseMonkey/gm_manager.cpp | 26 +++++++++++++++++++++---- src/plugins/GreaseMonkey/gm_manager.h | 1 + src/plugins/GreaseMonkey/gm_script.cpp | 16 +++++++-------- src/plugins/GreaseMonkey/gm_script.h | 5 +---- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/plugins/GreaseMonkey/gm_manager.cpp b/src/plugins/GreaseMonkey/gm_manager.cpp index 5d3018dd6..2be6d40d6 100644 --- a/src/plugins/GreaseMonkey/gm_manager.cpp +++ b/src/plugins/GreaseMonkey/gm_manager.cpp @@ -138,7 +138,8 @@ void GM_Manager::enableScript(GM_Script* script) script->setEnabled(true); m_disabledScripts.removeOne(script->fullName()); - mApp->webProfile()->scripts().insert(script->webScript()); + QWebEngineScriptCollection &collection = mApp->webProfile()->scripts(); + collection.insert(script->webScript()); } void GM_Manager::disableScript(GM_Script* script) @@ -146,7 +147,8 @@ void GM_Manager::disableScript(GM_Script* script) script->setEnabled(false); m_disabledScripts.append(script->fullName()); - mApp->webProfile()->scripts().remove(script->webScript()); + QWebEngineScriptCollection &collection = mApp->webProfile()->scripts(); + collection.remove(collection.findScript(script->fullName())); } bool GM_Manager::addScript(GM_Script* script) @@ -156,7 +158,10 @@ bool GM_Manager::addScript(GM_Script* script) } m_scripts.append(script); - mApp->webProfile()->scripts().insert(script->webScript()); + connect(script, &GM_Script::scriptChanged, this, &GM_Manager::scriptChanged); + + QWebEngineScriptCollection &collection = mApp->webProfile()->scripts(); + collection.insert(script->webScript()); emit scriptsChanged(); return true; @@ -169,7 +174,9 @@ bool GM_Manager::removeScript(GM_Script* script, bool removeFile) } m_scripts.removeOne(script); - mApp->webProfile()->scripts().insert(script->webScript()); + + QWebEngineScriptCollection &collection = mApp->webProfile()->scripts(); + collection.remove(collection.findScript(script->fullName())); m_disabledScripts.removeOne(script->fullName()); @@ -229,6 +236,17 @@ void GM_Manager::load() //m_jsObject->setSettingsFile(m_settingsPath + QL1S("/extensions.ini")); } +void GM_Manager::scriptChanged() +{ + GM_Script *script = qobject_cast(sender()); + if (!script) + return; + + QWebEngineScriptCollection &collection = mApp->webProfile()->scripts(); + collection.remove(collection.findScript(script->fullName())); + collection.insert(script->webScript()); +} + bool GM_Manager::canRunOnScheme(const QString &scheme) { return (scheme == QLatin1String("http") || scheme == QLatin1String("https") diff --git a/src/plugins/GreaseMonkey/gm_manager.h b/src/plugins/GreaseMonkey/gm_manager.h index 5064d97b4..87e2ca0f8 100644 --- a/src/plugins/GreaseMonkey/gm_manager.h +++ b/src/plugins/GreaseMonkey/gm_manager.h @@ -71,6 +71,7 @@ public slots: private slots: void load(); + void scriptChanged(); private: QString m_settingsPath; diff --git a/src/plugins/GreaseMonkey/gm_script.cpp b/src/plugins/GreaseMonkey/gm_script.cpp index d84b45ec4..bd291957f 100644 --- a/src/plugins/GreaseMonkey/gm_script.cpp +++ b/src/plugins/GreaseMonkey/gm_script.cpp @@ -23,6 +23,7 @@ #include #include +#include #include GM_Script::GM_Script(GM_Manager* manager, const QString &filePath) @@ -135,7 +136,13 @@ QString GM_Script::fileName() const QWebEngineScript GM_Script::webScript() const { - return m_webScript; + QWebEngineScript script; + script.setName(fullName()); + script.setInjectionPoint(startAt() == DocumentStart ? QWebEngineScript::DocumentCreation : QWebEngineScript::DocumentReady); + script.setWorldId(QWebEngineScript::MainWorld); + script.setRunsOnSubFrames(!m_noframes); + script.setSourceCode(QSL("%1\n%2\n%3").arg(m_metadata, m_manager->bootstrapScript(), m_script)); + return script; } bool GM_Script::match(const QString &urlString) @@ -285,11 +292,4 @@ void GM_Script::parseScript() const QString gmValues = m_manager->valuesScript().arg(nspace); m_script = QSL("(function(){%1\n%2\n%3\n})();").arg(gmValues, m_manager->requireScripts(requireList), script); - - // Create QWebEngineScript - m_webScript.setName(fullName()); - m_webScript.setInjectionPoint(startAt() == DocumentStart ? QWebEngineScript::DocumentCreation : QWebEngineScript::DocumentReady); - m_webScript.setWorldId(QWebEngineScript::MainWorld); - m_webScript.setRunsOnSubFrames(!m_noframes); - m_webScript.setSourceCode(QSL("%1\n%2\n%3").arg(m_metadata, m_manager->bootstrapScript(), m_script)); } diff --git a/src/plugins/GreaseMonkey/gm_script.h b/src/plugins/GreaseMonkey/gm_script.h index 953d31fe0..a4391f694 100644 --- a/src/plugins/GreaseMonkey/gm_script.h +++ b/src/plugins/GreaseMonkey/gm_script.h @@ -23,9 +23,8 @@ #include #include #include -#include -class QWebFrame; +class QWebEngineScript; class GM_Manager; class GM_UrlMatcher; @@ -95,8 +94,6 @@ private: QString m_fileName; bool m_enabled; bool m_valid; - - QWebEngineScript m_webScript; }; #endif // GM_SCRIPT_H