Adjust the Angular menu-bar directly from the content-script of a Chrome Extension

The project I've been working on involves creating an extension specifically for Google Chrome to enhance my school's online learning platform. This website, which is not managed by the school itself, utilizes Angular for its front-end design.

Within the extension, I am utilizing Typescript along with Parcel, and employing content-scripts to run scripts on the webpages. My goal is to add an item to the menu-bar located on the left side of the screen. So far, I have attempted a couple of methods:

  • Initially, I tried inserting pure HTML directly into the ul element of the menu-bar, but this action ended up breaking the entire functionality of the menu, rendering it unusable for the user.

  • Another approach I took was using Angular (installed via NPM) in the content-script to locate the scope of the menu-bar element, adding a new option to the array of the menu-bar, and applying it. However, this method also failed to yield the desired results. Here's a snippet showcasing the expected vs. received behavior:

    Expected behavior (using browser console)

    let target = document.getElementsByClassName("main-menu")[0];
    let menu = angular.element(target);
    console.log(menu);
    >>> p.fn.init [ul.main-menu, context: ul.main-menu]
    

    Received Behavior (from content-script)

    let target = document.getElementsByClassName("main-menu")[0];
    let menu = angular.element(target);
    console.log(menu);
    >>> p.fn.init [ul.main-menu]
    

    This discrepancy in contexts leads me to believe that my content-script and the webpage’s script are not operating within the same scope. Is this assumption correct? Additionally, I have encountered difficulty utilizing the .scope() method within the content-script.

Provided below is the contents of my manifest.json file for the plugin. Any guidance or suggestions would be greatly appreciated as I navigate through this unfamiliar territory of Angular.js.

