Building Browser Extensions

Kindle highlights from Building Browser Extensions by Matt Frisbie: the chrome.action, notifications and contextMenus APIs and what an extension can control.
Building Browser Extensions cover

On this page

Highlights

11. Extension and Browser APIs

My note: .h1

The chrome.action API allows you to control the extension toolbar icon, including appearance, title text, badge content, badge color, popup page, and click handlers
The chrome.notification API allows an extension to display rich notifications to the user using the host operating system’s notification mechanism
The chrome.contextMenus API allows an extension to add interactive options to right click and context menus throughout the browser
This API allows you to customize the browser’s behavior on a per-site basis instead of globally: enabling or disabling cookies, JavaScript, and plugins. Each of the following properties has a get(), set(), and clear() method:
The API allows an extension to manage for some font settings to depend on certain generic font families and language scripts.
The chrome.cookies API is a create/read/update/destroy interface for the browser’s cookies.
The chrome.bookmarks API is a create/read/update/destroy interface for the browser’s bookmarks. It includes methods to manage the hierarchical nature of bookmarks.
The chrome.history API is a create/read/update/destroy interface for the browser’s browsing history.
The chrome.downloads API is a create/read/update/destroy interface for the browser’s downloads. “Creating” a download means forcing the browser to download from a specified URL.
This API allows an extension to read the “top sites” (most visited sites) displayed on the new tab page.
This API allows an extension to programmatically remove browsing data, which encompasses a considerable number of categories.
This API allows an extension to programmatically inspect and reopen recently closed tabs.
These APIs allow a browser to totally control the tabs and windows that appear inside a browser. It can perform tasks such as create, remove, inspect, modify, rearrange, pin, and mute.
This API allows you to programmatically execute searches in the browser’s default search provider.
The chrome.debugger API allows you to programmatically control the browser’s debugger. With it, you can do almost anything that the Devtools API is capable of, including querying the response body, if the browser’s developer tools interface is not open.
This API is used to schedule code to run in the future. It is similar in concept to setTimeout() and setInterval(), but it is able to wake a service worker.
This API allows an extension to get and set a user’s privacy controls.
The tts API allows you to instruct your browser to speak text aloud using it’s native text-to-speech engine.
The dom API has a single method, openOrClosedShadowRoot(), used to get the open shadow root or the closed shadow root hosted by the specified element.
This API is used to programmatically inject JavaScript or CSS into the page.
This API indicates when the host system has gone idle.
The Devtools API allows you to add custom interfaces to the browser’s developer tools interface. These interfaces are granted special access to APIs only accessible from inside the developer tools.
This API is a collection of utilities. Many of the methods can be used to inspect the internal configuration of the running extension.
Extensions are able to control the fundamentals of own operation, including reloading the extension, checking for updates, self-uninstalling, and setting the uninstall URL.
The system API allows you to inspect certain details about the host operating system.
The cpu, display, memory, and storage properties are all getters that return a JavaScript object filled with whatever the host system provides.
12. Permissions

My note: .h1

Browser extension permissions are conceptually identical to mobile app permissions.
When building browser extensions, choosing permissions carefully is critically important.
This might surprise you, as the entire extension was loaded without issue. It is important to understand that the browser makes no assumptions about what permissions might be needed when loading the extension, or if a requested permission will be needed in the codebase at all.
The web browser geolocation API is not accessible by default, the user must grant access. However, the API is structured that simply calling this method will prompt the user with a dialog to grant permission; if the user grants permission, the script can proceed with execution as if the permission was granted all along. Contrast this with extension example from the beginning of the chapter: calling a method without permissions will simply throw an error with no recourse. When using API methods requiring optional permissions, you must use the permissions API to check if the extension has permissions, request permissions if they are not granted, and only then can you proceed with the permissioned API call.
Beginning in manifest v3, permissions and host_permissions were split out into separate fields, with a mirrored split between optional_permissions and optional_host_permissions. Conceptually, the difference is straightforward: permissions are the APIs you need access to, and host_permissions are the origins you need to use the APIs in.
XMLHttpRequest and fetch access to those origins without cross-origin restrictions (even for requests made from content scripts)
The ability to read tab-specific metadata without the tabs permission, such as the url, title, and favIconUrl properties of Tab objects
The ability to inject scripts programmatically using tabs.executeScript() into pages served from those origins
The ability to access cookies for that host using the cookies API, as long as the cookies API permission is also included
The ability to bypass tracking protection for extension pages where a host is specified as a full domain or with wildcards. Content scripts, however, can only bypass tracking protection for hosts specified with a full domain
Once a permission is granted, the extension has that permission for the entire lifetime of the extension.
However, it is possible that the user can explicitly revoke that permission via the browser’s extension management page. If the extension is uninstalled and then reinstalled, all the optional permissions that were formerly granted are lost.
For permissions such as alarms and storage, the APIs they enable are of little consequence to the user and therefore they can be accessed silently.
For the permissions and host_permissions lists, the warning dialog will be displayed immediately after installing, and before the extension has an opportunity to execute any code. If the permission is denied, the extension will not be installed.
For optional_permissions and optional_host_permissions, the warning will only display when the extension makes the request for those permissions. If the permission is denied, the extension will continue to operate as it did before without the additional permissions.
Unlike HTML5 permissions, the extension will be able to make another request for the optional permission if it is initially denied.
When developing your extension, you will notice that loading an unpacked will not show the permission warning dialog when initially installing the extension. (Optional permissions will still show the warning dialog.) Of course, this is to avoid annoying the developer when they repeatedly reinstall their unfinished extension.

45 highlights, exported from my Kindle via Readwise.

Subscribe to Ishan Girdhar newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!