The Tailwind CSS Chrome extension is causing disruptions on the websites I view

Currently, I am in the process of creating a chrome extension using various tools like React, Typescript, TailwindCSS, and a custom Webpack configuration. To enhance user experience, I have modified the default action in manifest.json so that clicking on the toolbar icon triggers a message opening a content script, which then injects an overlay onto any visited site.

However, I'm encountering an issue where my Tailwind styles are affecting every website I visit, sometimes causing unintended consequences. For example, Google search websites display smaller titles, while on Reddit, my extension results in much smaller text than desired.

My goal is to restrict the application of my extension styles solely to the overlay I've created. I attempted to use React Shadow DOM, but it didn't yield the expected outcome, indicating that I might have made an error during implementation.

Below are the key files involved:

manifest.json

{
    "name": "Intro",
    "description": "Intro extension",
    "version": "1.0.0",
    "manifest_version": 3,
    "icons": {
        "16": "icon.png",
        "48": "icon.png",
        "128": "icon.png"
    },
    "action": {
        "default_title": "Intro",
        "default_icon": "icon.png"
    },
    "permissions": ["alarms", "contextMenus", "storage"],
    "options_page": "options.html",
    "background": {
        "service_worker": "background.js"
    },
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["contentScript.js"]
        }
    ]
}

contentScript.tsx

import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { Messages } from '../utils/types';
import '@fontsource/roboto';
import { useDelayUnmount } from '../hooks/useDelayUnmount';
import App from '../App';
import './contentScript.css';

// Remaining code for ContentScriptOverlay component

contentScript.css

@tailwind base;
@tailwind components;
@tailwind utilities;

#intro-extension ::-webkit-scrollbar {
    width: 0px;
    background: transparent;
}

.overlay {
    width: 376px;
    z-index: 9999;
    position: fixed;
    border-radius: '6px 0px 0px 6px';
}

@keyframes inAnimation {
    0% {
        right: -376px;
    }
    100% {
        right: 0;
    }
}

@keyframes outAnimation {
    0% {
        right: 0;
    }
    100% {
        right: -376px;
    }
}

I would greatly appreciate any assistance or guidance provided!

Answer №1

Utilizing the PostCSS Parent Selector library can help achieve this functionality.

Ensure to generate a postcss.config.js file at the root of your project and integrate the plugin in the following manner.

module.exports = {
    plugins: {
        ...
        'postcss-parent-selector': { selector: '.parentclass' }
    }
}

Answer №2

Regarding the technique of using Tailwind's important, selector strategy as mentioned by @Anh-Thi DINH in the previous comment, I have found that some of the underlying pages were still undergoing modifications.

For instance, the links on a Google search results page were transitioning from blue to grey unexpectedly.

Fortunately, the PostCSS Parent Selector package proved to be a reliable solution.

Answer №3

Seems like there may be a misuse of Shadow DOM in React.

Check out this corrected example:

const element = document.createElement('my-extension');
document.documentElement.appendChild(element);
const shadowRoot = element.attachShadow({ mode: 'open' });

const container = document.createElement('div');
shadowRoot.appendChild(container);

ReactDOM.render(<ContentComponent />, container);

After making these changes, your React implementation should function properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hapi and Bell's attempt at authenticating with Twitter was unsuccessful

