React for Chrome Extension Development

Initial Challenges with HTML and jQuery

When starting with Chrome extension development, we initially relied on injecting HTML strings directly into the inner HTML of divs on the page. This approach quickly proved to be difficult to maintain. Despite recognizing the issue, our lack of experience led us to adopt jQuery after some research. While jQuery offered animation support and facilitated web scraping, it didn't solve the core problem of maintainability. The absence of unidirectional data flow and straightforward styling made the code complex and cumbersome.

Realizing that jQuery wasn't the ultimate solution, we sought a better approach for future projects. The experience taught us the importance of choosing the right tools and frameworks to ensure code maintainability and scalability.

Choosing React for Octoshop

When embarking on the Octoshop project, a browser extension for price comparison across the web, we aimed to avoid past mistakes. We needed a framework that supported rapid UI development and iteration. React stood out for its extensive package ecosystem and strong community support, making it an obvious choice for our team, who were already familiar with it.

React allowed us to consolidate our business logic within the component tree, enabling the creation of complex UIs efficiently. The availability of open-source libraries, like charting tools, further streamlined development, requiring minimal customization to fit our needs.

Integration Challenges with Ibotta

Upon acquisition by Ibotta, we faced the challenge of integrating our React-based extension with Ibotta's existing Vue-based extension. The solution involved creating a messaging layer to bridge the two systems. Although functional, the integration was messy, prompting us to consider a complete codebase rewrite for better maintainability.

This experience highlighted the complexities of integrating disparate technologies and underscored the need for a more unified and maintainable solution.

Addressing Style Conflicts

One major issue encountered during development was style conflicts. We needed to prevent our extension's styles from interfering with the host website's styles. The solution lay in using shadow DOMs, which allowed us to encapsulate our CSS within a hidden DOM structure on the page. A utility function was developed to create shadow DOMs, into which we could inject our CSS and render React components.

This approach effectively isolated our styles, ensuring they remained unaffected by external styles and vice versa. It provided a clean and maintainable way to manage styling within the extension.

Rendering Components with React Portal

For features like price comparison on search pages, rendering components in a maintainable manner was crucial. We utilized React Portal to centralize our UI elements, such as buttons, in an array. This allowed us to inject them into specific divs on the page, keeping the code organized and state centralized.

React Portal provided the flexibility to choose where to inject components while maintaining a clean and consistent code structure. This method was instrumental in managing state and rendering effectively across the extension.

Managing State Between Chrome and React

Another challenge was synchronizing state between Chrome's storage APIs and React's state management. Chrome's APIs, with their own reactivity callbacks, were not well integrated into the JavaScript ecosystem, making state management cumbersome.

To address this, we developed custom hooks that interfaced with Chrome's APIs, simplifying the process. These hooks mimicked React's useState, allowing engineers to use familiar patterns without delving into Chrome's complex callback system. This abstraction significantly improved the maintainability and ease of use of the extension's codebase.

Understanding Chrome Extension Architecture

Chrome extensions consist of two main components: the content script and the background script. The content script runs on every tab, handling logic specific to each page, while the background script serves as a centralized process, akin to a server, managing communication and API calls via a messaging layer.

This architecture allows for isolated operations within the extension, ensuring that API interactions occur internally rather than directly on websites. Understanding this structure is crucial for designing efficient and secure Chrome extensions.

From Vanilla JavaScript to React

Our journey began with simple extensions built using vanilla JavaScript, evolving over time as we tackled more complex projects like UT Registration Plus. This extension simplified class registration for students, integrating features like grade distributions and professor ratings.

The progression from vanilla JavaScript to React was driven by the need for more robust solutions as projects grew in complexity. React's component-based architecture and state management capabilities provided the tools necessary to build scalable and maintainable extensions.

Conclusion

Throughout our experiences developing Chrome extensions, we learned the importance of selecting the right tools and frameworks to ensure maintainability and scalability. React proved to be a powerful ally in creating complex UIs quickly and efficiently. By addressing challenges such as style conflicts and state management, we improved the overall quality and usability of our extensions. The journey from basic HTML and jQuery to a sophisticated React-based solution highlights the value of continuous learning and adaptation in software development.

Watch full talk with demos and examples:

Watch video on a separate page
Rate this content
Bookmark

This talk has been presented at React Summit US 2024, check out the latest edition of this React Conference.

FAQ

They initially used vanilla JavaScript to build Chrome extensions.

They chose React because it allowed quick UI building and had strong community support and packages.

The main topic is making React work effectively in Chrome extensions.

The speakers are Shriram Hidiyaddin and Darshan Bhatta.

