@@ -62,7 +62,7 @@ def qt_main_loop():
62
62
QApplication.quit()
63
63
return
64
64
do_something_meaningful()
65
-
65
+
66
66
app = init_qtgui()
67
67
main_timer = QTimer()
68
68
QObject.connect(main_timer, QtCore.SIGNAL("timeout()"), qt_main_loop)
@@ -108,9 +108,13 @@ def __init__(self,**kwargs):
108
108
# Set this to true if you want to capture flash.
109
109
# Not that your desktop must be large enough for
110
110
# fitting the whole window.
111
- self .grabWholeWindow = kwargs .get ('grabWholeWindow' , False )
111
+ self .grabWholeWindow = kwargs .get ('grabWholeWindow' , False )
112
112
self .renderTransparentBackground = kwargs .get ('renderTransparentBackground' , False )
113
-
113
+ self .ignoreAlert = kwargs .get ('ignoreAlert' , True )
114
+ self .ignoreConfirm = kwargs .get ('ignoreConfirm' , True )
115
+ self .ignorePrompt = kwargs .get ('ignorePrompt' , True )
116
+ self .interruptJavaScript = kwargs .get ('interruptJavaScript' , True )
117
+
114
118
# Set some default options for QWebPage
115
119
self .qWebSettings = {
116
120
QWebSettings .JavascriptEnabled : False ,
@@ -155,7 +159,7 @@ def render_to_bytes(self, url):
155
159
return qBuffer .buffer ().data ()
156
160
157
161
class _WebkitRendererHelper (QObject ):
158
- """This helper class is doing the real work. It is required to
162
+ """This helper class is doing the real work. It is required to
159
163
allow WebkitRenderer.render() to be called "asynchronously"
160
164
(but always from Qt's GUI thread).
161
165
"""
@@ -172,7 +176,9 @@ def __init__(self, parent):
172
176
setattr (self ,key ,value )
173
177
174
178
# Create and connect required PyQt4 objects
175
- self ._page = QWebPage ()
179
+ self ._page = CustomWebPage (logger = self .logger , ignore_alert = self .ignoreAlert ,
180
+ ignore_confirm = self .ignoreConfirm , ignore_prompt = self .ignorePrompt ,
181
+ interrupt_js = self .interruptJavaScript )
176
182
self ._view = QWebView ()
177
183
self ._view .setPage (self ._page )
178
184
self ._window = QMainWindow ()
@@ -244,12 +250,12 @@ def render(self, url):
244
250
image = QPixmap .grabWindow (self ._window .winId ())
245
251
else :
246
252
image = QPixmap .grabWidget (self ._window )
247
-
253
+
248
254
return self ._post_process_image (image )
249
255
250
256
def _load_page (self , url , width , height , timeout ):
251
257
"""
252
- This method implements the logic for retrieving and displaying
258
+ This method implements the logic for retrieving and displaying
253
259
the requested page.
254
260
"""
255
261
@@ -326,3 +332,48 @@ def _on_ssl_errors(self, reply, errors):
326
332
for e in errors :
327
333
if self .logger : self .logger .warn ("SSL: " + e .errorString ())
328
334
reply .ignoreSslErrors ()
335
+
336
+
337
+ class CustomWebPage (QWebPage ):
338
+ def __init__ (self , ** kwargs ):
339
+ super (CustomWebPage , self ).__init__ ()
340
+ self .logger = kwargs .get ('logger' , None )
341
+ self .ignore_alert = kwargs .get ('ignore_alert' , True )
342
+ self .ignore_confirm = kwargs .get ('ignore_confirm' , True )
343
+ self .ignore_prompt = kwargs .get ('ignore_prompt' , True )
344
+ self .interrupt_js = kwargs .get ('interrupt_js' , True )
345
+
346
+ def javaScriptAlert (self , frame , message ):
347
+ if self .logger : self .logger .debug ('Alert: %s' , message )
348
+ if not self .ignore_alert :
349
+ return super (CustomWebPage , self ).javaScriptAlert (frame , message )
350
+
351
+ def javaScriptConfirm (self , frame , message ):
352
+ if self .logger : self .logger .debug ('Confirm: %s' , message )
353
+ if not self .ignore_confirm :
354
+ return super (CustomWebPage , self ).javaScriptConfirm (frame , message )
355
+ else :
356
+ False
357
+
358
+ def javaScriptPrompt (self , frame , message , default , result ):
359
+ """This function is called whenever a JavaScript program running inside frame tries to prompt
360
+ the user for input. The program may provide an optional message, msg, as well as a default value
361
+ for the input in defaultValue.
362
+
363
+ If the prompt was cancelled by the user the implementation should return false;
364
+ otherwise the result should be written to result and true should be returned.
365
+ If the prompt was not cancelled by the user, the implementation should return true and
366
+ the result string must not be null.
367
+ """
368
+ if self .logger : self .logger .debug ('Prompt: %s (%s)' % (message , default ))
369
+ if not self .ignore_prompt :
370
+ return super (CustomWebPage , self ).javaScriptPrompt (frame , message , default , result )
371
+ else :
372
+ return False
373
+
374
+ def shouldInterruptJavaScript (self ):
375
+ """This function is called when a JavaScript program is running for a long period of time.
376
+ If the user wanted to stop the JavaScript the implementation should return true; otherwise false.
377
+ """
378
+ if self .logger : self .log .debug ("WebKit ask to interrupt JavaScript" )
379
+ return self .interrupt_js
0 commit comments