We are using jxbrowser version 6.21 (latest at the time of writing).
If a script gets stuck in an endless loop the jxbrowser just freezes and remains unresponsive. I waited more than 2 minutes.
Is there any way to handle this smoothly?
Modern browsers detect this and show an overlay to the user to stop the script.
SSCCE: html-file with this content:
<html>
<body>
<script>
function badLoop() {
var nextTime = new Date().getTime();
while (true) {
var now = new Date().getTime();
if (now > nextTime) {
console.log('badLoop() running: ' + now);
nextTime = now + 500;
}
}
}
</script>
<input type="button" value="startLoop" onClick="badLoop()" />
</body>
</html>
Test runs with other browsers:
When pressing "stop website" Firefox just stops the script, but keeps the website open and logs this in the console:
Google Chrome (other than firefox) actually leaves the webseite after pressing leave (and shows this error):
Is this possible with jxbrowser? Either fully automatically or with user-confirm?
Here is a sample that demonstrates how to intercept the corresponding onRenderUnresponsive
event in JxBrowser and detect that the web page became unresponsive:
public class BrowserSample {
public static void main(String[] args) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.addRenderListener(new RenderAdapter() {
@Override
public void onRenderUnresponsive(RenderEvent event) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(view, "The web page is unresponsive.");
}
});
}
});
browser.loadHTML("<html>\n"
+ " <body>\n"
+ " <script>\n"
+ " function badLoop() {\n"
+ " var nextTime = new Date().getTime();\n"
+ " while (true) {\n"
+ " var now = new Date().getTime();\n"
+ " if (now > nextTime) {\n"
+ " console.log('badLoop() running: ' + now);\n"
+ " nextTime = now + 500;\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " </script>\n"
+ " <input type=\"button\" value=\"startLoop\" onClick=\"badLoop()\" />\n"
+ " </body>\n"
+ "</html>");
}
}