{
    "name": "Magister Plus",
    "description": "TBD",
    "version": "0.0.1",
    "manifest_version": 3,
    "permissions": [
        "activeTab",
        "scripting",
        "tabs"
    ],
    "background": {
        "service_worker": "./background/background.js"
    },
    "host_permissions": [
        "https://*.magister.net/*"
    ],
    "content_scripts": [
        {
            "matches": [“https://*.magister.net/*”],
            "js”: ["./content/domain.js"]
        },
        {
            "matches": [“https://*.magister.net/magister/#/vandaag”],
            "js": ["./content/vandaag.js"]
        }
    ],
    "action": {}
}

I find myself at a loss regarding the next steps to take due to my limited familiarity with Angular.js. Any assistance provided would be immensely helpful. Thank you!

Answer №1

  1. If you want to modify the HTML, remember to use appendChild or insertAdjacentHTML instead of innerHTML. Also, make sure to execute the code in the page context to access the context property. By @wOxxOm

Wow, this solution truly did the trick! I really appreciate your help!

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

Utilize a single JavaScript script to handle numerous HTML forms within the same webpage

I am currently facing an issue with a page that contains multiple forms needing the same JavaScript code. This specific code is designed to add more input fields to the form, which it does successfully. However, the problem lies in the fact that it adds in ...

Learn how to efficiently pre-load data using the prefetchQuery method in React-Query

Attempting to pre-fetch data using react-query with prefetchQuery. The network tab in browser DevTools shows the requested data coming from the back-end, but strangely, the data is not present in the react-query DevTools cache. Any ideas on what might be c ...

Enhance your AJAX calls with jQuery by confidently specifying the data type of successful responses using TypeScript

In our development process, we implement TypeScript for type hinting in our JavaScript code. Type hinting is utilized for Ajax calls as well to define the response data format within the success callback. This exemplifies how it could be structured: inter ...

What steps can I take to stop the mui TreeView component from intercepting events intended for another component?

Within my application, I have implemented a TreeView component that is displayed alongside an Options component containing two buttons. return ( <div style={{ display: "flex", flexDirection: "column", height: ...

Troubleshooting GLSL scripts within a web-based WebGL environment

Are there ways to debug GLSL code or display variable values directly from within the GLSL code when using it with WebGL? Does three.js or scene.js offer any features for this purpose? ...

While the NPM package functions properly when used locally, it does not seem to work when installed from the

I have recently developed a command-line node application and uploaded it to npm. When I execute the package locally using: npm run build which essentially runs rm -rf lib && tsc -b and then npm link npx my-package arguments everything works sm ...

How can the client be informed about the ongoing processing of the request by the servlet?

In my web application, I have JS/jQuery on the front end and servlets on the back end. When making a request to a servlet, it performs multiple tasks in one call (such as executing a shell script that runs various Python scripts). My main query is whether ...

How to convert a JSON response into a select box using VueJS?

I have a VueJS component where I need to populate a html select box with data from a JSON response. This is my VueJS method: getTaskList() { axios.get('/api/v1/tasklist').then(response => { this.taskList = this.data.taskList; ...

Difficulty adding extra arguments to a function

I am currently working on a function in d3 that aims to evaluate the "time" of my data and determine if it falls within specific time intervals. This will then allow me to filter the data accordingly. //begin with a function that checks if the time for eac ...

Ways to iterate through a JSON formatted array variable using JavaScript

Below is my code snippet, used to plot all the locations fetched from my database onto a Google map. $.ajax({ url:"http://localhost/church_finder/index.php/MapController/search_church", type:'POST', data: ...

The reactjs-toolbox radio button group remains unchanged

In my React application, I have implemented radio buttons using the following code: <RadioGroup name='steptype' className={css.ProcessStepRadioButtons} value={this.state.stepsData[stepNumber].stepType} onChang ...

Assign a value to a variable within an asynchronous function

Initially, I understand that initializing variables inside asynchronous functions is not a feasible approach. However, I require assistance in rectifying this issue. In the following example, I have an observable and aim to extract its variable for use i ...

When evaluating code with eval, properties of undefined cannot be set, but the process works seamlessly without

Currently, I am attempting to utilize the eval() function to dynamically update a variable that must be accessed by path in the format myArray[0][0[1][0].... Strangely enough, when I try this approach, I encounter the following error: Uncaught TypeError: ...

Encountering difficulties when attempting to upload a file to Google Cloud Platform using Multer in a Node.js

I am currently experimenting with uploading a single file using Multer and the "multipart/form-data" content type to a Google Cloud Storage bucket. For this task, I am utilizing "Multer.memoryStorage()" and "@google-cloud/storage" try { const docume ...

Retrieve identification details from a button within an ion-item located in an ion-list

<ion-list> <ion-item-group *ngFor="let group of groupedContacts"> <ion-item-divider color="light">{{group.letter}}</ion-item-divider> <ion-item *ngFor="let contact of group.contacts" class="contactlist" style="cl ...

The component does not contain the specified property

One Angular 4 component that I have is like this: export class MenuComponent { constructor(private menuService: MenuService) { } @Input(nodes):any; getMenu(path:string): void { this.menuService.getData(path).subscribe(data => { // Re ...

Angular design featuring numerical staircase pattern

I'm attempting to implement a numeric version of the staircase pattern using Angular, but I have yet to find the solution. Here is the desired outcome: https://i.sstatic.net/KTU3k.png Below is the code I have developed thus far: main.ts import { Co ...

Collections of both letters and non-letter characters that are aligned

I am attempting to identify sets of characters that contain a mix of letters and non-letter characters, with many of them being just one or two letters. const match = 'tɕ\'i mɑ mɑ ku ʂ ɪɛ'.match(/\b(p|p\'|m|f|t|t ...

Where should data processing be conducted: in the service or controller layer?

Here's a question regarding the best practices for organizing code. I'm fetching data from an API using $resource and I need to manipulate it before displaying it on the view. My dilemma is at which stage should I process the data? My current b ...

Add a module to the main module or main component within the project

I have integrated a third-party library to manage the layout of my Angular application. I am considering importing it either in app.module.ts (the root module) or in app.component.ts (the root component). Do you think there would be any significant diff ...