diff --git a/python/pyphantomjs/bootstrap.js b/python/pyphantomjs/bootstrap.js index 7e9d50f9..45eeb87c 100644 --- a/python/pyphantomjs/bootstrap.js +++ b/python/pyphantomjs/bootstrap.js @@ -75,6 +75,39 @@ window.WebPage = function() { return page; } + +// override settimeout/setinterval with a try..catch +window._setTimeout = window.setTimeout; +window._setInterval = window.setInterval; +window.setTimeout = function(func, delay) { + var f = function() { + try { + typeof func === 'function' ? func() : eval(func); + } catch (err) { + if (err !== 'phantom.exit') { + throw err; + } + } + } + + return window._setTimeout(f, delay); +} + +window.setInterval = function(func, delay, lang) { + var f = function() { + try { + typeof func === 'function' ? func() : eval(func); + } catch (err) { + if (err !== 'phantom.exit') { + throw err; + } + } + } + + return window._setInterval(f, delay, lang); +} + + phantom.exit = function(code) { if (code == null) { code = 0; diff --git a/src/bootstrap.js b/src/bootstrap.js index 7e9d50f9..45eeb87c 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -75,6 +75,39 @@ window.WebPage = function() { return page; } + +// override settimeout/setinterval with a try..catch +window._setTimeout = window.setTimeout; +window._setInterval = window.setInterval; +window.setTimeout = function(func, delay) { + var f = function() { + try { + typeof func === 'function' ? func() : eval(func); + } catch (err) { + if (err !== 'phantom.exit') { + throw err; + } + } + } + + return window._setTimeout(f, delay); +} + +window.setInterval = function(func, delay, lang) { + var f = function() { + try { + typeof func === 'function' ? func() : eval(func); + } catch (err) { + if (err !== 'phantom.exit') { + throw err; + } + } + } + + return window._setInterval(f, delay, lang); +} + + phantom.exit = function(code) { if (code == null) { code = 0;