mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-11 01:22:10 +01:00
GreaseMonkey: Implement GM_ functions for userscripts
This commit is contained in:
parent
f95d46953f
commit
038e4f5014
91
src/plugins/GreaseMonkey/data/bootstrap.js
vendored
91
src/plugins/GreaseMonkey/data/bootstrap.js
vendored
|
@ -1,45 +1,42 @@
|
|||
if(typeof GM_xmlhttpRequest === "undefined") {
|
||||
if (typeof GM_xmlhttpRequest === "undefined") {
|
||||
GM_xmlhttpRequest = function(/* object */ details) {
|
||||
details.method = details.method.toUpperCase() || "GET";
|
||||
|
||||
if(!details.url) {
|
||||
if (!details.url) {
|
||||
throw("GM_xmlhttpRequest requires an URL.");
|
||||
}
|
||||
|
||||
// build XMLHttpRequest object
|
||||
var oXhr = new XMLHttpRequest;
|
||||
// run it
|
||||
if(oXhr) {
|
||||
if("onreadystatechange" in details)
|
||||
oXhr.onreadystatechange = function() { details.onreadystatechange(oXhr) };
|
||||
if("onload" in details)
|
||||
oXhr.onload = function() { details.onload(oXhr) };
|
||||
if("onerror" in details)
|
||||
oXhr.onerror = function() { details.onerror(oXhr) };
|
||||
if("onreadystatechange" in details)
|
||||
oXhr.onreadystatechange = function() { details.onreadystatechange(oXhr) };
|
||||
if("onload" in details)
|
||||
oXhr.onload = function() { details.onload(oXhr) };
|
||||
if("onerror" in details)
|
||||
oXhr.onerror = function() { details.onerror(oXhr) };
|
||||
|
||||
oXhr.open(details.method, details.url, true);
|
||||
oXhr.open(details.method, details.url, true);
|
||||
|
||||
if("headers" in details)
|
||||
for(var header in details.headers)
|
||||
oXhr.setRequestHeader(header, details.headers[header]);
|
||||
if("headers" in details)
|
||||
for(var header in details.headers)
|
||||
oXhr.setRequestHeader(header, details.headers[header]);
|
||||
|
||||
if("data" in details)
|
||||
oXhr.send(details.data);
|
||||
else
|
||||
oXhr.send();
|
||||
} else
|
||||
throw ("This Browser is not supported, please upgrade.")
|
||||
if("data" in details)
|
||||
oXhr.send(details.data);
|
||||
else
|
||||
oXhr.send();
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_addStyle === "undefined") {
|
||||
function GM_addStyle(/* String */ styles) {
|
||||
if (typeof GM_addStyle === "undefined") {
|
||||
function GM_addStyle(/* string */ styles) {
|
||||
var head = document.getElementsByTagName("head")[0];
|
||||
if (head === undefined) {
|
||||
document.onreadystatechange = function() {
|
||||
if (document.readyState == "interactive") {
|
||||
var oStyle = document.createElement("style");
|
||||
oStyle.setAttribute("type", "text\/css");
|
||||
oStyle.setAttribute("type", "text/css");
|
||||
oStyle.appendChild(document.createTextNode(styles));
|
||||
document.getElementsByTagName("head")[0].appendChild(oStyle);
|
||||
}
|
||||
|
@ -47,54 +44,29 @@ if(typeof GM_addStyle === "undefined") {
|
|||
}
|
||||
else {
|
||||
var oStyle = document.createElement("style");
|
||||
oStyle.setAttribute("type", "text\/css");
|
||||
oStyle.setAttribute("type", "text/css");
|
||||
oStyle.appendChild(document.createTextNode(styles));
|
||||
head.appendChild(oStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_log === "undefined") {
|
||||
if (typeof GM_log === "undefined") {
|
||||
function GM_log(log) {
|
||||
if(console)
|
||||
console.log(log);
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_openInTab === "undefined") {
|
||||
if (typeof GM_openInTab === "undefined") {
|
||||
function GM_openInTab(url) {
|
||||
return window.open(url);
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_setClipboard === "undefined") {
|
||||
if (typeof GM_setClipboard === "undefined") {
|
||||
function GM_setClipboard(text) {
|
||||
window._qz_greasemonkey.setClipboard(text);
|
||||
}
|
||||
}
|
||||
|
||||
// GM Settings Impl
|
||||
if(typeof GM_getValueImpl === "undefined") {
|
||||
function GM_getValueImpl(namespace, name, value) {
|
||||
return window._qz_greasemonkey.getValue(namespace, name, value);
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_setValueImpl === "undefined") {
|
||||
function GM_setValueImpl(namespace, name, value) {
|
||||
window._qz_greasemonkey.setValue(namespace, name, value);
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_deleteValueImpl === "undefined") {
|
||||
function GM_deleteValueImpl(namespace, name) {
|
||||
window._qz_greasemonkey.deleteValue(namespace, name);
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_listValuesImpl === "undefined") {
|
||||
function GM_listValuesImpl(namespace) {
|
||||
return window._qz_greasemonkey.listValues(namespace);
|
||||
//window._qz_greasemonkey.setClipboard(text);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,19 +75,6 @@ var unsafeWindow = window;
|
|||
window.wrappedJSObject = unsafeWindow;
|
||||
|
||||
// GM_registerMenuCommand not supported
|
||||
if(typeof GM_registerMenuCommand === "undefined") {
|
||||
if (typeof GM_registerMenuCommand === "undefined") {
|
||||
function GM_registerMenuCommand(caption, commandFunc, accessKey) { }
|
||||
}
|
||||
|
||||
// GM Resource not supported
|
||||
if(typeof GM_getResourceText === "undefined") {
|
||||
function GM_getResourceText(resourceName) {
|
||||
throw ("QupZilla: GM Resource is not supported!");
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof GM_getResourceURL === "undefined") {
|
||||
function GM_getResourceURL(resourceName) {
|
||||
throw ("QupZilla: GM Resource is not supported!");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
"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;if(b){"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()}else throw"This Browser is not supported, please upgrade.";}); 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){window._qz_greasemonkey.setClipboard(a)};if("undefined"===typeof GM_getValueImpl)var GM_getValueImpl=function(a,b,c){return window._qz_greasemonkey.getValue(a,b,c)}; if("undefined"===typeof GM_setValueImpl)var GM_setValueImpl=function(a,b,c){window._qz_greasemonkey.setValue(a,b,c)};if("undefined"===typeof GM_deleteValueImpl)var GM_deleteValueImpl=function(a,b){window._qz_greasemonkey.deleteValue(a,b)};if("undefined"===typeof GM_listValuesImpl)var GM_listValuesImpl=function(a){return window._qz_greasemonkey.listValues(a)};var unsafeWindow=window;window.wrappedJSObject=unsafeWindow; if("undefined"===typeof GM_registerMenuCommand)var GM_registerMenuCommand=function(a,b,c){};if("undefined"===typeof GM_getResourceText)var GM_getResourceText=function(a){throw"QupZilla: GM Resource is not supported!";};if("undefined"===typeof GM_getResourceURL)var GM_getResourceURL=function(a){throw"QupZilla: GM Resource is not supported!";};
|
||||
"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){};
|
30
src/plugins/GreaseMonkey/data/values.js
Normal file
30
src/plugins/GreaseMonkey/data/values.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
// Modified from https://gist.githubusercontent.com/arantius/3123124/raw/grant-none-shim.js
|
||||
//
|
||||
// %1 - unique script id
|
||||
|
||||
function GM_deleteValue(aKey) {
|
||||
localStorage.removeItem("%1" + aKey);
|
||||
}
|
||||
|
||||
function GM_getValue(aKey, aDefault) {
|
||||
var val = localStorage.getItem("%1" + aKey)
|
||||
if (null === val && 'undefined' != typeof aDefault) return aDefault;
|
||||
return val;
|
||||
}
|
||||
|
||||
function GM_listValues() {
|
||||
var prefixLen = "%1".length;
|
||||
var values = [];
|
||||
var i = 0;
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var k = localStorage.key(i);
|
||||
if (k.substr(0, prefixLen) === "%1") {
|
||||
values.push(k.substr(prefixLen));
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
function GM_setValue(aKey, aVal) {
|
||||
localStorage.setItem("%1" + aKey, aVal);
|
||||
}
|
1
src/plugins/GreaseMonkey/data/values.min.js
vendored
Normal file
1
src/plugins/GreaseMonkey/data/values.min.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
function GM_deleteValue(b){localStorage.removeItem("%1"+b)}function GM_getValue(b,a){var c=localStorage.getItem("%1"+b);return null===c&&"undefined"!=typeof a?a:c}function GM_listValues(){for(var b=[],a=0,a=0;a<localStorage.length;a++){var c=localStorage.key(a);"%1"===c.substr(0,2)&&b.push(c.substr(2))}return b}function GM_setValue(b,a){localStorage.setItem("%1"+b,a)};
|
|
@ -89,6 +89,16 @@ QString GM_Manager::requireScripts(const QStringList &urlList) const
|
|||
return script;
|
||||
}
|
||||
|
||||
QString GM_Manager::bootstrapScript() const
|
||||
{
|
||||
return m_bootstrapScript;
|
||||
}
|
||||
|
||||
QString GM_Manager::valuesScript() const
|
||||
{
|
||||
return m_valuesScript;
|
||||
}
|
||||
|
||||
void GM_Manager::unloadPlugin()
|
||||
{
|
||||
// Save settings
|
||||
|
@ -190,6 +200,9 @@ void GM_Manager::load()
|
|||
gmDir.mkdir("requires");
|
||||
}
|
||||
|
||||
m_bootstrapScript = QzTools::readAllFileContents(":gm/data/bootstrap.min.js");
|
||||
m_valuesScript = QzTools::readAllFileContents(":gm/data/values.min.js");
|
||||
|
||||
QSettings settings(m_settingsPath + QL1S("/extensions.ini"), QSettings::IniFormat);
|
||||
settings.beginGroup("GreaseMonkey");
|
||||
m_disabledScripts = settings.value("disabledScripts", QStringList()).toStringList();
|
||||
|
@ -213,7 +226,6 @@ void GM_Manager::load()
|
|||
}
|
||||
}
|
||||
|
||||
m_bootstrap = QzTools::readAllFileContents(":gm/data/bootstrap.min.js");
|
||||
//m_jsObject->setSettingsFile(m_settingsPath + QL1S("/extensions.ini"));
|
||||
}
|
||||
|
||||
|
|
|
@ -44,6 +44,8 @@ public:
|
|||
QString settinsPath() const;
|
||||
QString scriptsDirectory() const;
|
||||
QString requireScripts(const QStringList &urlList) const;
|
||||
QString bootstrapScript() const;
|
||||
QString valuesScript() const;
|
||||
|
||||
void unloadPlugin();
|
||||
|
||||
|
@ -72,7 +74,8 @@ private slots:
|
|||
|
||||
private:
|
||||
QString m_settingsPath;
|
||||
QString m_bootstrap;
|
||||
QString m_bootstrapScript;
|
||||
QString m_valuesScript;
|
||||
QPointer<GM_Settings> m_settings;
|
||||
|
||||
QStringList m_disabledScripts;
|
||||
|
|
|
@ -187,6 +187,9 @@ void GM_Script::parseScript()
|
|||
m_exclude.clear();
|
||||
m_downloadUrl.clear();
|
||||
m_startAt = DocumentEnd;
|
||||
m_noframes = false;
|
||||
m_script.clear();
|
||||
m_metadata.clear();
|
||||
m_enabled = true;
|
||||
m_valid = false;
|
||||
|
||||
|
@ -275,11 +278,9 @@ void GM_Script::parseScript()
|
|||
int index = fileData.indexOf(QLatin1String("// ==/UserScript==")) + 18;
|
||||
m_metadata = fileData.mid(0, index);
|
||||
|
||||
QString script = fileData.mid(index).trimmed();
|
||||
const QString script = fileData.mid(index).trimmed();
|
||||
m_valid = !script.isEmpty();
|
||||
|
||||
m_script = QSL("(function(){%1\n%2\n})();").arg(m_manager->requireScripts(requireList), script);
|
||||
|
||||
#if QTWEBENGINE_DISABLED
|
||||
QString jscript("(function(){"
|
||||
"function GM_getValue(name,val){return GM_getValueImpl('%1',name,val);}"
|
||||
|
@ -287,13 +288,16 @@ void GM_Script::parseScript()
|
|||
"function GM_deleteValue(name){return GM_deleteValueImpl('%1',name);}"
|
||||
"function GM_listValues(){return GM_listValuesImpl('%1');}"
|
||||
"\n%2\n})();");
|
||||
QString nspace = QCryptographicHash::hash(fullName().toUtf8(), QCryptographicHash::Md4).toHex();
|
||||
#endif
|
||||
const QString nspace = QCryptographicHash::hash(fullName().toUtf8(), QCryptographicHash::Md4).toHex();
|
||||
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").arg(m_metadata, m_script));
|
||||
m_webScript.setSourceCode(QSL("%1\n%2\n%3").arg(m_metadata, m_manager->bootstrapScript(), m_script));
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<RCC>
|
||||
<qresource prefix="/gm">
|
||||
<file>data/bootstrap.min.js</file>
|
||||
<file>data/values.min.js</file>
|
||||
<file>data/icon.png</file>
|
||||
<file>data/icon16.png</file>
|
||||
<file>data/icon18.png</file>
|
||||
|
|
Loading…
Reference in New Issue
Block a user