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

GreaseMonkey: Add support for GreaseMonkey 4.0 API

Properly implement values functions in 4.0 API version.

Closes #2355
This commit is contained in:
David Rosca 2018-01-03 16:22:53 +01:00
parent 6a5b7d30e3
commit 7ce93354c8
10 changed files with 178 additions and 77 deletions

View File

@ -7,11 +7,12 @@ set( GreaseMonkey_SRCS
gm_downloader.cpp gm_downloader.cpp
gm_addscriptdialog.cpp gm_addscriptdialog.cpp
gm_notification.cpp gm_notification.cpp
gm_icon.cpp
gm_jsobject.cpp
settings/gm_settings.cpp settings/gm_settings.cpp
settings/gm_settingslistdelegate.cpp settings/gm_settingslistdelegate.cpp
settings/gm_settingsscriptinfo.cpp settings/gm_settingsscriptinfo.cpp
settings/gm_settingslistwidget.cpp settings/gm_settingslistwidget.cpp
gm_icon.cpp
) )
set( GreaseMonkey_UIS set( GreaseMonkey_UIS

View File

@ -1,5 +1,28 @@
if (typeof GM_xmlhttpRequest === "undefined") { var GM = {
GM_xmlhttpRequest = function(/* object */ details) { info: {
script: {
description: "",
excludes: [],
includes: [],
matches: [],
name: "",
namespace: "",
resources: {},
'run-at': "document-end",
version: ""
},
scriptMetaStr: "",
scriptHandler: "QupZilla GreaseMonkey",
version: "4.0"
}
};
window.GM = GM;
function GM_info() {
return GM.info;
}
function GM_xmlhttpRequest(/* object */ details) {
details.method = details.method.toUpperCase() || "GET"; details.method = details.method.toUpperCase() || "GET";
if (!details.url) { if (!details.url) {
@ -27,9 +50,7 @@ if (typeof GM_xmlhttpRequest === "undefined") {
else else
oXhr.send(); oXhr.send();
} }
}
if (typeof GM_addStyle === "undefined") {
function GM_addStyle(/* string */ styles) { function GM_addStyle(/* string */ styles) {
var head = document.getElementsByTagName("head")[0]; var head = document.getElementsByTagName("head")[0];
if (head === undefined) { if (head === undefined) {
@ -49,25 +70,18 @@ if (typeof GM_addStyle === "undefined") {
head.appendChild(oStyle); head.appendChild(oStyle);
} }
} }
}
if (typeof GM_log === "undefined") {
function GM_log(log) { function GM_log(log) {
if(console) if(console)
console.log(log); console.log(log);
} }
}
if (typeof GM_openInTab === "undefined") {
function GM_openInTab(url) { function GM_openInTab(url) {
return window.open(url); return window.open(url);
} }
}
if (typeof GM_setClipboard === "undefined") {
function GM_setClipboard(text) { function GM_setClipboard(text) {
//window._qz_greasemonkey.setClipboard(text); window.external.extra.greasemonkey.setClipboard(text);
}
} }
// Define unsafe window // Define unsafe window
@ -75,6 +89,19 @@ var unsafeWindow = window;
window.wrappedJSObject = unsafeWindow; window.wrappedJSObject = unsafeWindow;
// GM_registerMenuCommand not supported // GM_registerMenuCommand not supported
if (typeof GM_registerMenuCommand === "undefined") {
function GM_registerMenuCommand(caption, commandFunc, accessKey) { } function GM_registerMenuCommand(caption, commandFunc, accessKey) { }
}
// GM_getResourceUrl not supported
function GM_getResourceUrl(resourceName) { }
// GreaseMonkey 4.0 support
GM.openInTab = GM_openInTab;
GM.setClipboard = GM_setClipboard;
GM.xmlhttpRequest = GM_xmlhttpRequest;
// GM_getResourceUrl not supported
GM.getResourceUrl = function(resourceName) {
return new Promise((resolve, reject) => {
reject();
});
};

View File

@ -1 +1,4 @@
"undefined"===typeof GM_xmlhttpRequest&&(GM_xmlhttpRequest=function(a){a.method=a.method.toUpperCase()||"GET";if(!a.url)throw"GM_xmlhttpRequest requires an URL.";var b=new XMLHttpRequest;"onreadystatechange"in a&&(b.onreadystatechange=function(){a.onreadystatechange(b)});"onload"in a&&(b.onload=function(){a.onload(b)});"onerror"in a&&(b.onerror=function(){a.onerror(b)});b.open(a.method,a.url,!0);if("headers"in a)for(var c in a.headers)b.setRequestHeader(c,a.headers[c]);"data"in a?b.send(a.data):b.send()});if("undefined"===typeof GM_addStyle)var GM_addStyle=function(a){var b=document.getElementsByTagName("head")[0];if(void 0===b)document.onreadystatechange=function(){if("interactive"==document.readyState){var b=document.createElement("style");b.setAttribute("type","text/css");b.appendChild(document.createTextNode(a));document.getElementsByTagName("head")[0].appendChild(b)}};else{var c=document.createElement("style");c.setAttribute("type","text/css");c.appendChild(document.createTextNode(a));b.appendChild(c)}};if("undefined"===typeof GM_log)var GM_log=function(a){console&&console.log(a)};if("undefined"===typeof GM_openInTab)var GM_openInTab=function(a){return window.open(a)};if("undefined"===typeof GM_setClipboard)var GM_setClipboard=function(a){};var unsafeWindow=window;window.wrappedJSObject=unsafeWindow;if("undefined"===typeof GM_registerMenuCommand)var GM_registerMenuCommand=function(a,b,c){}; var GM={info:{script:{description:"",excludes:[],includes:[],matches:[],name:"",namespace:"",resources:{},"run-at":"document-end",version:""},scriptMetaStr:"",scriptHandler:"QupZilla GreaseMonkey",version:"4.0"}};window.GM=GM;function GM_info(){return GM.info}
function GM_xmlhttpRequest(a){a.method=a.method.toUpperCase()||"GET";if(!a.url)throw"GM_xmlhttpRequest requires an URL.";var b=new XMLHttpRequest;"onreadystatechange"in a&&(b.onreadystatechange=function(){a.onreadystatechange(b)});"onload"in a&&(b.onload=function(){a.onload(b)});"onerror"in a&&(b.onerror=function(){a.onerror(b)});b.open(a.method,a.url,!0);if("headers"in a)for(var c in a.headers)b.setRequestHeader(c,a.headers[c]);"data"in a?b.send(a.data):b.send()}
function GM_addStyle(a){var b=document.getElementsByTagName("head")[0];if(void 0===b)document.onreadystatechange=function(){if("interactive"==document.readyState){var b=document.createElement("style");b.setAttribute("type","text/css");b.appendChild(document.createTextNode(a));document.getElementsByTagName("head")[0].appendChild(b)}};else{var c=document.createElement("style");c.setAttribute("type","text/css");c.appendChild(document.createTextNode(a));b.appendChild(c)}}
function GM_log(a){console&&console.log(a)}function GM_openInTab(a){return window.open(a)}function GM_setClipboard(a){window.external.extra.greasemonkey.setClipboard(a)}var unsafeWindow=window;window.wrappedJSObject=unsafeWindow;function GM_registerMenuCommand(a,b,c){}function GM_getResourceUrl(a){}GM.openInTab=GM_openInTab;GM.setClipboard=GM_setClipboard;GM.xmlhttpRequest=GM_xmlhttpRequest;GM.getResourceUrl=function(a){return new Promise(function(a,c){c()})};

View File

@ -26,3 +26,56 @@ function GM_listValues() {
function GM_setValue(aKey, aVal) { function GM_setValue(aKey, aVal) {
localStorage.setItem("%1" + aKey, aVal); localStorage.setItem("%1" + aKey, aVal);
} }
// GreaseMonkey 4.0 support
var asyncCall = (func) => {
if (window.external.extra) {
func();
} else {
document.addEventListener("_qupzilla_external_created", func);
}
};
GM.deleteValue = function(name) {
return new Promise((resolve, reject) => {
asyncCall(() => {
window.external.extra.greasemonkey.deleteValue("%1", name, (res) => {
if (res) {
resolve();
} else {
reject();
}
});
});
});
};
GM.getValue = function(name, value) {
return new Promise((resolve) => {
asyncCall(() => {
window.external.extra.greasemonkey.getValue("%1", name, value, resolve);
});
});
};
GM.setValue = function(name, value) {
return new Promise((resolve, reject) => {
asyncCall(() => {
window.external.extra.greasemonkey.setValue("%1", name, value, (res) => {
if (res) {
resolve();
} else {
reject();
}
});
});
});
};
GM.listValues = function() {
return new Promise((resolve) => {
asyncCall(() => {
window.external.extra.greasemonkey.listValues("%1", resolve);
});
});
};

View File

@ -1 +1,3 @@
function GM_deleteValue(a){localStorage.removeItem("%1"+a)}function GM_getValue(a,b){var c=localStorage.getItem("%1"+a);return null===c&&"undefined"!=typeof b?b:c}function GM_listValues(){for(var a=[],b=0;b<localStorage.length;b++){var c=localStorage.key(b);0===c.indexOf("%1")&&a.push(c.replace("%1",""))}return a}function GM_setValue(a,b){localStorage.setItem("%1"+a,b)}; function GM_deleteValue(a){localStorage.removeItem("%1"+a)}function GM_getValue(a,b){var c=localStorage.getItem("%1"+a);return null===c&&"undefined"!=typeof b?b:c}function GM_listValues(){for(var a=[],b=0;b<localStorage.length;b++){var c=localStorage.key(b);0===c.indexOf("%1")&&a.push(c.replace("%1",""))}return a}function GM_setValue(a,b){localStorage.setItem("%1"+a,b)}var asyncCall=function(a){window.external.extra?a():document.addEventListener("_qupzilla_external_created",a)};
GM.deleteValue=function(a){return new Promise(function(b,c){asyncCall(function(){window.external.extra.greasemonkey.deleteValue("%1",a,function(a){a?b():c()})})})};GM.getValue=function(a,b){return new Promise(function(c){asyncCall(function(){window.external.extra.greasemonkey.getValue("%1",a,b,c)})})};GM.setValue=function(a,b){return new Promise(function(c,d){asyncCall(function(){window.external.extra.greasemonkey.setValue("%1",a,b,function(a){a?c():d()})})})};
GM.listValues=function(){return new Promise(function(a){asyncCall(function(){window.external.extra.greasemonkey.listValues("%1",a)})})};

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* GreaseMonkey plugin for Falkon * GreaseMonkey plugin for Falkon
* Copyright (C) 2013-2014 David Rosca <nowrep@gmail.com> * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -70,7 +70,7 @@ QVariant GM_JSObject::getValue(const QString &nspace, const QString &name, const
return dValue; return dValue;
} }
void GM_JSObject::setValue(const QString &nspace, const QString &name, const QVariant &value) bool GM_JSObject::setValue(const QString &nspace, const QString &name, const QVariant &value)
{ {
QString savedValue; QString savedValue;
@ -92,17 +92,19 @@ void GM_JSObject::setValue(const QString &nspace, const QString &name, const QVa
break; break;
default: default:
break; return false;
} }
QString valueName = QString("GreaseMonkey-%1/%2").arg(nspace, name); QString valueName = QString("GreaseMonkey-%1/%2").arg(nspace, name);
m_settings->setValue(valueName, savedValue); m_settings->setValue(valueName, savedValue);
return true;
} }
void GM_JSObject::deleteValue(const QString &nspace, const QString &name) bool GM_JSObject::deleteValue(const QString &nspace, const QString &name)
{ {
QString valueName = QString("GreaseMonkey-%1/%2").arg(nspace, name); QString valueName = QString("GreaseMonkey-%1/%2").arg(nspace, name);
m_settings->remove(valueName); m_settings->remove(valueName);
return true;
} }
QStringList GM_JSObject::listValues(const QString &nspace) QStringList GM_JSObject::listValues(const QString &nspace)

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* GreaseMonkey plugin for Falkon * GreaseMonkey plugin for Falkon
* Copyright (C) 2013-2014 David Rosca <nowrep@gmail.com> * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,8 +34,8 @@ public:
public slots: public slots:
QVariant getValue(const QString &nspace, const QString &name, const QVariant &dValue); QVariant getValue(const QString &nspace, const QString &name, const QVariant &dValue);
void setValue(const QString &nspace, const QString &name, const QVariant &value); bool setValue(const QString &nspace, const QString &name, const QVariant &value);
void deleteValue(const QString &nspace, const QString &name); bool deleteValue(const QString &nspace, const QString &name);
QStringList listValues(const QString &nspace); QStringList listValues(const QString &nspace);
void setClipboard(const QString &text); void setClipboard(const QString &text);

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* GreaseMonkey plugin for Falkon * GreaseMonkey plugin for Falkon
* Copyright (C) 2012-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2012-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -18,6 +18,7 @@
#include "gm_manager.h" #include "gm_manager.h"
#include "gm_script.h" #include "gm_script.h"
#include "gm_downloader.h" #include "gm_downloader.h"
#include "gm_jsobject.h"
#include "gm_icon.h" #include "gm_icon.h"
#include "gm_addscriptdialog.h" #include "gm_addscriptdialog.h"
#include "settings/gm_settings.h" #include "settings/gm_settings.h"
@ -28,6 +29,7 @@
#include "mainapplication.h" #include "mainapplication.h"
#include "networkmanager.h" #include "networkmanager.h"
#include "desktopnotificationsfactory.h" #include "desktopnotificationsfactory.h"
#include "javascript/externaljsobject.h"
#include <QTimer> #include <QTimer>
#include <QDir> #include <QDir>
@ -39,8 +41,14 @@
GM_Manager::GM_Manager(const QString &sPath, QObject* parent) GM_Manager::GM_Manager(const QString &sPath, QObject* parent)
: QObject(parent) : QObject(parent)
, m_settingsPath(sPath) , m_settingsPath(sPath)
, m_jsObject(new GM_JSObject(this))
{ {
QTimer::singleShot(0, this, SLOT(load())); load();
}
GM_Manager::~GM_Manager()
{
ExternalJsObject::unregisterExtraObject(QSL("greasemonkey"));
} }
void GM_Manager::showSettings(QWidget* parent) void GM_Manager::showSettings(QWidget* parent)
@ -239,6 +247,9 @@ void GM_Manager::load()
mApp->webProfile()->scripts()->insert(script->webScript()); mApp->webProfile()->scripts()->insert(script->webScript());
} }
} }
m_jsObject->setSettingsFile(m_settingsPath + QSL("/greasemonkey/values.ini"));
ExternalJsObject::registerExtraObject(QSL("greasemonkey"), m_jsObject);
} }
void GM_Manager::scriptChanged() void GM_Manager::scriptChanged()

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* GreaseMonkey plugin for Falkon * GreaseMonkey plugin for Falkon
* Copyright (C) 2013-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -28,6 +28,7 @@ class QWebFrame;
class BrowserWindow; class BrowserWindow;
class GM_Script; class GM_Script;
class GM_JSObject;
class GM_Settings; class GM_Settings;
class GM_Icon; class GM_Icon;
@ -36,6 +37,7 @@ class GM_Manager : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit GM_Manager(const QString &sPath, QObject* parent = 0); explicit GM_Manager(const QString &sPath, QObject* parent = 0);
~GM_Manager();
void showSettings(QWidget* parent); void showSettings(QWidget* parent);
void downloadScript(const QUrl &url); void downloadScript(const QUrl &url);
@ -80,7 +82,7 @@ private:
QPointer<GM_Settings> m_settings; QPointer<GM_Settings> m_settings;
QStringList m_disabledScripts; QStringList m_disabledScripts;
//GM_JSObject* m_jsObject; GM_JSObject *m_jsObject;
QList<GM_Script*> m_scripts; QList<GM_Script*> m_scripts;
QHash<BrowserWindow*, GM_Icon*> m_windows; QHash<BrowserWindow*, GM_Icon*> m_windows;

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* GreaseMonkey plugin for Falkon * GreaseMonkey plugin for Falkon
* Copyright (C) 2012-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2012-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -40,7 +40,7 @@ PluginSpec GM_Plugin::pluginSpec()
spec.name = "GreaseMonkey"; spec.name = "GreaseMonkey";
spec.info = "Userscripts for Falkon"; spec.info = "Userscripts for Falkon";
spec.description = "Provides support for userscripts"; spec.description = "Provides support for userscripts";
spec.version = "0.8.0"; spec.version = "0.9.0";
spec.author = "David Rosca <nowrep@gmail.com>"; spec.author = "David Rosca <nowrep@gmail.com>";
spec.icon = QIcon(":gm/data/icon.svg").pixmap(32); spec.icon = QIcon(":gm/data/icon.svg").pixmap(32);
spec.hasSettings = true; spec.hasSettings = true;