Whenever I include __MSG_@@extension_id__ in my CSS file, I am encountering chrome://invalid

Currently, I am in the process of developing a chrome extension using Manifest V3. In my CSS file, I have incorporated the following code snippet to load local fonts.

@font-face {
    font-family: 'PoppinsLight';
    src: url('chrome-extension://__MSG_@@extension_id__/assets/Fonts/Poppins-Light.ttf');
}

Unfortunately, upon loading it into my browser, an error message pops up:

Request URL: chrome-extension://invalid/
Referrer Policy: strict-origin-when-cross-origin

To tackle this issue, I made sure to include web_accessible_resources in my Manifest. However, the error still lingers. Any assistance on this matter would be greatly appreciated. Thank you!

Answer №1

While struggling with the same error, I stumbled upon this page during my quest for solutions (it ranks high in Google search results). Fortunately, I managed to resolve the issue and am eager to share the solution with others facing a similar predicament.

The code snippet for the @font-face src mentioned in the original poster's question is accurate. However, the error message signifies that Chrome is unable to locate the specified font file. To address this, we need to ensure the file is accessible before attempting to load it, achieved through the web_accessible_resources property as outlined in our v3 manifest (refer to documentation here).

It's crucial to insert this property correctly within the manifest. For reference, you can examine an example of a sample manifest file here. Here's how it should be configured:

manifest.json

{
    "manifest_version": 3,
    ...
    "web_accessible_resources": [
        {
            "resources": ["assets/Fonts/Poppins-Light.ttf"],
            "matches": ["https://*.example.com/*"]
        }
    ]
}

Note: The matches key must adhere to specific match patterns (consult the documentation), ensuring it aligns with a complete domain name.

After updating your extension on chrome://extensions/ to reflect the manifest changes and make the file accessible, proceed to load your CSS into the webpage using various methods detailed below.



Injecting A CSS File

You have the option to inject your CSS file (containing the @font-face rule) directly into the website via your manifest file (view documentation here). Upon webpage loading, this injected file will automatically apply the styles.

manifest.json

{
    "manifest_version": 3,
    ...
    "content_scripts": [
        {
            "matches": ["https://*.example.com/*"],
            "js": ["content_script.js"],
            "css": ["path/to/your/style.css"]
        }
    ]
}


Linking A CSS File With JavaScript

Alternatively, utilize JavaScript in your content script to load the CSS file. Create a new <link> element, configure it, and append it to the webpage's <header> section.

Important: Remember to include your CSS file in the web_accessible_resources property of your manifest when utilizing this method. This ensures the CSS file's availability to both the content script and target website.

To access/load the CSS file using JavaScript, employ the chrome.runtime.getURL() method (documentation link). Consider the following example:

content_script.js

window.onload = () => {
    var newLink = document.createElement("link");
    newLink.rel = "stylesheet";
    newLink.type = "text/css";
    newLink.href = chrome.runtime.getURL("path/to/your/style.css");
    document.head.appendChild(newLink);
}


Injecting CSS With JavaScript

Incorporate CSS directly into a <style> element within the webpage's <header> using JavaScript in your content script. If any external files like a font file need loading, ensure they are accessible in the manifest under web_accessible_resources.

In the provided example, we embed the OP's @font-face rule into the webpage. Utilize the chrome.runtime.getURL() method to load the font file as demonstrated below:

content_script.js

window.onload = () => {
    var newStyle = document.createElement("style");
    newStyle.type = "text/css";
    var poppinsFont = chrome.runtime.getURL("assets/Fonts/Poppins-Light.ttf");
    newStyle.textContent = `@font-face {
        font-family: 'PoppinsLight';
        src: url('${poppinsFont}');
    }`;
    document.head.appendChild(newStyle);
}

I personally verified these approaches within my own extension to confirm their efficacy. If you continue encountering the error, verify your file paths and match patterns within the manifest file. Hopefully, this guidance proves beneficial!

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

Is there a way to assign the value of a textfield to a session attribute in JSP prior to rendering?

Imagine I have the following code snippet: <html> <head> <script> function setSession() { var roll=document.getElementById("rollno").value; session.setAttribute("rollno", roll); } & ...

Three fluid columns in a CSS flexible container

I need help with creating a fluid div container: width: 97%; min-height: 80px; -webkit-box-shadow: 1px 1px 5px 1px rgba(0,0,0,0.45); -moz-box-shadow: 1px 1px 5px 1px rgba(0,0,0,0.45); box-shadow: 1px 1px 5px 1px rgba(0,0,0,0.45); background: white; margin ...

Vue.js Dynamic Class Binding Fails to Update

