QWebView - 处理 javascript 无限循环
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>
我想从我的 python 代码中修复这个问题,而不接触 javascript。我可以以某种方式杀死 JavaScript 吗?
编辑:这两个文件都是本地的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sip.delete(page)
将杀死页面对象,停止执行,然后您所要做的就是在 Python 中删除对页面的引用:del page
您可能还想检查子类化 WebPage 并重新实现
shouldInterruptJavaScript()
,也许会在页面执行时杀死您的页面。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.???这实在是毫无意义。 Javascript 文件是一个无限循环。您无法从服务器“杀死”页面代码。这是不可能的 - 尤其当浏览器卡在运行受 CPU 限制的 Javascript 时。
也许该 Javascript 文件只是作为示例提供的,但这并不重要。如果您知道某个页面以这种方式损坏,则必须修复该页面。
??? 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.