开发者

QWebView - dealing with javascript infinite loop

开发者 https://www.devze.com 2023-01-07 19:34 出处:网络
web_view_crash.py import sys from PyQt4.QtGui import * from PyQt4.QtCore import * from PyQt4.QtWebKit import *

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.


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.


??? This really makes no sense at all. The Javascript file is an infinite loop. You cannot "kill" the page code from the server. It's just impossible - especially when the browser is stuck running CPU-bound Javascript.

Maybe that Javascript file was just supplied as an example, but it doesn't really matter. If you know that you've got a page that's broken in that way, you have to fix the page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号