1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

GreaseMonkey: Correctly run scripts in frames

Closes #1457
This commit is contained in:
David Rosca 2014-09-16 16:58:36 +02:00
parent 0eef26f843
commit 171267cfb0
3 changed files with 29 additions and 29 deletions

View File

@ -194,46 +194,43 @@ void GM_Manager::showNotification(const QString &message, const QString &title)
mApp->desktopNotifications()->showNotification(icon, title.isEmpty() ? tr("GreaseMonkey") : title, message); mApp->desktopNotifications()->showNotification(icon, title.isEmpty() ? tr("GreaseMonkey") : title, message);
} }
void GM_Manager::pageLoadStart() void GM_Manager::frameLoadStart()
{ {
QWebFrame* mainFrame = qobject_cast<QWebFrame*>(sender()); QWebFrame* frame = qobject_cast<QWebFrame*>(sender());
if (!mainFrame) { if (!frame) {
return; return;
} }
const QString urlScheme = mainFrame->url().scheme(); const QUrl url = frame->url().isEmpty() ? frame->baseUrl() : frame->url();
const QString urlString = mainFrame->url().toEncoded(); const QString urlScheme = url.scheme();
const QString urlString = url.toEncoded();
if (!canRunOnScheme(urlScheme)) { if (!canRunOnScheme(urlScheme)) {
return; return;
} }
// Run it in every frame frame->addToJavaScriptWindowObject(QSL("_qz_greasemonkey"), m_jsObject);
QList<QWebFrame*> frames;
frames.append(mainFrame);
while (!frames.isEmpty()) {
QWebFrame* frame = frames.takeFirst();
if (frame) {
mainFrame->addToJavaScriptWindowObject("_qz_greasemonkey", m_jsObject);
foreach (GM_Script* script, m_startScripts) { foreach (GM_Script* script, m_startScripts) {
if (script->match(urlString)) { if (script->match(urlString)) {
mainFrame->evaluateJavaScript(m_bootstrap + script->script()); frame->evaluateJavaScript(m_bootstrap + script->script());
} }
} }
foreach (GM_Script* script, m_endScripts) { foreach (GM_Script* script, m_endScripts) {
if (script->match(urlString)) { if (script->match(urlString)) {
const QString jscript = QString("window.addEventListener(\"DOMContentLoaded\"," const QString jscript = QString(QSL("window.addEventListener(\"DOMContentLoaded\","
"function(e) { \n%1\n }, false);").arg(m_bootstrap + script->script()); "function(e) { \n%1\n }, false);")).arg(m_bootstrap + script->script());
mainFrame->evaluateJavaScript(jscript); frame->evaluateJavaScript(jscript);
}
}
frames += frame->childFrames();
} }
} }
} }
void GM_Manager::frameCreated(QWebFrame *frame)
{
connect(frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(frameLoadStart()));
}
void GM_Manager::load() void GM_Manager::load()
{ {
QDir gmDir(m_settingsPath + QL1S("/greasemonkey")); QDir gmDir(m_settingsPath + QL1S("/greasemonkey"));

View File

@ -24,6 +24,7 @@
#include <QHash> #include <QHash>
class QUrl; class QUrl;
class QWebFrame;
class QNetworkRequest; class QNetworkRequest;
class BrowserWindow; class BrowserWindow;
@ -67,7 +68,8 @@ public slots:
void mainWindowCreated(BrowserWindow* window); void mainWindowCreated(BrowserWindow* window);
void mainWindowDeleted(BrowserWindow* window); void mainWindowDeleted(BrowserWindow* window);
void pageLoadStart(); void frameLoadStart();
void frameCreated(QWebFrame* frame);
private slots: private slots:
void load(); void load();

View File

@ -41,7 +41,7 @@ PluginSpec GM_Plugin::pluginSpec()
spec.name = "GreaseMonkey"; spec.name = "GreaseMonkey";
spec.info = "Userscripts for QupZilla"; spec.info = "Userscripts for QupZilla";
spec.description = "Provides support for userscripts (www.userscripts.org)"; spec.description = "Provides support for userscripts (www.userscripts.org)";
spec.version = "0.4.6"; spec.version = "0.5.0";
spec.author = "David Rosca <nowrep@gmail.com>"; spec.author = "David Rosca <nowrep@gmail.com>";
spec.icon = QPixmap(":gm/data/icon.png"); spec.icon = QPixmap(":gm/data/icon.png");
spec.hasSettings = true; spec.hasSettings = true;
@ -114,7 +114,8 @@ QNetworkReply* GM_Plugin::createRequest(QNetworkAccessManager::Operation op, con
void GM_Plugin::webPageCreated(WebPage* page) void GM_Plugin::webPageCreated(WebPage* page)
{ {
connect(page->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), m_manager, SLOT(pageLoadStart())); m_manager->frameCreated(page->mainFrame());
connect(page, SIGNAL(frameCreated(QWebFrame*)), m_manager, SLOT(frameCreated(QWebFrame*)));
} }
#if QT_VERSION < 0x050000 #if QT_VERSION < 0x050000