Reputation: 547
I've written a phantomjs script using node to take screenshots of a webpage. The page I'm trying to screenshot includes an important external script which adds variables to the global window object. ie. window.__components.
The problem is, this is never set. It works fine in a browser but it just dies in phantomJS. I've altered the script to inject the script before opening the page, I've added a check to make sure that the components object has been added before opening the page and still it fails.
Any ideas?
function injectExternalScripts(page, ph, url) {
page.includeJs('http://external-script', ()=>{
page.evaluate(() => {
return window;
}, function(result) {
if(result.__components) {
openPage(page, ph, url);
}
});
});
}
function openPage(page, ph, url) {
page.open(url, (status)=>{
// errors returned from page
// window.__components is null
});
}
Upvotes: 0
Views: 182
Reputation: 61892
You can only return primitive objects from the page context in PhantomJS. window
and DOM nodes are not primitive objects and if __components
is also not a primitive object, then you also cannot return that.
You can check if __components
is set with this:
page.evaluate(() => {
return !!window.__components;
}, function(result) {
if(result) {
openPage(page, ph, url);
}
});
Upvotes: 1