As I delve into learning Vue Js, I have been working on a simple todo app. One interesting thing I've done is binding a class to my todo items based on whether todo.completed is true or not: <a v-bind:class="{iscomplete : todo.completed}& ...

Is there a way to send JSON data back to Riot.js from a Node.js express server?

I am in the process of creating a web application using Riot.js on the client side and Node.js express on the server side. One of the challenges I'm facing is how to mount JSON data with Riot.js for rendering HTML. Currently, I have JSON data stored o ...

Looking for a way to verify and match passwords in React Hook Form? Wondering if there's a built-in validation feature to display error messages with React Hook Form?

Having trouble validating the password and confirm the password in my form. Is there a specific property in useForm for this purpose in the latest version of react hook form? Any help would be appreciated. import React, { useState } from 'react' ...

Preventing Cyber Attacks: Protecting My Express Server from Potential Hackers

Recently, I set up a Digital Ocean Droplet on Ubuntu with Nginx and an Express Server as a proxy. This setup also includes Node and React JS. Lately, I've been encountering a recurring issue where upon waking up in the morning, my front-end app only d ...

Issue: The module '[object Object]' could not be located

const express = require('express'); const app = express(); const jade = require('jade'); const path = require('path'); const server = require('http').createServer(app); const io = require('socket.io').liste ...

Firefox failing to display fonts from Google Font API

The landing page our company outsourced does not display the Lato font in Firefox, resorting to a different option instead. We have received a stylesheet from fonts.googleapis.com that includes the following: @font-face { font-family: 'Lato'; ...

How does the use of <ol> with list-style-type: disc; differ from that of <ul>?

Why create a separate <ul> when visually both unordered lists and ordered lists look the same? <head> <title>Changing Numbering Type in an HTML Unordered List Using CSS</title> <style> ol { list-s ...

The absolute positioned div disappears once it moves beyond the boundaries of its parent container and enters a different container

Exploring the issue further, I have provided examples and a sandbox for better understanding. Take a look at this sandbox link with all dependencies included. In the image below, you can see that my dropdown menu is hidden behind other elements and only p ...

What could be causing my Bootstrap carousel to only slide once?

I'm currently working on integrating a Bootstrap Carousel into my website, but I've encountered an issue where it moves only once and then becomes unresponsive. After checking the file order, I couldn't find any issues there. So, I'm qu ...

What is the method for toggling the icon only when the ID matches?

After retrieving data from the API and mapping it out, I encountered an issue where toggling one icon would toggle all icons that were mapped out. My goal is to be able to click on an individual icon instead of affecting all icons. import { FaRegHeart, FaH ...

Unexpected patterns observed when utilizing parent/child routing files

I am working with a Node/Express backend that is implemented using TypeScript. Whenever I make changes to a file and save it, if I test the root route in Postman localhost:8000/, I receive the expected response. However, when I test localhost:8000/user af ...

The latest dev compile ran smoothly, however, the localhost webpage is not loading

Whenever I try running npm run dev, localhost just seems to keep loading endlessly. The screenshot of the response can be found in image 1: https://i.sstatic.net/HaUA7.png I initially suspected that a recent change might have caused this issue, but after ...

Passing parameters in a callback function using $.getJSON in Javascript

I am currently using a $.getJSON call that is working perfectly fine, as demonstrated below. var jsonUrl = "http://www.somesite.co.uk/jsonusv.php?callback=?"; $.getJSON(jsonUrl,function(zippy){ ...some code } However, I want to pass a ...

Using the for loop to asynchronously fetch data and access the finished result variable

Exploring the world of asynchronous JavaScript, I find myself pondering a question: how can I ensure that my code only starts working on a created array once all queries are completed? My current approach involves fetching pages in a for loop. Here's ...

Image carousel

I'm in need of an HTML slider similar to selectToUISlider, but I want it to display images instead of text values. Can anyone recommend something like this? I've been looking online without any luck so far. Thank you in advance. ...

Storing the values of a React JS application in local storage using

Storing data received from the backend in local storage: async onSubmit(e){ e.preventDefault(); const {login, password } = this.state; const response = await api.post('/login', { login,password }); const user ...

What is the best way to receive real-time updates from an ongoing PHP call triggered by AJAX?

Is there a way to receive live updates from an ongoing PHP process called via AJAX, where the flushed output from the PHP script can be obtained? var proc; $('#start').click(function () { proc = $.ajax({ type: 'POST', ...

Implementing Form Validation on the Client Side Using React and Material-UI Components

Many material-ui components come with the errorText prop, which can be used to display errors. However, implementing this in React becomes more complex when dealing with forms that have a large number of field components. Any recommendations on the most e ...