WebBrowser/Tools/Scripts.py

branch
QtWebEngine
changeset 4741
f9e1adc69076
parent 4733
ae291a307ea6
child 4743
f9e2e536d130
equal deleted inserted replaced
4735:84e78ee0f361 4741:f9e1adc69076
11 # This code was ported from QupZilla. 11 # This code was ported from QupZilla.
12 # Copyright (C) David Rosca <nowrep@gmail.com> 12 # Copyright (C) David Rosca <nowrep@gmail.com>
13 # 13 #
14 14
15 from __future__ import unicode_literals 15 from __future__ import unicode_literals
16
17 from PyQt5.QtCore import QUrlQuery, QUrl
16 18
17 from .WebBrowserTools import readAllFileContents 19 from .WebBrowserTools import readAllFileContents
18 20
19 21
20 def setupWebChannel(): 22 def setupWebChannel():
174 content: e.getAttribute('content'), 176 content: e.getAttribute('content'),
175 httpequiv: e.getAttribute('http-equiv') 177 httpequiv: e.getAttribute('http-equiv')
176 }); 178 });
177 } 179 }
178 return out; 180 return out;
179 })()""" 181 })()"""
180 return source 182 return source
183
184
185 def getOpenSearchLinks():
186 """
187 Function generating a script to extract all open search links.
188
189 @return script to extract all open serach links
190 @rtype str
191 """
192 source = """
193 (function() {
194 var out = [];
195 var links = document.getElementsByTagName('link');
196 for (var i = 0; i < links.length; ++i) {
197 var e = links[i];
198 if (e.type == 'application/opensearchdescription+xml') {
199 out.push({
200 url: e.getAttribute('href'),
201 title: e.getAttribute('title')
202 });
203 }
204 }
205 return out;
206 })()"""
207 return source
208
209
210 def sendPostData(url, data):
211 """
212 Function generating a script to send Post data.
213
214 @param url URL to send the data to
215 @type QUrl
216 @param data data to be sent
217 @type QByteArray
218 @return script to send Post data
219 @rtype str
220 """
221 source = """
222 (function() {{
223 var form = document.createElement('form');
224 form.setAttribute('method', 'POST');
225 form.setAttribute('action', '{0}');
226 var val;
227 {1}
228 form.submit();
229 }})()"""
230
231 valueSource = """
232 val = document.createElement('input');
233 val.setAttribute('type', 'hidden');
234 val.setAttribute('name', '{0}');
235 val.setAttribute('value', '{1}');
236 form.appendChild(val);"""
237
238 values = ""
239 query = QUrlQuery(data)
240 for name, value in query.queryItems(QUrl.FullyDecoded):
241 value = value.replace("'", "\\'")
242 name = name.replace("'", "\\'")
243 values += valueSource.format(name, value)
244
245 return source.format(url.toString(), values)

eric ide

mercurial