JavaScript-powered Chrome Extension designed for modifying CSS styling

Like many others, I've embarked on creating a Chrome Extension to modify the CSS of a specific webpage.
Despite reading through various threads, my approach is a bit more complex than what has been discussed. The page I want to style features a select list with one or more options.
Here's a basic representation:

<select id="users"><option value="123">Username</option></select>

The '123' represents a unique user ID for each individual.
My goal is to dynamically insert a CSS file based on this user ID. Ideally, the new CSS would be injected like so:
//mysite/myfolder/cssfiles/ #userID# .css

The challenge lies in customizing a page that isn't mine, making server-side modifications impossible. Nevertheless, I aim to create an Extension for my colleagues so they can feel a sense of uniqueness whenever they view the webpage.

The current manifest.json file includes the following content_script:

"content_scripts": [
{
"js": ["myscript.js"]
}
]

Here's the code within myscript.js:

var user = document.getElementById("users");
var id = user.options[user.selectedIndex].value;
var name = user.options[user.selectedIndex].text;
var showinfo = alert("Userid: " + id + "\nUsername: " + name);

Upon page load, the alert successfully displays the user data, but I'm still exploring how to leverage this information to inject my desired CSS.

I've also tested adding "css": ["mystyle.css"] alongside "js": for testing purposes, but this applies a fixed style and doesn't cater to the selected user. Nonetheless, it achieves the desired look as specified in the CSS.

Answer №1

A simple method for accomplishing this task would involve the following steps:

let identifier = window.prompt("Please input your id","default");
let fileLocation = identifier + ".css";
chrome.tabs.insertCSS(tabId,{file: fileLocation});

It is important to note that you will need to have the appropriate tabs permission specified in your manifest.

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

Try utilizing the array find() method in place of a traditional for loop

Is there a better way to refactor this code using the Array.find() method instead of nested for loops? onLoadTickets() { const ticketsReq = this.ticketService.getTickets(); const tariffsReq = this.tariffService.getTariffs(); forkJoin([ticketsR ...

Issues arise when Angular animations fail to function properly due to the addition of styles through the [ng

Exploring Angular animations and seeking advice. Currently, I am utilizing ngStyle to show or hide text on hover. The goal is to smoothly animate the text as it appears on the screen. Are there alternative methods to achieve the same hover effect without r ...

What is the best way to stop a jQuery event listener on a button within a form from triggering upon the user hitting the Enter key?

Here is an example of a form containing a button: <form> <input type="text"> <button>Click Me</button> <input type="submit" value="Submit"/> </form> Using the following JavaScript: $('form').subm ...

Troubleshooting VueJS: Issues with V-for display behavior

Is there a way to use v-for to arrange table data in a format similar to the code below: <tr> <th scope="col">Name</th> <th scope="col">Price</th> <th scope="col">Product ID</th> <th></th& ...

Submitting a JSPX form to interact with PHP backend

Can a JSP/JSPX form be used to submit data to a PHP file? The PHP file will handle validation and database updates, then send a response back to the same JSP/JSPX form. The process should be done using AJAX with jQuery. What steps are involved in achievi ...

To ensure that any changes made to data are reflected automatically when viewing data in Angular 2

In the process of developing an Angular 2 application, I encountered a scenario that requires special attention. The data displayed on the view is fetched from an API, with certain fields being editable by the user. These modifications can be saved using ...

Each slider only displays a single database entry

I have an array that retrieves testimonials from a database table and displays them using a foreach loop. I am looking to implement a slider that can fade in and fade out the results. Here is my database query: $showTestimonials = array(); $getTestimoni ...

Maintaining my navigation menu as you scroll through the page

I've been working on creating a website for my business but I'm facing a challenge. My goal is to have a fixed navigation bar that stays in place as people scroll down the page, similar to what you can see on this website: (where the navigat ...

Creating dynamic axes and series in Ext JS 4 on the fly

I am looking to dynamically generate the Y axis based on a JSON response. For example: { "totalCount":"4", "data":[ {"asOfDate":"12-JAN-14","eventA":"575","eventB":"16","eventC":"13",...}, {"asOfDate":"13-JAN-14","eventA":"234","eventB":"46","even ...

AFRAME - Enhanced scene overlay

I am currently exploring ways to implement an overlay menu on top of an A-FRAME scene. A great example of what I am aiming for can be found on this website -> By clicking on one of the tiles in the home menu, you will see that the previous scene is mo ...

The Jquery animate function is not compatible with the transform property

I am facing an issue with the Jquery animate function. Despite trying various solutions, I have been unable to get it to work. Below is the HTML code that I have used: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

Integrating HTML elements based on scroll movement

Is there a way to dynamically add a new Bootstrap row to my HTML code when scrolling the mouse downward using JavaScript? <div class="row"> <div class="col-md-6 col-xs-6" > <div> <img src="img/picture1.jpg" a ...

If there are multiple Monaco diff editors present on a single page, only the first instance will display the diff

I'm currently working with a Vue component that renders a diff editor using Monaco. However, when I have more than one instance of this component on the same page, only the first one displays the diff highlights. Here is the template: <template> ...

Personalized Facebook Share Dialog - unique title and description

I need to send a link using the FB Send Dialog with custom name and description. FB.ui({ method: 'send', name: 'Custom name', display: 'popup', link: 'http://link.com', to: facebookUserId, description: 'Custom ...

I am looking to remove all white spaces from my website's HTML code

I am looking to remove all white spaces from my website in html. The more I align my text to the right, the more whitespace it creates on the right side of the page. As seen in the images, there are a lot of whitespaces and I suspect that elements are caus ...

Issue with using Javascript variables within Highcharts

I am facing an issue with displaying a high charts pie chart dynamically. When I pass the exact value format into the data index in the high chart, it doesn't show anything in the chart. However, if I directly assign a value to a variable, it works fi ...

Responsive grid made with HTML and CSS to create dynamic DIVs

Currently, I am working on a project that requires a responsive page layout using divs. The layout should resemble a grid, with 3 columns that reorganize into 2 columns on smaller screens. Here is the basic HTML structure I have so far: <div id="main" ...

Having trouble with Vue component registration repeatedly failing

Currently, I am working on a front-end project using [THIS VUE TEMPLATE][https://www.creative-tim.com/product/vue-material-dashboard-pro] The issue I am facing involves trying to register a component locally and encountering the following error: "1 ...

``In three.js, is there a trick to perfectly translateZ on camera when capturing footage

My current use of the translateZ function allows the camera to move forward or backward along its lookat direction. However, because translateZ moves it relatively, performing camera.translateZ(10); and then another camera.translateZ(10); will result in a ...

The inner division appears to have a larger bottom margin than expected

Can anyone explain why the .slot or .card classes in this code have a larger margin at the bottom compared to the top? Appreciate any help, Link to JsFiddle: http://jsfiddle.net/Tighttempo/LgeAf/ <div id="hand"> <div class="card" id="card1" ...