src/eric7/WebBrowser/Tools/Scripts.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
18 18
19 19
20 def setupWebChannel(worldId): 20 def setupWebChannel(worldId):
21 """ 21 """
22 Function generating a script to setup the web channel. 22 Function generating a script to setup the web channel.
23 23
24 @param worldId world ID for which to setup the channel 24 @param worldId world ID for which to setup the channel
25 @type int 25 @type int
26 @return script to setup the web channel 26 @return script to setup the web channel
27 @rtype str 27 @rtype str
28 """ 28 """
72 }} 72 }}
73 }} 73 }}
74 registerWebChannel(); 74 registerWebChannel();
75 75
76 }})()""" 76 }})()"""
77 77
78 from WebBrowser.WebBrowserPage import WebBrowserPage 78 from WebBrowser.WebBrowserPage import WebBrowserPage
79
79 match = ( 80 match = (
80 "// @exclude eric:*" 81 "// @exclude eric:*"
81 if worldId == WebBrowserPage.SafeJsWorld else 82 if worldId == WebBrowserPage.SafeJsWorld
82 "// @include eric:*" 83 else "// @include eric:*"
83 ) 84 )
84 return source.format(match, getJavascript("qwebchannel.js")) 85 return source.format(match, getJavascript("qwebchannel.js"))
85 86
86 87
87 def setupWindowObject(): 88 def setupWindowObject():
88 """ 89 """
89 Function generating a script to setup window.object add-ons. 90 Function generating a script to setup window.object add-ons.
90 91
91 @return generated script 92 @return generated script
92 @rtype str 93 @rtype str
93 """ 94 """
94 source = """ 95 source = """
95 (function() { 96 (function() {
104 window.external = external; 105 window.external = external;
105 window.print = function() { 106 window.print = function() {
106 window.location = 'eric:PrintPage'; 107 window.location = 'eric:PrintPage';
107 }; 108 };
108 })()""" 109 })()"""
109 110
110 return source 111 return source
111 112
112 113
113 def setStyleSheet(css): 114 def setStyleSheet(css):
114 """ 115 """
115 Function generating a script to set a user style sheet. 116 Function generating a script to set a user style sheet.
116 117
117 @param css style sheet to be applied 118 @param css style sheet to be applied
118 @type str 119 @type str
119 @return script to set a user style sheet 120 @return script to set a user style sheet
120 @rtype str 121 @rtype str
121 """ 122 """
124 var css = document.createElement('style'); 125 var css = document.createElement('style');
125 css.setAttribute('type', 'text/css'); 126 css.setAttribute('type', 'text/css');
126 css.appendChild(document.createTextNode('{0}')); 127 css.appendChild(document.createTextNode('{0}'));
127 document.getElementsByTagName('head')[0].appendChild(css); 128 document.getElementsByTagName('head')[0].appendChild(css);
128 }})()""" 129 }})()"""
129 130
130 style = css.replace("'", "\\'").replace("\n", "\\n") 131 style = css.replace("'", "\\'").replace("\n", "\\n")
131 return source.format(style) 132 return source.format(style)
132 133
133 134
134 def getFormData(pos): 135 def getFormData(pos):
135 """ 136 """
136 Function generating a script to extract data for a form element. 137 Function generating a script to extract data for a form element.
137 138
138 @param pos position to extract data at 139 @param pos position to extract data at
139 @type QPoint 140 @type QPoint
140 @return script to extract form data 141 @return script to extract form data
141 @rtype str 142 @rtype str
142 """ 143 """
169 170
170 171
171 def getAllImages(): 172 def getAllImages():
172 """ 173 """
173 Function generating a script to extract all image tags of a web page. 174 Function generating a script to extract all image tags of a web page.
174 175
175 @return script to extract image tags 176 @return script to extract image tags
176 @rtype str 177 @rtype str
177 """ 178 """
178 source = """ 179 source = """
179 (function() { 180 (function() {
192 193
193 194
194 def getAllMetaAttributes(): 195 def getAllMetaAttributes():
195 """ 196 """
196 Function generating a script to extract all meta attributes of a web page. 197 Function generating a script to extract all meta attributes of a web page.
197 198
198 @return script to extract meta attributes 199 @return script to extract meta attributes
199 @rtype str 200 @rtype str
200 """ 201 """
201 source = """ 202 source = """
202 (function() { 203 (function() {
217 218
218 219
219 def getOpenSearchLinks(): 220 def getOpenSearchLinks():
220 """ 221 """
221 Function generating a script to extract all open search links. 222 Function generating a script to extract all open search links.
222 223
223 @return script to extract all open serach links 224 @return script to extract all open serach links
224 @rtype str 225 @rtype str
225 """ 226 """
226 source = """ 227 source = """
227 (function() { 228 (function() {
242 243
243 244
244 def sendPostData(url, data): 245 def sendPostData(url, data):
245 """ 246 """
246 Function generating a script to send Post data. 247 Function generating a script to send Post data.
247 248
248 @param url URL to send the data to 249 @param url URL to send the data to
249 @type QUrl 250 @type QUrl
250 @param data data to be sent 251 @param data data to be sent
251 @type QByteArray 252 @type QByteArray
252 @return script to send Post data 253 @return script to send Post data
259 form.setAttribute('action', '{0}'); 260 form.setAttribute('action', '{0}');
260 var val; 261 var val;
261 {1} 262 {1}
262 form.submit(); 263 form.submit();
263 }})()""" 264 }})()"""
264 265
265 valueSource = """ 266 valueSource = """
266 val = document.createElement('input'); 267 val = document.createElement('input');
267 val.setAttribute('type', 'hidden'); 268 val.setAttribute('type', 'hidden');
268 val.setAttribute('name', '{0}'); 269 val.setAttribute('name', '{0}');
269 val.setAttribute('value', '{1}'); 270 val.setAttribute('value', '{1}');
270 form.appendChild(val);""" 271 form.appendChild(val);"""
271 272
272 values = "" 273 values = ""
273 query = QUrlQuery(data) 274 query = QUrlQuery(data)
274 for name, value in query.queryItems( 275 for name, value in query.queryItems(QUrl.ComponentFormattingOption.FullyDecoded):
275 QUrl.ComponentFormattingOption.FullyDecoded
276 ):
277 value = value.replace("'", "\\'") 276 value = value.replace("'", "\\'")
278 name = name.replace("'", "\\'") 277 name = name.replace("'", "\\'")
279 values += valueSource.format(name, value) 278 values += valueSource.format(name, value)
280 279
281 return source.format(url.toString(), values) 280 return source.format(url.toString(), values)
282 281
283 282
284 def setupFormObserver(): 283 def setupFormObserver():
285 """ 284 """
286 Function generating a script to monitor a web form for user entries. 285 Function generating a script to monitor a web form for user entries.
287 286
288 @return script to monitor a web page 287 @return script to monitor a web page
289 @rtype str 288 @rtype str
290 """ 289 """
291 source = """ 290 source = """
292 (function() { 291 (function() {
355 354
356 355
357 def completeFormData(data): 356 def completeFormData(data):
358 """ 357 """
359 Function generating a script to fill in form data. 358 Function generating a script to fill in form data.
360 359
361 @param data data to be filled into the form 360 @param data data to be filled into the form
362 @type QByteArray 361 @type QByteArray
363 @return script to fill a form 362 @return script to fill a form
364 @rtype str 363 @rtype str
365 """ 364 """
386 }} 385 }}
387 }} 386 }}
388 }} 387 }}
389 388
390 }})()""" 389 }})()"""
391 390
392 data = bytes(data).decode("utf-8") 391 data = bytes(data).decode("utf-8")
393 data = data.replace("'", "\\'") 392 data = data.replace("'", "\\'")
394 return source.format(data) 393 return source.format(data)
395 394
396 395
397 def setCss(css): 396 def setCss(css):
398 """ 397 """
399 Function generating a script to set a given CSS style sheet. 398 Function generating a script to set a given CSS style sheet.
400 399
401 @param css style sheet 400 @param css style sheet
402 @type str 401 @type str
403 @return script to set the style sheet 402 @return script to set the style sheet
404 @rtype str 403 @rtype str
405 """ 404 """
415 414
416 415
417 def scrollToAnchor(anchor): 416 def scrollToAnchor(anchor):
418 """ 417 """
419 Function generating script to scroll to a given anchor. 418 Function generating script to scroll to a given anchor.
420 419
421 @param anchor name of the anchor to scroll to 420 @param anchor name of the anchor to scroll to
422 @type str 421 @type str
423 @return script to set the style sheet 422 @return script to set the style sheet
424 @rtype str 423 @rtype str
425 """ 424 """
434 if (e) 433 if (e)
435 e.scrollIntoView() 434 e.scrollIntoView()
436 }})()""" 435 }})()"""
437 return source.format(anchor) 436 return source.format(anchor)
438 437
438
439 ########################################################################### 439 ###########################################################################
440 ## scripts below are specific for eric 440 ## scripts below are specific for eric
441 ########################################################################### 441 ###########################################################################
442 442
443 443
444 def getFeedLinks(): 444 def getFeedLinks():
445 """ 445 """
446 Function generating a script to extract all RSS and Atom feed links. 446 Function generating a script to extract all RSS and Atom feed links.
447 447
448 @return script to extract all RSS and Atom feed links 448 @return script to extract all RSS and Atom feed links
449 @rtype str 449 @rtype str
450 """ 450 """
451 source = """ 451 source = """
452 (function() { 452 (function() {

eric ide

mercurial