Font Adjuster Extension Technical Guide
Lyle Schwemley
2022
Contents
Purpose
This technical guide documents the Font Adjuster extension, its functionality, code, implementation, and uses. It is provided for the development team and end users of the extension alike to fully understand what the program does and to facilitate troubleshooting of and further updates to the extension.
Overview
The need for a Firefox browser extension that would allow users to adjust font size was identified. A common complaint submitted to the development team was that certain websites utilized fonts that were too small to be easily read by users with accessibility needs. The Font Adjuster extension was developed as a solution for the identified problem.
Once installed into the web browser, the Font Adjuster extension allows Firefox users to increase or decrease the size of the font on any webpage. It does not store any information about the user. Adjustments to font sizes are made when the user interacts with a popup containing three buttons: one that increases font size, one that decreases font size, and one that returns the font to its default size.
Code
This section will identify the code of the Font Adjuster extension for maintenance and troubleshooting purposes.
The Font Adjuster extension for Firefox requires the following prerequisite knowledge:
- JavaScript
- HTML
- WebExtensions API
In addition to the above, an HTML editor will be necessary. For this documentation, the open source software Notepad++ was utilized.
Organization
The extension is contained in one archive. See the Archiving Files Reference Guide for more information on file archives. The code references other files in the archive, so proper file structure is critical.
Inside the root of the archive is manifest.json and the assets folder. The assets folder contains all of the files the manifest references. Within the assets folder is the images folder, which contains the icons for the extension, and the js folder, which contains the necessary JavaScript files. The file path is shown below.
Figure 1 - File path
The extension is primarily organized through the manifest. The overall structure of the extension is pictured below.
Figure 2 - Structure of the Font Adjuster extension
Manifest.json
All extensions must have a manifest.json that contains the metadata needed by the WebExtensions API to know what the extension is, such as the name, version number, description, and the icon used to represent the extension. Name, version, and manifest_version are required keys. Description and icon keys are optional, but it is best practice to include them. In addition to the necessary metadata, manifest.json also includes references to popup.html and fontsize.js. Below is the complete manifest.json for the Font Adjuster extension.
See the table below for a description of each key used in the manifest.
Key | Description |
---|---|
Name | The title of the extension. |
Version | The current version of Font Adjuster. |
Description | A short explanation of the purpose of the extension. It will be displayed in the add-ons database. |
Manifest_version | The version of the manifest. MV2 is used in this manifest. |
Icons | The number on the left indicates the size in pixels of the icon, and on the right is the file location of the image within the extension archive. |
Browser_action | Enables the use of popup.html when the extension button is clicked in Firefox. default_popup calls the popup.html, and browser_style is set to true to allow Firefox to inject the default style of the browser into the popup. |
Content_scripts | References the code that is injected into web pages in order to modify font size. |
Table 1 - Description of keys
Popup.html
The popup.html file is what makes the extension visible to the user. It is an HTML file that contains the code for the visuals, description, and buttons in the popup that enable the user to adjust the font size on any webpage. The popup is shown when the extension button is clicked in Firefox because the "browser_action" field in manifest.json contains the "default_popup" key with a value "popup.html".
The <style></style>
section of the code is written in CSS and dictates the appearance of the popup. The <body></body>
section of creates the buttons, paragraphs, and heading.
Below is the complete popup.html.
Figure 4 - popup.html
Line 49 contains a script reference to popup.js. This enables the use of JavaScript in the popup.
Popup.js
Popup.js is the JavaScript file that gives popup.html file full functionality. The first three lines of the code set the variables that will be used in the script. Lines 4 through 19 query the browser and obtain the URL the user is currently viewing. This is necessary in order for the code to parse the HTML text on the website before the font size can be adjusted. If the code cannot access the URL, it will log an error.
Figure 5 - Set variables and get current URL
Since popup.html cannot have any JavaScript in it, the onclick toggle switches have been added to popup.js and passed into the HTML file to make the buttons clickable.
Figure 6 - Toggle switches
Fontsize.js
In order to alter the webpage itself, the fontsize.js content script is needed. Using the Document Object Model (DOM), the content script is able to access and change the font size of almost any webpage. The content script, which is referenced in the manifest, is active at document start.
First, the variables are declared. If loaded from the addons database instead of a local folder, then getManifest
will have the update_url
property. The font_times
variable is set to 1 with a multiplier of 10%. On fresh extension installs and the first time the content script is injected, it originally would not run because dom_ready
was always false. This was remedied by including the if
statements on lines 14 and 15.
Figure 7 - Declare variables
After the variables have been declared, the script is ready to call functions. The script uses three functions: isOnScreen
, save_defaults
, and change_fontsize
.
The isOnScreen
function keeps the page view closer to the same location when changing font size. Without it, the page would jump up or down when the user clicked A+ or A- buttons.
Figure 8 - the isOnScreen function
The next function to be called is the save_defaults function. It is important that this function be called before change_fontsize because the child element fontSizes is relative to the parent element. This means that if the parent element is changed, then the child elements continue to change, and the data-default-fontsize element will store an incorrect value, making the Default Size button return the font to an incorrect size.
Figure 9 - the save_defaults function
The bulk of the content script is the change_fontsize
function. This function passes in the button click information in order to increase or decrease the size of the font by a mathematical factor of plus or minus 0.1 * 10, or 10% per click. Once the button has been pressed, the variable "up" or "down" is passed into the function, and the value is calculated and added to the default font size.
The change_fontsize
function also enables the use of the Default Size button by representing the original size of the font as 1 and reverting the font_times
variable to 1 when the button is pressed.
Figure 10 - the change_fontsize function
The final elements of fontsize.js are the listeners. The listeners associate the variables up, down, or default, with the A+, A-, and Default Size buttons, respectively.
Figure 11 - Event listeners
Implementation
Installation
The Font Adjuster browser extension is installed using the same methods as any other Firefox add-on. Click the three lines to the right hand side of the address bar. Then click on Add-ons and Themes in the menu. Alternatively, the keyboard shortcut CTRL + SHIFT + A can be used to open the Add-ons and Themes menu.
In the space above the Manage Your Extensions header, there is a search bar where users can enter the name of the specific add-on desired. Type “Font Adjuster” in the search bar and press enter. The database will appear with add-ons for download.
Alternatively, the direct link to the store page of the extension can be followed: View Store Page
Once Font Adjuster has been located, click on it. The home page for it will appear. Click the blue Add to Firefox button, and then click Add on the popup.
Firefox will notify the user that the add-on was successfully installed. Click Okay to close the notification.
Uninstallation
To uninstall the extension, right-click on its icon in the address bar and select Remove Extension in the dropdown menu. Firefox will ask the user to confirm the removal of the extension via a popup. Click the blue Remove button on the popup to complete the uninstallation.
Using Font Adjuster
The extension is accessed via a popup that can opened at any time by clicking on the Font Adjuster on the upper right side of the toolbar.
Figure 12 - The Font Adjuster icon
The popup contains a heading, three buttons, and instructions on how to use the extension. To increase the font size on a webpage, click the A+ button. To decrease the font size, click the A- button. The font increases or decreases by a factor of 10% per click. Clicking the Default Size button returns the font to its original size.
Figure 13 - The Font Adjuster popup
Limitations
Font Adjuster is a lightweight extension; it does not store any information about the user or previously visited webpages. This means that every time a page is refreshed, the font will return to its default size. Additionally, if new text appears on the screen, for example in a chatroom, it will appear at its default size.
Appendix: Definitions
Add-on: A program that can be installed into the browser and allows users to tailor their browsing experience. Different types of add-ons include themes, dictionaries, and extensions.
API – Application Programming Interface: A software interface that allows two or more programs or computers to communicate with each other.
Content script: The part of the extension that directly accesses the content of a webpage.
*CSS – Cascading Style Sheets: A web design programming language.
DOM – Document Object Model: An object oriented representation of an HTML document.
Extension: Primarily coded with JavaScript, HTML, and CSS, extensions add new features to a browser, such as blocking advertisements, embedded translation tools, and the ability to change the appearance of webpages.
Firefox: An open source web browser developed by Mozilla.
HTML – HyperText Markup Language: A web design programming language.
JavaScript: A web design programming language.
WebExtensions API: A cross-browser extension API for developing extensions implemented by Mozilla.