WebBrowser/GreaseMonkey/GreaseMonkeyJavaScript.py

branch
QtWebEngine
changeset 4763
8ad353f31184
parent 4631
5c1a96925da4
child 4886
b56735d36159
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebBrowser/GreaseMonkey/GreaseMonkeyJavaScript.py	Sun Feb 21 19:54:14 2016 +0100
@@ -0,0 +1,155 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2012 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module containing some JavaScript resources.
+"""
+
+from __future__ import unicode_literals
+
+bootstrap_js = """
+if(typeof GM_xmlhttpRequest === "undefined") {
+    GM_xmlhttpRequest = function(/* object */ details) {
+        details.method = details.method.toUpperCase() || "GET";
+
+        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) };
+
+            oXhr.open(details.method, details.url, true);
+
+            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(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.appendChild(document.createTextNode(styles));
+                  document.getElementsByTagName("head")[0].appendChild(oStyle);
+                }
+            }
+        }
+        else {
+            var oStyle = document.createElement("style");
+            oStyle.setAttribute("type", "text\/css");
+            oStyle.appendChild(document.createTextNode(styles));
+            head.appendChild(oStyle);
+        }
+    }
+}
+
+if(typeof GM_log === "undefined") {
+    function GM_log(log) {
+        if(console)
+            console.log(log);
+    }
+}
+
+if(typeof GM_openInTab === "undefined") {
+    function GM_openInTab(url) {
+        window.open(url)
+    }
+}
+
+// Define unsafe window
+var unsafeWindow = window;
+window.wrappedJSObject = unsafeWindow;
+
+// GM_registerMenuCommand not supported
+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 ("eric6 Web Browser: GM Resource is not supported!");
+    }
+}
+
+if(typeof GM_getResourceURL === "undefined") {
+    function GM_getResourceURL(resourceName) {
+        throw ("eric6 Web Browser: GM Resource is not supported!");
+    }
+}
+
+// GM Settings not supported
+if(typeof GM_getValue === "undefined") {
+    function GM_getValue(name, defaultValue) {
+        return defaultValue;
+    }
+}
+
+if(typeof GM_setValue === "undefined") {
+    function GM_setValue(name, value) { }
+}
+
+if(typeof GM_deleteValue === "undefined") {
+    function GM_deleteValue(name) { }
+}
+
+if(typeof GM_listValues === "undefined") {
+    function GM_listValues() {
+        return new Array("");
+    }
+}
+"""
+
+
+# {0} - unique script id
+values_js = """
+function GM_deleteValue(aKey) {{
+    localStorage.removeItem("{0}" + aKey);
+}}
+
+function GM_getValue(aKey, aDefault) 
+    var val = localStorage.getItem("{0}" + aKey)
+    if (null === val && 'undefined' != typeof aDefault) return aDefault;
+    return val;
+}}
+
+function GM_listValues() {{
+    var values = [];
+    for (var i = 0; i < localStorage.length; i++) {{
+        var k = localStorage.key(i);
+        if (k.indexOf("{0}") === 0) {{
+            values.push(k.replace("{0}", ""));
+        }}
+    }}
+    return values;
+}}
+
+function GM_setValue(aKey, aVal) {{
+    localStorage.setItem("{0}" + aKey, aVal);
+}}
+"""

eric ide

mercurial