Programmer Notes

Building a Chrome Extension to Track Visitors Impression on Web Pages

Google Chrome extensions are being used to enhance UI experience by many folds. You can download various types of extensions from Chrome Web Store. This article will take you through the course of building a Chrome extension that tracks the amount of time visitors spend on web pages.

Basic Structure

Each extension entails the following files:

  1. A manifest file (JSON file)
  2. One or more HTML files (unless the extension is a theme)
  3. Optional: One or more JavaScript files
  4. Optional: Any other files your extension needs—for example, image files, etc
    .

While you’re working on your extension, you place all these files into a single folder.

Manifest File

A Manifest file contains rudimentary information regarding an extension such as:

  • Name
  • Description
  • Version number
  • Permissions
  • Configuration options
    .

Additionally, it determines the purpose and functionality of an extension. The basic structure of a manifest.json file is mentioned below:

HTML Files

Further dissecting the manifest.json, you can incorporate numerous HTML files within several subdivisions such as:

  • browser_action
  • page_action
  • options_page
  • devtools_page
  • background
  • options_ui
    .

In the current scenario, you need to integrate only one HTML file in browser_action. This file is used to show popup content and include JavaScript and CSS files as desired.

Make sure that you cannot write inline script in your HTML file. You need to write it in a separate file and then include it inside your HTML. For including files in HTML, you need to specify a path according to the referred file guidelines as mentioned below.

Referring Files

There are two ways to refer files in an extension:

  1. Relative URL: Files can be referred by a relative URL. This means that if you want to include a specific file present in your extension’s directory then you can include it without specifying complete path of your extension root directory.For instance if “sample_image.png” is available in the sub-folder “images” of the extension’s code then following tag can be used:
  2. Absolute URL: You can also specify an absolute URL, which is of the form:
    Here, extensionID is a unique identifier for every extension. You can see extensionID for every single extension by entering ‘chrome://extensions/’ in Chrome’s location bar.

    pathToFile is a path of file from root directory of the extension. For instance if “sample_image.png” is available in extension sub-folder “images” then pathToFile will be “images/sample_image.png”.

Other Files

JavaScript files allow us to run various scripts in an extension. These files can be incorporated in HTML and manifest.json file as well.

Furthermore, image files are used to set extension icon as well as for HTML views, while the extension icon images are encompassed in manifest.json file. You can also specify multiple sizes of images that can be used for extension display in Chrome. Images used in views can be incorporated through HTML or JavaScript.

Upload Extension

Once a chrome extension is developed, it can be uploaded using a mechanism defined here.

Code

extension_popup.html:

popup.js: