Switching image source using Firefox add-on

I am venturing into the world of creating my initial browser extension for Firefox. Currently, I am testing it in Firefox Dev v120. I have added an image to the 'images' directory of the extension and have specified "web_accessible_resources" in my manifest file. However, the image refuses to load.

Initially, I was using manifest version 2, but I also experimented with manifest version 3 after reading Mozilla's guidelines here.

Unfortunately, when using manifest version 2, the image does not display. On the other hand, if I switch to manifest version 3 and include "matches", the extension encounters an error.

This is a snippet from my manifest.json (version 2)

{
    "manifest_version": 2,
    "name": "Newscorp",
    "version": "1.0",
  
    "description": "An attempt to replace the logos of Newscorp news sites with more accurate ones",
  
    "icons": {
      "48": "icons/border-48.png"
    },
  
    "content_scripts": [
      {
        "matches": ["*://*.heraldsun.com.au/*"],
        "js": ["newscorp.js"]
      }
    ],

    "web_accessible_resources": [
        "images/Herald-Scum.png"
      ]   
  }

If I were to use manifest version 3, the web resources section would look like this:

"web_accessible_resources": [
    {
      "resources": ["Herald-Scum.png"],
      "matches": ["/images/*"]
    }
]

Below is the code snippet of my JavaScript file

document.body.style.border = "2px solid red";
var head= document.getElementsByClassName("header_link_image")[0];
head.src = "images/Herald-Scum.png";

Upon debugging in the browser (using version 2 manifest), I notice that the image source has been replaced: https://i.sstatic.net/XeteQ.png

However, it seems like the extension is attempting to load the image from the website URL instead of its own extension directory, resulting in a 404 error as the image doesn't exist on the website: https://i.sstatic.net/UKVVa.png

Answer №1

I was able to solve my issue.

To link the image to the extension's URL, I used this line of code: browser.runtime.getURL

Here is the JavaScript code I implemented:

document.body.style.border = "5px solid red";
var head= document.getElementsByClassName("header_link_image")[0];
head.src = browser.runtime.getURL('/images/Herald-Scum.png');

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

Several middlewares using router.params()

Is it possible to include multiple middlewares as parameters in the function router.params() in Node-Express? I currently have the following setup: const checkAuth = (req, res, next) => {console.log("checking auth"); next()} const checkAuth = ...

What is the best way to import modules with the "@" symbol in their path when working with Node.js?

Situation In my VueJS project, I have created service modules for use with vue cli. My code makes use of the @ symbol to easily access files within the src folder: /* Inside someService.js */ import API from '@/services/APIService.js' Ch ...

Incorporating momentum into Laravel using webpack

It has come to my attention that the moment JS library is experiencing a known issue when attempting to require ./locale. Unfortunately, the problem remains unresolved and it is recommended to seek out solutions from various tickets. While I am open to imp ...

"The position of the Textbox shifts suddenly following the input of text

One interesting quirk I've noticed in my HTML code is that a text box with a jQuery button attached to it seems to shift down by about 4 pixels when the user enters text and then moves away from the box using the tab key or mouse. <div class=&apos ...

Difficulty accessing `evt.target.value` with `RaisedButton` in ReactJS Material UI

My goal is to update a state by submitting a value through a button click. Everything works perfectly when using the HTML input element. However, when I switch to the Material UI RaisedButton, the value isn't passed at all. Can someone help me identif ...

The steps to implement an onchange function for displaying image previews in a profile image tag

Within my code, there is a profile image tag along with an input tag for updating the image. I aim to implement a functionality that allows me to select a new image and automatically update the profile picture when it changes <div class="col-sm-6"> ...

Updating multiple values within a JSON object, jsObject, or string

After receiving a response from a web service, I need to replace certain values in the response with custom values that I have defined. One possible approach is to create a tree traverser to identify the specific values and replace them with the custom va ...

Ways to modify color while user moves the screen

I'm working with some jQuery code that changes the colors of elements as the user scrolls down, and I want it to revert back to the original color when scrolling back up. However, the script is not behaving as expected... Here is the original working ...

Is it acceptable to initiate an import with a forward slash when importing from the root directory in Next.js?

I've noticed that this import works without any issues, but I couldn't find official documentation confirming its validity: // instead of using a complex nested import like this import { myUtil } from '../../../../../lib/utils' // this ...

Unveiling the essence of Lodash's differenceBy functionality

I am facing a challenge with two arrays of objects. I need to identify the differences between the newData and oldData arrays based on their identifiers. Specifically, I want to display objects from oldData whose identifiers are not present in newData. Her ...

Update the hidden input's value by the ZIP code entered

I'm currently working on setting up arrays for specific regions and then comparing them to the zip code entered in order to designate the value of a hidden field (which signifies the region). No matter what I input, it always seems to result in "Not F ...

How do I go about utilizing or bringing in those constants within the Drawerr.js module?

Currently, working with Next.js, I have defined some constants in the Nav.js file: export default function NestedList() { const [value,setValue]=React.useState(); const theme=useTheme(); const isMatch=useMediaQuery(theme.breakpoints.down('lg&apo ...

Incorporating custom HTML5 player to watch Youtube videos on my website

I'm trying to display YouTube videos on my website using a simple video tag. I've managed to retrieve the encoded url of the video from the page source and successfully download it through IDM, but when I try to use this URL as the source for an ...

Tactile interactions on iPhone

My goal is to create an off-canvas menu that can be opened with touch events in a systematic way. It functions perfectly in my browser when I click and drag on the body to reveal the menu. However, it encounters a problem on the iPhone. The error message ...

What methods can Ajax utilize to make asynchronous requests and receive synchronous responses?

While using jQuery ajax to send a request to a web service, I came across an interesting bug: var AjaxResult; login = function () { AjaxResult = ""; $.ajax({ type: "POST", url: KnutBase.getServiceUrl() + "ServiceInterface/HmsPlanne ...

redux - managing asynchronous storage using key-value pairs

Utilizing associative arrays with redux and storing them in async storage for later retrieval is my current challenge. When using redux, I am able to quickly access the values and efficiently map the content into cards in my react native app. However, aft ...

Send a substantial item for display using Express Layout

Utilizing Express layouts to render my webpage upon accessing a specific route: The current project I am developing is an admin panel designed for editing a substantial table of data (imagine a website dedicated to managing thousands of product entries, fo ...

PHP Loop News/Image Slider with Clickable Interval Reset and Improved Unique ID Formatting

Currently, I am in the process of setting up a news/image slider on my website using JavaScript. I have the slide data coming through a PHP loop with unique IDs, which is functioning smoothly. However, I am struggling to figure out how to reset the timer/i ...

Enhancing server-generated content with AngularJS

Is there a way to seamlessly integrate ng-repeat with static content? In other words, is it possible to send static divs and then bind them to a JavaScript array (or create an array from the content and bind it later)? I understand that one solution could ...

What is the best way to fetch all records where the property matches a portion of the specified parameter?

Below is the Mongoose Schema that I currently have var People= new Schema({ firstName: String, alias: [String] }); I am trying to find a method to retrieve all documents where one of the alias strings matches or is contained in some way withi ...