From 3d0c56af40635dd6990c5d19b47e39f7377301f6 Mon Sep 17 00:00:00 2001 From: IceArmy Date: Sun, 19 Jun 2011 21:18:14 -0700 Subject: [PATCH] Fix the last problem with setTimeout and setInterval --- python/pyphantomjs/bootstrap.js | 33 +++++++++++++++++++++++++++++++++ src/bootstrap.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) 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;