|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2019 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 var GM = { |
|
14 info: { |
|
15 script: { |
|
16 description: "", |
|
17 excludes: [], |
|
18 includes: [], |
|
19 matches: [], |
|
20 name: "", |
|
21 namespace: "", |
|
22 resources: {}, |
|
23 'run-at': "document-end", |
|
24 version: "" |
|
25 }, |
|
26 scriptMetaStr: "", |
|
27 scriptHandler: "eric Browser GreaseMonkey", |
|
28 version: "4.0" |
|
29 } |
|
30 }; |
|
31 window.GM = GM; |
|
32 |
|
33 function GM_info() { |
|
34 return GM.info; |
|
35 } |
|
36 |
|
37 function GM_xmlhttpRequest(/* object */ details) { |
|
38 details.method = details.method.toUpperCase() || "GET"; |
|
39 |
|
40 if (!details.url) { |
|
41 throw("GM_xmlhttpRequest requires an URL."); |
|
42 } |
|
43 |
|
44 // build XMLHttpRequest object |
|
45 var oXhr = new XMLHttpRequest; |
|
46 // run it |
|
47 if("onreadystatechange" in details) |
|
48 oXhr.onreadystatechange = function() { |
|
49 details.onreadystatechange(oXhr) }; |
|
50 if("onload" in details) |
|
51 oXhr.onload = function() { details.onload(oXhr) }; |
|
52 if("onerror" in details) |
|
53 oXhr.onerror = function() { details.onerror(oXhr) }; |
|
54 |
|
55 oXhr.open(details.method, details.url, true); |
|
56 |
|
57 if("headers" in details) |
|
58 for(var header in details.headers) |
|
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); |
|
76 } |
|
77 } |
|
78 } |
|
79 else { |
|
80 var oStyle = document.createElement("style"); |
|
81 oStyle.setAttribute("type", "text/css"); |
|
82 oStyle.appendChild(document.createTextNode(styles)); |
|
83 head.appendChild(oStyle); |
|
84 } |
|
85 } |
|
86 |
|
87 function GM_log(log) { |
|
88 if(console) |
|
89 console.log(log); |
|
90 } |
|
91 |
|
92 function GM_openInTab(url) { |
|
93 return window.open(url); |
|
94 } |
|
95 |
|
96 function GM_setClipboard(text) { |
|
97 external.extra.greasemonkey.setClipboard(text); |
|
98 } |
|
99 |
|
100 // GM_registerMenuCommand not supported |
|
101 function GM_registerMenuCommand(caption, commandFunc, accessKey) { } |
|
102 |
|
103 // GM_getResourceUrl not supported |
|
104 function GM_getResourceUrl(resourceName) { } |
|
105 |
|
106 // GreaseMonkey 4.0 support |
|
107 GM.openInTab = GM_openInTab; |
|
108 GM.setClipboard = GM_setClipboard; |
|
109 GM.xmlhttpRequest = GM_xmlhttpRequest; |
|
110 |
|
111 // GM_getResourceUrl not supported |
|
112 GM.getResourceUrl = function(resourceName) { |
|
113 return new Promise((resolve, reject) => { |
|
114 reject(); |
|
115 }); |
|
116 }; |
|
117 """ |
|
118 |
|
119 |
|
120 # {0} - unique script id |
|
121 values_js = """ |
|
122 function GM_deleteValue(aKey) {{ |
|
123 localStorage.removeItem("{0}" + aKey); |
|
124 }} |
|
125 |
|
126 function GM_getValue(aKey, aDefault) {{ |
|
127 var val = localStorage.getItem("{0}" + aKey) |
|
128 if (null === val) return aDefault; |
|
129 return val; |
|
130 }} |
|
131 |
|
132 function GM_listValues() {{ |
|
133 var values = []; |
|
134 for (var i = 0; i < localStorage.length; i++) {{ |
|
135 var k = localStorage.key(i); |
|
136 if (k.indexOf("{0}") === 0) {{ |
|
137 values.push(k.replace("{0}", "")); |
|
138 }} |
|
139 }} |
|
140 return values; |
|
141 }} |
|
142 |
|
143 function GM_setValue(aKey, aVal) {{ |
|
144 localStorage.setItem("{0}" + aKey, aVal); |
|
145 }} |
|
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 """ |