WebBrowser/GreaseMonkey/GreaseMonkeyJavaScript.py

branch
QtWebEngine
changeset 4763
8ad353f31184
parent 4631
5c1a96925da4
child 4886
b56735d36159
equal deleted inserted replaced
4762:ea40955a0937 4763:8ad353f31184
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module containing some JavaScript resources.
8 """
9
10 from __future__ import unicode_literals
11
12 bootstrap_js = """
13 if(typeof GM_xmlhttpRequest === "undefined") {
14 GM_xmlhttpRequest = function(/* object */ details) {
15 details.method = details.method.toUpperCase() || "GET";
16
17 if(!details.url) {
18 throw("GM_xmlhttpRequest requires an URL.");
19 }
20
21 // build XMLHttpRequest object
22 var oXhr = new XMLHttpRequest;
23 // run it
24 if(oXhr) {
25 if("onreadystatechange" in details)
26 oXhr.onreadystatechange = function() {
27 details.onreadystatechange(oXhr)
28 };
29 if("onload" in details)
30 oXhr.onload = function() { details.onload(oXhr) };
31 if("onerror" in details)
32 oXhr.onerror = function() { details.onerror(oXhr) };
33
34 oXhr.open(details.method, details.url, true);
35
36 if("headers" in details)
37 for(var header in details.headers)
38 oXhr.setRequestHeader(header, details.headers[header]);
39
40 if("data" in details)
41 oXhr.send(details.data);
42 else
43 oXhr.send();
44 } else
45 throw ("This Browser is not supported, please upgrade.")
46 }
47 }
48
49 if(typeof GM_addStyle === "undefined") {
50 function GM_addStyle(/* String */ styles) {
51 var head = document.getElementsByTagName("head")[0];
52 if (head === undefined) {
53 document.onreadystatechange = function() {
54 if (document.readyState == "interactive") {
55 var oStyle = document.createElement("style");
56 oStyle.setAttribute("type", "text\/css");
57 oStyle.appendChild(document.createTextNode(styles));
58 document.getElementsByTagName("head")[0].appendChild(oStyle);
59 }
60 }
61 }
62 else {
63 var oStyle = document.createElement("style");
64 oStyle.setAttribute("type", "text\/css");
65 oStyle.appendChild(document.createTextNode(styles));
66 head.appendChild(oStyle);
67 }
68 }
69 }
70
71 if(typeof GM_log === "undefined") {
72 function GM_log(log) {
73 if(console)
74 console.log(log);
75 }
76 }
77
78 if(typeof GM_openInTab === "undefined") {
79 function GM_openInTab(url) {
80 window.open(url)
81 }
82 }
83
84 // Define unsafe window
85 var unsafeWindow = window;
86 window.wrappedJSObject = unsafeWindow;
87
88 // GM_registerMenuCommand not supported
89 if(typeof GM_registerMenuCommand === "undefined") {
90 function GM_registerMenuCommand(caption, commandFunc, accessKey) { }
91 }
92
93 // GM Resource not supported
94 if(typeof GM_getResourceText === "undefined") {
95 function GM_getResourceText(resourceName) {
96 throw ("eric6 Web Browser: GM Resource is not supported!");
97 }
98 }
99
100 if(typeof GM_getResourceURL === "undefined") {
101 function GM_getResourceURL(resourceName) {
102 throw ("eric6 Web Browser: GM Resource is not supported!");
103 }
104 }
105
106 // GM Settings not supported
107 if(typeof GM_getValue === "undefined") {
108 function GM_getValue(name, defaultValue) {
109 return defaultValue;
110 }
111 }
112
113 if(typeof GM_setValue === "undefined") {
114 function GM_setValue(name, value) { }
115 }
116
117 if(typeof GM_deleteValue === "undefined") {
118 function GM_deleteValue(name) { }
119 }
120
121 if(typeof GM_listValues === "undefined") {
122 function GM_listValues() {
123 return new Array("");
124 }
125 }
126 """
127
128
129 # {0} - unique script id
130 values_js = """
131 function GM_deleteValue(aKey) {{
132 localStorage.removeItem("{0}" + aKey);
133 }}
134
135 function GM_getValue(aKey, aDefault)
136 var val = localStorage.getItem("{0}" + aKey)
137 if (null === val && 'undefined' != typeof aDefault) return aDefault;
138 return val;
139 }}
140
141 function GM_listValues() {{
142 var values = [];
143 for (var i = 0; i < localStorage.length; i++) {{
144 var k = localStorage.key(i);
145 if (k.indexOf("{0}") === 0) {{
146 values.push(k.replace("{0}", ""));
147 }}
148 }}
149 return values;
150 }}
151
152 function GM_setValue(aKey, aVal) {{
153 localStorage.setItem("{0}" + aKey, aVal);
154 }}
155 """

eric ide

mercurial