Search code examples
javascriptpythonpyqtpyqt4qwebview

QWebView - dealing with javascript infinite loop


web_view_crash.py

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
view = QWebView()
view.settings().setAttribute(QWebSettings.JavascriptEnabled, True)
view.load(QUrl('infinite_loop.html'))
view.show()
app.exec_()

infinite_loop.html

<script>
    while(true) {
        document.write('infinite loop...')}
</script>

I want to fix this from my python code, without touching the javascript. Can I kill the javascript somehow?

Edit: Both files are local.


Solution

  • sip.delete(page) will kill the page object, stopping the execution, then all you have to do after that is delete the reference to the page in Python: del page

    You might also want to check out subclassing WebPage and re-implementing shouldInterruptJavaScript(), perhaps killing your page when it executes.