They founded a company called Octoshub, which was later acquired by Ibotta.

A shadow DOM is a hidden DOM used to avoid style conflicts with the website's existing styles.

They created custom hooks to integrate Chrome's state management with React's state, improving maintainability.

Using jQuery made the code unmaintainable due to lack of unidirectional data flow and complex styling.

They installed React DevTools core and managed tab visibility to connect and disconnect from the DevTools WebSocket.

They met while studying at UT Austin.

Sriram Hariharan
Sriram Hariharan
Darshan Bhatta
Darshan Bhatta
11 min
22 Nov, 2024

Comments

Sign in or register to post your comment.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
Understanding React’s Fiber Architecture
React Advanced 2022React Advanced 2022
29 min
Understanding React’s Fiber Architecture
Top Content
This Talk explores React's internal jargon, specifically fiber, which is an internal unit of work for rendering and committing. Fibers facilitate efficient updates to elements and play a crucial role in the reconciliation process. The work loop, complete work, and commit phase are essential steps in the rendering process. Understanding React's internals can help with optimizing code and pull request reviews. React 18 introduces the work loop sync and async functions for concurrent features and prioritization. Fiber brings benefits like async rendering and the ability to discard work-in-progress trees, improving user experience.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
RemixConf EU discussed full stack components and their benefits, such as marrying the backend and UI in the same file. The talk demonstrated the implementation of a combo box with search functionality using Remix and the Downshift library. It also highlighted the ease of creating resource routes in Remix and the importance of code organization and maintainability in full stack components. The speaker expressed gratitude towards the audience and discussed the future of Remix, including its acquisition by Shopify and the potential for collaboration with Hydrogen.
The Eternal Sunshine of the Zero Build Pipeline
React Finland 2021React Finland 2021
36 min
The Eternal Sunshine of the Zero Build Pipeline
For many years, we have migrated all our devtools to Node.js for the sake of simplicity: a common language (JS/TS), a large ecosystem (NPM), and a powerful engine. In the meantime, we moved a lot of computation tasks to the client-side thanks to PWA and JavaScript Hegemony.
So we made Webapps for years, developing with awesome reactive frameworks and bundling a lot of dependencies. We progressively moved from our simplicity to complex apps toolchains. We've become the new Java-like ecosystem. It sucks.
It's 2021, we've got a lot of new technologies to sustain our Users eXperience. It's time to have a break and rethink our tools rather than going faster and faster in the same direction. It's time to redesign the Developer eXperience. It's time for a bundle-free dev environment. It's time to embrace a new frontend building philosophy, still with our lovely JavaScript.
Introducing Snowpack, Vite, Astro, and other Bare Modules tools concepts!
Composition vs Configuration: How to Build Flexible, Resilient and Future-proof Components
React Summit 2022React Summit 2022
17 min
Composition vs Configuration: How to Build Flexible, Resilient and Future-proof Components
Top Content
Today's Talk discusses building flexible, resilient, and future-proof React components using composition and configuration approaches. The composition approach allows for flexibility without excessive conditional logic by using multiple components and passing props. The context API can be used for variant styling, allowing for appropriate styling and class specification. Adding variants and icons is made easy by consuming the variant context. The composition and configuration approaches can be combined for the best of both worlds.
Remix Architecture Patterns
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Remix Architecture Patterns
Top Content
This Talk introduces the Remix architecture patterns for web applications, with over 50% of participants using Remix professionally. The migration from single page applications to Remix involves step-by-step refactoring and offers flexibility in deployment options. Scalability can be achieved by distributing the database layer and implementing application caching. The backend for frontend pattern simplifies data fetching, and Remix provides real-time capabilities for collaborative features through WebSocket servers and Server-SendEvents.

Workshops on related topic

AI on Demand: Serverless AI
DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Top Content
Featured WorkshopFree
Nathan Disidore
Nathan Disidore
In this workshop, we discuss the merits of serverless architecture and how it can be applied to the AI space. We'll explore options around building serverless RAG applications for a more lambda-esque approach to AI. Next, we'll get hands on and build a sample CRUD app that allows you to store information and query it using an LLM with Workers AI, Vectorize, D1, and Cloudflare Workers.
High-performance Next.js
React Summit 2022React Summit 2022
50 min
High-performance Next.js
Workshop
Michele Riva
Michele Riva
Next.js is a compelling framework that makes many tasks effortless by providing many out-of-the-box solutions. But as soon as our app needs to scale, it is essential to maintain high performance without compromising maintenance and server costs. In this workshop, we will see how to analyze Next.js performances, resources usage, how to scale it, and how to make the right decisions while writing the application architecture.