Ravgeet is a Full Stack Developer/Technical Content Writer based in India who codes and writes about React, Vue, Flutter, Node, Strapi, and Python.
Published on
Chrome extensions are a great way to customize your browsing experience. Most of the time, Chrome extensions need to be reactive and this is where building the extension in vanilla JavaScript can be a painful experience. To overcome this shortcoming, you can use a JavaScript-based front-end framework like Next.js or Nuxt.js to build your Chrome extensions.
In this tutorial, you’ll learn to build a Chrome extension using Next.js. For this tutorial, you’ll build a Chrome extension that allows you to tag and save web links to a Notion database.
Next.js is a React.js framework for building server-side rendered React.js applications. It has a bunch of features with a good developer experience and supports TypeScript out of the box. This is tutorial will be helpful even if you want to use React.js as it is instead of using Next.js.
According to Notion, it is a way of organizing docs, wikis, tasks in a single place. Basically, Notion is a sort of database that allows you to view your data in multiple views like a kanban board, table, timeline, calendar, gallery, list to showcase your data but with a nice visual interface.
For this tutorial, you’ll set up a database table in Notion to store your web links. To connect with Notion, you can use their API for public use.
To follow along with this tutorial, you’ll need the following things:
The entire source code for this tutorial is available in this GitHub repository.
The first thing you need to do is to create a Notion database.
First, in your Notion workspace, create a new page and give it the title - Bookmarks.
Next, click inside the text area of the new page and type /table full. Select Table - Full page in the modal that appears.
Next, give the table the same title as you gave to the page (”Bookmarks”).
Every database table in Notion has fields, which are shown as columns. For the Bookmarks table, add the following columns by clicking the plus sign (+) icon in the header row of the table:
To use the Notion API, you’ll need to create a Notion integration in order to obtain a Notion API token.
To do so, first, visit your integrations page and click on the New integration button in the left sidebar.
Next, configure the basic information about the integration and name it “Notion Bookmark”. Select the appropriate workspace and click Submit.
Once the integration is complete, you will be presented with a secret Internal Integration API token. Copy it somewhere safe as you’ll need it later while writing the code.
By default, Notion integrations don't have access to pages or databases in the workspace. You need to share the specific page or database that you want to connect to the Notion integration.
To do so, first, open the Bookmarks database in your Notion workspace and click on the Share link in the upper right corner. A modal will appear. Use the search field to the left of the Invite button to find the “Notion Bookmark” integration, and click Invite.
That’s it. You have set up your Notion database and you can now add data to it with the help of the Notion API.
To add entries to your Bookmarks database, you need its database ID.
If your Notion database is within a workspace, the URL structure of the database page may look like this: *https://www.notion.so/{workspace_name}/{database_id}?v={view_id}*
If your Notion database is not within a workspace, or if it simply doesn’t match the URL shown above, it probably looks like this: *https://www.notion.so/{database_id}?v={view_id}*
Depending upon your URL structure, extract the 36-character-long {database_id} portion from the URL. Keep it somewhere safe as you will need it to connect with this particular database through Notion API.
In case you get stuck somewhere, make sure to check out Notion’s documentation page for working with databases for more information.
Now that you have completely set up your Notion, it's time to build the Next.js frontend app.
First, open up your terminal, navigate to a path of your choice, and execute the following command to create a Next.js project:
On the terminal, when you are asked about the project‘s name, set it to notion-bookmark. Next, it will install all the NPM dependencies.
Finally, after the installation is complete, navigate into the notion-bookmark directory and start the Next.js development server by running the following commands in your terminal:
This will start the development server on port 3000 and take you to localhost:3000. The first view of the Next.js website will look like this:
There are a couple of things you need to configure for building Chrome extension in Next.js,
First, you need to install the following packages:
Shut down the Next.js development server by pressing Control-C in your terminal and then run the following commands to install the NPM dependencies:
Once the installation is complete, next, open the package.json and add the following scripts to the scripts property:
In the above configuration:
1. dev-extension is used to run the npm-watch.
2. Since for some reason, Chrome extensions can’t have an underscore in their paths, the lint:build script renames the paths in your build files.
3. The build script is the final script you need to run to build the Chrome extension for production. This script is run by the npm-watch. This prevents you from running the build script, again and again, and makes sure that the build script runs automatically when the files change.
Next, add the following configuration for npm-watch to the package.json:
The above configuration tells npm-watch to run the build script whenever the files matching with the patterns and extensions array change.
In the public directory, create an inject.js and a manifest.json file. Leave the inject.js file empty and add the following configuration to manifest.json file:
To know more about the above properties and keys, make sure to check out Google’s official documentation for Chrome extensions.
Finally, run the following command to build and export your Chrome extension for the first time:
Next, you need to register your extension within Google Chrome.
First, visit Chrome Extensions by visiting chrome://extensions in the Chrome browser.
Next, turn on the Developer mode.
Next, click on Load unpacked and select the path that references to the out directory in your project directory (notion-bookmark).
If your extension loads successfully, it’ll be added under the Chrome extensions and in the chrome extensions bar.
Finally, click the extension icon in the extension bar and you’ll get the Next.js default home page in your extension.
Now that your extension is set up, next, you need to write code to collect and save web links to the Bookmarks database.
First, at the project’s root, create an .env file and the following environment variables to it:
Replace the <your-notion-database-id> and <your-notion-api-token> with your respective values.
Next, open the next.config.js file and add the following two environment variables to the env property:
In the above config, you have specified the NEXT_PUBLIC_NOTION_API_TOKEN and NEXT_PUBLIC_NOTION_DATABASE_ID. It is important to prefix NEXT_PUBLIC to your environment variables so that they can be used on the client-side.
The UI for your Chrome extension basically needs a form to collect the data from the user and a button to save the form data to the Notion database.
First, in the pages directory, replace the code in index.js file with the following code:
In the above code:
Note: We have skipped the styling part of the Chrome extension because we believe that it is important for us to help you understand the functionality as you can style the extension per your own preference.
Next, add the following code just before the return method of the Home component:
In the above code:
Next, add the following code for the saveBookmarkToNotion method:
In the above code:
Next, add the following code for the handleSubmit method:
In the above code:
Next, save your progress and start the development server by running the following command:
The above command will build your extension and make it ready for being a Chrome extension. The coding part is complete and finally, you need to test the Chrome extension.
To verify that everything is working as expected, visit your favorite web page and try saving it to your Bookmarks database with the help of your Chrome extension:
And it works flawlessly!
That’s it. Today, you learned to build a Chrome extension using Next.js. You also learned to interact with Notion API and save your data to a Notion database. You can add/remove fields according to your needs and customize the extension according to your needs.
Since Notion API is still in beta, the above code may/may not work in the future. To make sure that you are able to track the production issues, you can try Bird.
The entire source code for this tutorial is available in this GitHub repository.
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop