Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5556

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T10:22:08+00:00 2024-11-27T10:22:08+00:00

Browser extensions – Custom options page

  • 60k

In the previous article, we added an option to change the color of our extensions.
So far, we have used our main popup view, but we can also leverage the browser options menu.

This is a neat trick to clean up extension options, and in this article, I'll show you how you can achieve it.

Chrome extension options

If you would like to follow along with the article, take the following GitHub branch as your starting point.

Adding options to a browser extension

Like everything involving the extensions, we must first register the options menu in our manifest file.
Open the manifest.json file and add the following config.

{   "options_ui": {     "page": "options.html",     "open_in_tab": false   } } 
Enter fullscreen mode Exit fullscreen mode

This will register the options screen we call options.html, and we tell it not to open a new tab. (this will open it in a popup).

This options file will become a new app and run separately from our existing one.
Create your project's options.html file and add the following code.

<!DOCTYPE html> <html lang="en">   <head>     <meta charset="UTF-8" />     <meta name="viewport" content="width=device-width, initial-scale=1.0" />     <meta http-equiv="X-UA-Compatible" content="ie=edge" />     <title>DDT Pop-up</title>     <link rel="stylesheet" type="text/css" href="./css/style.css" />   </head>   <body>     <div id="options"></div>     <script type="module" src="src/options/index.jsx"></script>   </body> </html> 
Enter fullscreen mode Exit fullscreen mode

Let's go ahead and create this options folder in our src directory, and inside create the index.jsx file.

import ReactDOM from 'react-dom'; import { App } from './App';  const app = document.getElementById('options'); ReactDOM.render(<App />, app); 
Enter fullscreen mode Exit fullscreen mode

As you can see, this is just another React bootstrap file. Let's go ahead and create the options App.jsx file.

import { useState } from 'react';  export function App() {   const [color, setColor] = useState('indigo');    chrome.storage.sync.get('color', (storedColor) => {     setColor(storedColor.color);   });    const pickColor = (pickedValue) => {     setColor(pickedValue);     chrome.storage.sync.set({ color: pickedValue });     window.close();   };    return (     <div className='m-4'>       <select         onChange={(event) => pickColor(event.target.value)}         value={color}         className='block w-full p-4 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-indigo-500 dark:focus:border-indigo-500'       >         <option>Pick a color</option>         <option value='indigo'>Indigo</option>         <option value='pink'>Pink</option>         <option value='purple'>Purple</option>         <option value='red'>Red</option>       </select>     </div>   ); } 
Enter fullscreen mode Exit fullscreen mode

This code came from the main App.jsx file, so go ahead and remove the select option from that.

We need to register this new entry point in our Vite config, so Vite knows it should create two files.
Open your vite.config.js file and place the following config in it.

import { defineConfig } from 'vite'; const { resolve } = require('path'); import react from '@vitejs/plugin-react';  export default defineConfig({   plugins: [react()],   build: {     rollupOptions: {       input: {         main: resolve(__dirname, 'index.html'),         options: resolve(__dirname, 'options.html'),       },     },   }, }); 
Enter fullscreen mode Exit fullscreen mode

Now Vite knows it's a multi-app project.
The last thing we want to do is modify our tailwind.config.js file as we now use subfolders.

/** @type {import('tailwindcss').Config} */ module.exports = {   // Old   content: ['src/*.jsx'],   // New   content: ['src/**/*.jsx'], }; 
Enter fullscreen mode Exit fullscreen mode

And that's it. You can now go ahead and build your application.
The user will now be able to modify the color through the options menu.

Browser extensions - Custom options page

You can find the complete code in this GitHub branch.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

beginnershtmljavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.