WebBrowser/GreaseMonkey/GreaseMonkeyJavaScript.py

changeset 6128
afc2cda1a743
parent 6048
82ad8ec9548c
child 6326
5ef9456a0cbe
equal deleted inserted replaced
6127:128d9567a533 6128:afc2cda1a743
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 bootstrap_js = """ 12 bootstrap_js = """
13 if(typeof GM_xmlhttpRequest === "undefined") { 13 var GM = {
14 GM_xmlhttpRequest = function(/* object */ details) { 14 info: {
15 details.method = details.method.toUpperCase() || "GET"; 15 script: {
16 16 description: "",
17 if(!details.url) { 17 excludes: [],
18 throw("GM_xmlhttpRequest requires an URL."); 18 includes: [],
19 } 19 matches: [],
20 20 name: "",
21 // build XMLHttpRequest object 21 namespace: "",
22 var oXhr = new XMLHttpRequest; 22 resources: {},
23 // run it 23 'run-at': "document-end",
24 if(oXhr) { 24 version: ""
25 if("onreadystatechange" in details) 25 },
26 oXhr.onreadystatechange = function() { 26 scriptMetaStr: "",
27 details.onreadystatechange(oXhr) 27 scriptHandler: "eric Browser GreaseMonkey",
28 }; 28 version: "4.0"
29 if("onload" in details) 29 }
30 oXhr.onload = function() { details.onload(oXhr) }; 30 };
31 if("onerror" in details) 31 window.GM = GM;
32 oXhr.onerror = function() { details.onerror(oXhr) }; 32
33 33 function GM_info() {
34 oXhr.open(details.method, details.url, true); 34 return GM.info;
35 35 }
36 if("headers" in details) 36
37 for(var header in details.headers) 37 function GM_xmlhttpRequest(/* object */ details) {
38 oXhr.setRequestHeader(header, details.headers[header]); 38 details.method = details.method.toUpperCase() || "GET";
39 39
40 if("data" in details) 40 if (!details.url) {
41 oXhr.send(details.data); 41 throw("GM_xmlhttpRequest requires an URL.");
42 else 42 }
43 oXhr.send(); 43
44 } else 44 // build XMLHttpRequest object
45 throw ("This Browser is not supported, please upgrade.") 45 var oXhr = new XMLHttpRequest;
46 } 46 // run it
47 } 47 if("onreadystatechange" in details)
48 48 oXhr.onreadystatechange = function() {
49 if(typeof GM_addStyle === "undefined") { 49 details.onreadystatechange(oXhr) };
50 function GM_addStyle(/* String */ styles) { 50 if("onload" in details)
51 var head = document.getElementsByTagName("head")[0]; 51 oXhr.onload = function() { details.onload(oXhr) };
52 if (head === undefined) { 52 if("onerror" in details)
53 document.onreadystatechange = function() { 53 oXhr.onerror = function() { details.onerror(oXhr) };
54 if (document.readyState == "interactive") { 54
55 var oStyle = document.createElement("style"); 55 oXhr.open(details.method, details.url, true);
56 oStyle.setAttribute("type", "text\/css"); 56
57 oStyle.appendChild(document.createTextNode(styles)); 57 if("headers" in details)
58 document.getElementsByTagName("head")[0].appendChild(oStyle); 58 for(var header in details.headers)
59 } 59 oXhr.setRequestHeader(header, details.headers[header]);
60
61 if("data" in details)
62 oXhr.send(details.data);
63 else
64 oXhr.send();
65 }
66
67 function GM_addStyle(/* string */ styles) {
68 var head = document.getElementsByTagName("head")[0];
69 if (head === undefined) {
70 document.onreadystatechange = function() {
71 if (document.readyState == "interactive") {
72 var oStyle = document.createElement("style");
73 oStyle.setAttribute("type", "text/css");
74 oStyle.appendChild(document.createTextNode(styles));
75 document.getElementsByTagName("head")[0].appendChild(oStyle);
60 } 76 }
61 } 77 }
62 else { 78 }
63 var oStyle = document.createElement("style"); 79 else {
64 oStyle.setAttribute("type", "text\/css"); 80 var oStyle = document.createElement("style");
65 oStyle.appendChild(document.createTextNode(styles)); 81 oStyle.setAttribute("type", "text/css");
66 head.appendChild(oStyle); 82 oStyle.appendChild(document.createTextNode(styles));
67 } 83 head.appendChild(oStyle);
68 } 84 }
69 } 85 }
70 86
71 if(typeof GM_log === "undefined") { 87 function GM_log(log) {
72 function GM_log(log) { 88 if(console)
73 if(console) 89 console.log(log);
74 console.log(log); 90 }
75 } 91
76 } 92 function GM_openInTab(url) {
77 93 return window.open(url);
78 if(typeof GM_openInTab === "undefined") { 94 }
79 function GM_openInTab(url) { 95
80 window.open(url) 96 function GM_setClipboard(text) {
81 } 97 external.extra.greasemonkey.setClipboard(text);
82 } 98 }
83
84 // Define unsafe window
85 var unsafeWindow = window;
86 window.wrappedJSObject = unsafeWindow;
87 99
88 // GM_registerMenuCommand not supported 100 // GM_registerMenuCommand not supported
89 if(typeof GM_registerMenuCommand === "undefined") { 101 function GM_registerMenuCommand(caption, commandFunc, accessKey) { }
90 function GM_registerMenuCommand(caption, commandFunc, accessKey) { } 102
91 } 103 // GM_getResourceUrl not supported
92 104 function GM_getResourceUrl(resourceName) { }
93 // GM Resource not supported 105
94 if(typeof GM_getResourceText === "undefined") { 106 // GreaseMonkey 4.0 support
95 function GM_getResourceText(resourceName) { 107 GM.openInTab = GM_openInTab;
96 throw ("eric6 Web Browser: GM Resource is not supported!"); 108 GM.setClipboard = GM_setClipboard;
97 } 109 GM.xmlhttpRequest = GM_xmlhttpRequest;
98 } 110
99 111 // GM_getResourceUrl not supported
100 if(typeof GM_getResourceURL === "undefined") { 112 GM.getResourceUrl = function(resourceName) {
101 function GM_getResourceURL(resourceName) { 113 return new Promise((resolve, reject) => {
102 throw ("eric6 Web Browser: GM Resource is not supported!"); 114 reject();
103 } 115 });
104 } 116 };
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 """ 117 """
127 118
128 119
129 # {0} - unique script id 120 # {0} - unique script id
130 values_js = """ 121 values_js = """
150 }} 141 }}
151 142
152 function GM_setValue(aKey, aVal) {{ 143 function GM_setValue(aKey, aVal) {{
153 localStorage.setItem("{0}" + aKey, aVal); 144 localStorage.setItem("{0}" + aKey, aVal);
154 }} 145 }}
155 """ 146
147 // GreaseMonkey 4.0 support
148 var asyncCall = (func) => {{
149 if (window._eric_external) {{
150 func();
151 }} else {{
152 document.addEventListener("_eric_external_created", func);
153 }}
154 }};
155
156 var decode = (val) => {{
157 val = String(val);
158 if (!val.length) {{
159 return val;
160 }}
161 var v = val.substr(1);
162 if (val[0] == "b") {{
163 return Boolean(v == "true" ? true : false);
164 }} else if (val[0] == "i") {{
165 return Number(v);
166 }} else if (val[0] == "s") {{
167 return v;
168 }} else {{
169 return undefined;
170 }}
171 }};
172
173 var encode = (val) => {{
174 if (typeof val == "boolean") {{
175 return "b" + (val ? "true" : "false");
176 }} else if (typeof val == "number") {{
177 return "i" + String(val);
178 }} else if (typeof val == "string") {{
179 return "s" + val;
180 }} else {{
181 return "";
182 }}
183 }};
184
185 GM.deleteValue = function(name) {{
186 return new Promise((resolve, reject) => {{
187 asyncCall(() => {{
188 external.extra.greasemonkey.deleteValue("{0}", name, (res) => {{
189 if (res) {{
190 resolve();
191 }} else {{
192 reject();
193 }}
194 }});
195 }});
196 }});
197 }};
198
199 GM.getValue = function(name, value) {{
200 return new Promise((resolve) => {{
201 asyncCall(() => {{
202 external.extra.greasemonkey.getValue("{0}", name, encode(value),
203 (res) => {{
204 resolve(decode(res));
205 }});
206 }});
207 }});
208 }};
209
210 GM.setValue = function(name, value) {{
211 return new Promise((resolve, reject) => {{
212 asyncCall(() => {{
213 external.extra.greasemonkey.setValue("{0}", name, encode(value),
214 (res) => {{
215 if (res) {{
216 resolve();
217 }} else {{
218 reject();
219 }}
220 }});
221 }});
222 }});
223 }};
224
225 GM.listValues = function() {{
226 return new Promise((resolve) => {{
227 asyncCall(() => {{
228 external.extra.greasemonkey.listValues("{0}", resolve);
229 }});
230 }});
231 }};
232 """

eric ide

mercurial