Extending Fake Data

Introduction

Writing Custom Generators is the best way to extend Fake Data and set it up to fill the forms in any way you want. In the following tutorials you will see how easy it is to create a Custom Generator and how powerful Fake Data can be.

MV2 vs MV3

In the upcoming examples and explanations, there will be many occurrences of the terms "MV2" and "MV3". If you never coded a browser extension before, you might have not heard about them. In short, MV stands for Manifest Version, which is the API version that the browser offers for extensions to interact with it.

MV2 (or Manifest Version 2) is the version that was available for many years, and pretty much all extensions (as of June 2022) are built with. This version became deprecated and will be replaced by the new MV3 (of Manifest Version 3). MV3 will be enforced in Google Chrome starting with June 2024, and all extensions will have to be migrated to it. If you want to read more about this migration and how it affects Fake Data, please read this section.

What you should know when writing code for Fake Data is what Manifest version is installed on your browser. Below is a table with each browser and version of Fake Data:

Fake Data versionBrowserManifest version
AllChromeMV2, (MV3 in betaopen in new window)
AllFirefoxMV2
AllEdgeMV2

For now all stable versions of Fake Data are using MV2. This will change later this year, with Chrome being the first one to transition to MV3.

How is the code executed

There are a few ways that Fake Data executes the code that you write, and it would be useful to know about them.

In MV3 version of Fake Data:

The code is executed in a sandboxed iframe. Your code will not have direct access to DOM elements, but an API for that is provided to make your life easier.

In MV3 version, you don't have access to any of the chrome.* API methods, in case you were using them for something (there are very high chances that you weren't using them).

In MV2 version of Fake Data:

By default, the code is executed in the background script of the extension. This means that you don't have direct access to DOM elements, but you can return a function that will be executed in the content script (this will be described later).

Functions that are executed in the content script of the extension, will have direct access to DOM elements, so you can manipulate them directly.

In MV2 version, you will also have direct access to chrome.* API.


Enough talking, let's see exactly how to write code for Fake Data!