Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running user inputed Javascript, is there a way to detect and stop "problem" scripts?

On a page, something like jsFiddle, that executes user inputed Javascript, is there a way to stop / disrupt "problem" scripts running in an iframe?

The major class of problem scripts would be infinite loops, I think. Browsers deal with multiple alerts quite well, but a script like, ​for (var i = 0; ++i; i < 100) { /* do stuff */ }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ will go forever.

How can I either detect and not run, or run and stop, say after 10 seconds of running, a script?

Removing the iframe is fine, but I only want to remove it if the script is still running after 10 seconds, but I don't want to remove it if the script has stopped running.


Here is how I imagine the page will work. If you have a better solution, let me know...

The input page contains a textarea and a blank iframe. The user enters their script into the textarea, and when ready they click on run. Then (backstage) a separate page is created that contains the user script in executable form on an HTML page. The src of the iframe is set to the page with the executable code. This all happens dynamically without a page refresh.

like image 997
Peter Ajtai Avatar asked Aug 28 '10 02:08

Peter Ajtai


2 Answers

I haven't used this jsandbox script, but it appears to have what you want.

like image 100
Mottie Avatar answered Oct 13 '22 23:10

Mottie


If one script freezes on a page, other scripts will not continue to run. Therefore, there is no way to detect if another script has stopped running, without using a custom plugin or something. Browsers do not use multithreading in that way.

like image 44
Chris Laplante Avatar answered Oct 13 '22 22:10

Chris Laplante