Currently, I have developed a basic backend API that requires multiple authentications. My current challenge is connecting to the Twitter API using Bell. However, instead of displaying the authentication page for the app, an error is being shown: {"statusC ...

Ways to slow down page transition on NextJs

I'm currently working on securing my private pages using a HOC withAuth. While the protection is functioning correctly, I am looking to avoid users seeing a loading screen for a split second while the access token is being retrieved from local storage ...

The MUI Drawer does not open with styled components when the variant is set to temporary

I have customized a MuiDrawer component to apply some unique styling. Despite setting the variant to temporary, the Drawer fails to open when the open prop is passed. Strangely, switching the variant to permanent results in the Drawer being displayed succe ...

Angular's array filter functionality allows you to narrow down an

I am working with an Array and aiming to filter it based on multiple criteria: primasSalud = [ { nombre: 'one', internacional: true, nacional: false, nacionalSinReembolso: false, nacionalClinicasAcotadas: false ...

Tips for customizing the timing of a Bootstrap modal's opening delay?

I have integrated gem "bootstrap-sass", "~> 2.3.0.0", which indicates that I am utilizing Bootstrap 2. Detailed information on the modal can be found here. The code for my custom modal looks like this: #Modal.modal.hide.fade{"aria-hidden" => "true" ...

Error: Unable to access the 'date' property of the 'Chat' type

chats object //I have an object containing chats retrieved from firebase date: nt nanoseconds: 269000000 seconds: 1684252737 lastMessage: {texts: 'So all the image disappears'} userInfo: {uid: '97iObwAHkQcnyq1CHgwnPg2f4Ga2', username: & ...

Ensuring consistency in aligning float elements

I've been struggling to create a concept design for my internship project. I aim to have a page with six clickable elements (images). When one is clicked, the others should disappear and the active one moves to the top. I managed to achieve this using ...

Creating a scrollable table with CSS: A step-by-step guide

My table has too many columns causing it to exceed the width of the screen. I would like to make it scrollable horizontally for a neater and more organized appearance. I believe CSS with the overflow hidden property can achieve this, but I am unsure where ...

Update the JSON data label

When I receive a category list, it looks like this: 0: name: "Acessórios para Veículos" uuid: "d87506-bb-08-d0-58c2d4af" 1: name: "Agro, Indústria e Comércio" uuid: "d87507-bb-07-d0-57c2d4af" However, the component I am using does not recognize the n ...

Tips on displaying tooltip specifically for displaying x values only in React charts

Currently, I have a line chart that displays only four values, yet my datasets contain more data points. The issue is that the dot on the line is shown for all datasets. Is there a way to show the circle pointer only for x values that are displayed? https ...

Display an element that has been tucked away, and then bring it to life

Is there a way to implement an animation that causes the hidden form to slide from right to left after the .button class is hidden? I have been able to achieve similar effects individually, but struggle with synchronizing their animations. When the butt ...

What are some techniques for obtaining the second duplicate value from a JSON Array in a React JS application by utilizing lodash?

Currently, I am tackling a project that requires me to eliminate duplicate values from a JSON array object in react JS with specific criteria. My initial attempt was to use the _.uniqBy method, but it only retained the first value from each set of duplicat ...

What is the best approach for testing the TypeScript code below?

Testing the following code has been requested, although I am not familiar with it. import AWS from 'aws-sdk'; import db from './db'; async function uploadUserInfo(userID: number) { const user = db.findByPk(userID); if(!user) throw ...

React component state not being updated as expected

I've been developing an app for managing income and expenses, which allows users to input data and select the type of income or expense from a dropdown list. The issue I'm facing is that my Total Income (Total expense will be added once this pro ...

Nesting objects within arrays using Typescript Generics

Hello, I am currently attempting to assign the correct type to an object with nested values. Here is a link to the code in the sandbox: https://codesandbox.io/s/0tftsf interface Product { name: string, id: number productList?:ProductItem[] } interf ...

The table toggle feature seems to be malfunctioning in Safari, whereas it works perfectly in Chrome

My table includes a td element that serves as a toggle switch, transitioning between 3 states flawlessly in Chrome. However, I am facing issues with its functionality in Safari and seek assistance in rectifying the issue to ensure cross-browser compatibili ...

Guide on accessing a modal component in Angular?

I have an Edit Button on my component called SearchComponent. When the user clicks this button, it currently redirects them to another component named EditFormComponent using navigateByUrl('url-link'). However, I would like to enhance the user ex ...

Adding custom CSS and JavaScript to an Angular 4 project can be done by including the necessary

When working with Angular 2, I was able to include stylesheets directly in the index.html like so: <link rel="stylesheet" href="css/mycss.css"> However, with Angular 4, the styles need to be added to the angular-cli.json file within the styles and ...

The property this.props.Values is not defined

I'm facing an issue with a page. Specifically, I am working with the value: this.props.CategoriesList. This value represents a list of categories. The problem is that when I click on a button to navigate to the page where this value is used, it shows ...

What steps can I take to resolve the CSP errors I am experiencing?

I am currently working with NextJs@12 and I am attempting to set up CSP for my application. Unfortunately, I keep encountering errors in my console and I cannot figure out where I am going wrong. Below is the current policy that I have in my next.config fi ...