Skip to content

Instantly share code, notes, and snippets.

@ryanmark
Last active June 16, 2016 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmark/e8d4b080e6271083eccc319326d47f61 to your computer and use it in GitHub Desktop.
Save ryanmark/e8d4b080e6271083eccc319326d47f61 to your computer and use it in GitHub Desktop.
Phantom js doesn't seem to handle em-based media queries properly when zoomed
var WebPage = require('webpage'),
System = require('system');
var address = System.args[1];
var zoom = System.args[2];
var width = 700;
var page = WebPage.create();
page.viewportSize = {
width: width*zoom,
height: width*zoom
};
page.zoomFactor = zoom;
page.open(address);
page.onConsoleMessage = function(msg) { console.log(msg); };
page.onCallback = function(data) {
if ( data === 'gtg' ) {
page.close();
phantom.exit(0);
}
};
page.onLoadFinished = function(status) {
page.evaluate(function() {
function _phantomLoadCB() {
if (document.readyState === 'complete') {
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
console.log('window is '+[w,h].join('x')+' pixels');
var div = document.createElement('div');
div.style.width = "1em";
document.body.appendChild(div);
var pixels = div.offsetWidth;
document.body.removeChild(div);
console.log('1em becomes '+pixels+' pixels');
var pixelTarget = w - 100;
var emTarget = pixelTarget / pixels;
if ( window.matchMedia('(min-width: '+emTarget+'em)').matches ) {
console.log('window is at least '+emTarget+'em ('+(emTarget*pixels)+'px)');
} else {
console.log('window is NOT at least '+emTarget+'em ('+(emTarget*pixels)+'px)');
}
if ( window.matchMedia('(min-width: '+pixelTarget+'px)').matches ) {
console.log('window is at least '+pixelTarget+'px');
} else {
console.log('window is NOT at least '+pixelTarget+'px');
}
setTimeout(function() { window.callPhantom('gtg'); }, 1000);
}
}
document.onreadystatechange = _phantomLoadCB;
_phantomLoadCB();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment