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