Skip to content

Instantly share code, notes, and snippets.

@sorribas
Created June 13, 2014 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorribas/e6821d5527d95cf87f46 to your computer and use it in GitHub Desktop.
Save sorribas/e6821d5527d95cf87f46 to your computer and use it in GitHub Desktop.
New Phantom process.
// Code to be run by PhantomJS.
// The docs for these modules are here: http://phantomjs.org/api/
// Note that the 'fs' module here has a different API than the one in node.js core.
var webpage = require('webpage');
var system = require('system');
var fs = require('fs');
var page = webpage.create();
var inc = 0;
var forcePrintMedia = function() {
page.evaluate(function() {
var findPrintMedia = function() {
var styles = [];
Array.prototype.slice.call(document.querySelectorAll('style')).forEach(function(el) {
styles.push(el.innerText);
});
Array.prototype.slice.call(document.querySelectorAll('link')).forEach(function(el) {
if (el.rel && el.rel.indexOf('stylesheet') === -1) return;
try {
// try-catch is just precaution (we already set web-security to no)
var xhr = new XMLHttpRequest();
// 99.99% of the cases we just hit the cache so no real io
xhr.open('GET', el.href, false);
xhr.send(null);
styles.push(xhr.responseText);
} catch (err) {
// do nothing
}
});
var style = styles.join('\n');
return style.split('@media print').slice(1).filter(function(text) {
return text.indexOf('attr(href)') === -1;
}).map(function(text) {
var lvl = 0;
var from = text.indexOf('{');
for (var i = from; i < text.length; i++) {
if (text[i] === '{') lvl++;
if (text[i] === '}') lvl--;
if (lvl === 0) break;
}
return text.substring(from+1, i-1);
}).join('\n');
};
var div = document.createElement('div');
div.innerHTML = '<style>\n'+findPrintMedia()+'\n</style>';
document.body.appendChild(div);
document.body.style.backgroundImage = 'none';
document.body.style.backgroundColor = 'white';
});
};
var loop = function() {
var line = system.stdin.readLine();
if (!line.trim()) {
//fs.remove(fifoFile);
return phantom.exit(0);
}
try {
line = JSON.parse(line);
} catch (err) {
//fs.remove(fifoFile);
return phantom.exit(1);
}
if (!page) page = webpage.create();
page.viewportSize = {
width: line.width || 1280,
height: line.height || 960
};
page.paperSize = {
format: line.paperFormat || 'A4',
orientation: line.orientation || 'portrait',
margin: line.margin || '0cm'
};
if (line.userAgent) page.settings.userAgent = line.userAgent;
if (line.crop) {
page.clipRect = {
width: line.crop.width || page.viewportSize.width,
height: line.crop.height || page.viewportSize.height,
top: line.crop.top || 0,
left: line.crop.left || 0
}
}
page.open(line.url, function(requestStatus) {
// If there's a failure, communicate that through the FIFO by writing just the "!" character.
if (requestStatus !== 'success') {
line.success = false;
console.log(JSON.stringify(line));
page = null;
loop();
return;
}
var render = function() {
setTimeout(function() {
if (line.printMedia) forcePrintMedia();
page.render(line.file, {format:line.format || 'png'});
page = null;
line.success = true;
console.log(JSON.stringify(line));
loop();
}, 0);
};
var waitAndRender = function() {
var timeout = setTimeout(function() {
page.onAlert('webpage-renderable');
}, 10000);
var rendered = false;
page.onAlert = function(msg) {
if (rendered || msg !== 'webpage-renderable') return;
rendered = true;
clearTimeout(timeout);
render();
};
page.evaluate(function() {
if (window.renderable) return alert('webpage-renderable');
var renderable = false;
Object.defineProperty(window, 'renderable', {
get: function() {
return renderable;
},
set: function(val) {
renderable = val;
alert('webpage-renderable');
}
});
});
};
var renderable = page.evaluate(function() {
return window.renderable;
});
if (renderable === false) return waitAndRender();
render();
});
};
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment