Build a Chrome Extension to Track Web Page CO2 Emissions

Pages have weight, some more than others, and the median size is growing.

Measuring emissions associated with page weight is a relatively new practice and given the number of factors to be controlled for, any values should be regarded as estimates.

But estimates are sufficient to compare pages across sites and for development teams to use them to compare versions of their pages over time.

Keeping an eye on emissions also means being aware of the individual requests that make up a page. This number is in the hundreds for sites that pull in data from many sources and employ third party tracking and advertising.

The emissions tracker lets visitors view requests and emissions in real time, as the page loads.

Architecture

The extension relies on the service worker to monitor and process requests and the side panel to display them.

The service worker has access to indexedDB (the browser’s built-in database) but not the DOM.

This side panel has access to the Window object and Chrome's APIs.

When a visitor first opens the extension by clicking on the icon, they are asked to refresh the page to begin tracking emissions. Thereafter one of three scenarios takes place.

Service worker

The extension service worker is responsible for intercepting requests, storing them, processing them (sorting, etc.) and messaging the side panel.

Side panel

Request size (bytes transferred)

Unless the server responsible for serving a cross-origin request has set Access-Control-Expose-Headers the content length is 0. In order to find bytes transferred, we need to look at the content length and then, if compression has been used, apply it.

How emissions are calculated

The Green Web Foundation (TGWF)

Once we have a byte count, we can calculate its associated emissions. TGWF provides a useful set of methods and a clear methodology. We recommend using Ecograder for a comparative analysis.

Features

Purpose

The cost of our actions is of general interest and the emissions tracker provides a simple way to see the impact of a web page.

The emissions tracker is also an inducement to development teams to spend more time looking at how their web sites and apps behave in the browser where they are used and seen by others.

At People and Code we spend a lot of time in the browser and build tools to help us there.