InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PhantomJS tutorials

  • Install phantomjs on Mac
  • Install phantomjs on Ubuntu
  • create site snapshot thumbnail
  • find all fonts-families on a web page
  • print resources request by a page
  • skip Google analytics when fetching a web page
 
  • Home
  • > Tutorials
  • > Javascript
  • > PhantomJS

PhantomJS – find all fonts-families on a web page

By admin | Last updated on Feb 9, 2016

PhantomJS (Headless browser javascript library based on webkit) can be used to find the font-families used on a website page using jQuery. This can be used for QA purpose to do some sanity checks. It can also be used to find fonts on few sites for analysis purpose.
phantomjs-find-font-families

Here are the steps to display all unique font-families used on a web page:

  1. We’ll be using jQuery for iterating over various DOM elements on the page. First download the jQuery library from any place. Easier is to download it from google CDN site using wget. Here is the command to do it on Linux/Mac:
    wget http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js
  2. Then use the jQuery descendant selector to select all descendants of body element. Here is the javascript code snippet for this:
    jQuery("body *").each(function() {
      // ...
    }
  3. Use jQuery .css() api to get the font-family property. Note that if font-family is not defined for that element, it is taken from the style of parent element and so on. Here is code snippet for this:
    var fontFamily = jQuery(this).css("font-family");
  4. Here is the complete code (font.js) for printing all unique font-families present on a page (infoheap.com in this example):
    var page = require('webpage').create();
    page.open('https://infoheap.com/', function (status) {
      if (status !== 'success') {
        console.log('Unable to access network');
      } else {
        page.injectJs('jquery.min.js');
        var fontArray = page.evaluate(function () {
          var jQuery = window.jQuery || window.$;
          var fontArray = {};
          jQuery("body *").each(function() {
            var fontFamily = jQuery(this).css("font-family");
            if (fontFamily) {
              fontArray[fontFamily] = 1;
            }
          });
          return fontArray;
        });
        for (font in fontArray) {
          console.log(font);
        }
      }
      phantom.exit();
    });
  5. Assuming you have pahntomjs installed, run the following on command line:
    phantomjs font.js
  6. Here is the outcome for site infoheap.com:
    Arial, Verdana, sans-serif
    Arial, sans-serif
    Georgia, 'Times new Roman', serif

Few points to note

  1. We are just printing the unique font-families used. You may want to print them by occurrence count, etc.
  2. We are injecting jQuery on the page. In case the page already has a jQuery, it should still work.
  3. for jQuery injection (or any other javascript) it has to be present in local working directory.

 

Suggested posts:

  1. Logo creation high level checklist
  2. CSS font-size
  3. CSS pointer-events – disable click on an element
  4. WordPress how to check if a post is being viewed by admin
  5. How to crop an image using imagemagick convert
  6. How to use qpdf to add/remove password from pdf on mac
  7. How to display wordpress top level pages
  8. CSS font shorthand property
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, HTML, Javascript, jQuery, PhantomJS, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress