Skip to content

Instantly share code, notes, and snippets.

@firedfox
Last active March 10, 2017 09:40
Show Gist options
  • Save firedfox/fbb3e17e419b57308018 to your computer and use it in GitHub Desktop.
Save firedfox/fbb3e17e419b57308018 to your computer and use it in GitHub Desktop.
// example:
// phantomjs getComputedStyle.js "http://www.iqiyi.com/" ".usrTxGeneral-box_hover" "position,display,visibility,overflow"
var TIMEOUT = 60 * 1000;
var webPage = require('webpage');
var system = require('system');
var page = webPage.create();
page.settings.loadImages = false;
var args = system.args;
if (args.length < 4) {
console.log('usage: phantomjs getComputedStyle.js url elementSelector styleNames');
phantom.exit();
}
var url = args[1];
var elementSelector = args[2];
var styleNames = args[3].split(',');
var getStyles = function(status) {
var styleValues = page.evaluate(function(elementSelector, styleNames) {
var element = document.querySelector(elementSelector);
var computedStyle = window.getComputedStyle(element);
var styleValues = [];
styleNames.forEach(function(styleName) {
styleValues.push(computedStyle.getPropertyValue(styleName));
});
return styleValues;
}, elementSelector, styleNames);
console.log(styleValues.join('\t'));
phantom.exit();
};
page.open(url, getStyles);
setTimeout(getStyles, TIMEOUT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment