Best Practices for Storing Static JSON Data in a Chrome Extension

In my extension, I have a collection of around 350 lines of static JSON data. This data is essential for the functioning of a content script, as it consists of key-value pairs that are checked upon user interaction to trigger specific actions. Here's an example snippet:

{
    "ABC": 323.32,
    "BDS": 23.12,
    "GTO": 96.52
}

Now, I'm contemplating the best location to store this data. Three ideas come to mind:

  • Embedding it directly within the content script.
  • Loading it in an Event Page and utilizing Message Passing to access the information.
  • Storing it in a JSON file and finding a way to retrieve it using XHR. Although, I believe this option may not be feasible.

While I am aware of technologies like localStorage and various storage types provided by Chrome, they seem more suitable for data generated during user interactions with the extension.

This JSON data remains static once the extension is installed until potential updates modify it. Currently, the data resides within the content script which is loaded each time a page is opened despite its unchanging nature. Considering this, would an Event Page be a better choice or is there a purpose-built solution for managing this type of static data?

Answer №1

To access the file, you can make use of XHR or fetch method. Simply utilize chrome.extension.getURL to obtain the URL of your specific file.

Here is an example of how it can be done:

fetch(chrome.extension.getURL('/data.json'))
    .then((response) => response.json())
    .then(function (jsonData) {
        console.log(jsonData);
    });

Additionally, remember to include the file in your manifest.json like this:

"web_accessible_resources": [
  "data.json"
],

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

What causes my React app menu to unexpectedly open while simply updating the state without any CSS modifications?

In the Header component, there is a button that triggers the toggleNav function in context.js when clicked. This function changes the state of isNavOpen from false to true, resulting in the opening of the navigation. Surprisingly, there doesn't appear ...

I am having trouble properly customizing the slotDuration in fullcalendar

Utilizing angular's fullcalendar plugin, I am attempting to modify the slots behavior to have only one slot per interval. To accomplish this, I have adjusted the settings as follows: ("#calendar").fullcalendar({ ... slotDuration: "00:90:01", ...

Animating Card depth on hover with Material UI

I am looking to add an animation effect to the Card component when the mouse hovers over it. I'm still fairly new to React and struggling with implementing it. Here is what I've tried so far: <Card linkButton={true} href="/servicios/ ...

Regular expression that can be used to extract information from a text file by filtering and returning only

When I open a text file, it contains several lines like the examples below. surname australia enter name j.jonhs enter name j.cause enter name f.london enter I am seeking assistance to modify the code snippet below so that it captures the first line m ...

Enhance Data3 Sankey to disperse data efficiently

There are a few instances where the D3 Sankey spread feature is showcased here. However, it seems that this specific function is not included in the official D3 Sankey plugin. Is there anyone who can assist me in obtaining the code for the Spread function ...

Creating an AJAX request in PHP to send data from multiple input fields with the same name

My goal is to create a poll system similar to Facebook for my project, but I am facing a challenge. I have implemented the following JavaScript code to add answers to inputs: $("body").on("click",".createnew", function(){ $(".inputsl").append("<d ...

Conceal descendant of list item and reveal upon clicking

In my responsive side menu, there is a submenu structured like this: .navbar ul li ul I would like the child menus to be hidden and only shown when the parent menu is clicked. Although I attempted to achieve this with the following code, it was unsucces ...

Tips for maintaining an active Chrome browser using Selenium in Python

Looking to automate opening the Google page using Python code in Visual Studio? Check out this snippet: from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager #browser = webdriver.Chrome(ChromeDriverManager().install()) Pa ...

Scraping multiple websites using NodeJS

I have been immersing myself in learning NodeJS and experimenting with web scraping a fan wikia to extract character names and save them in a json file. I currently have an array of character names that I want to iterate through, visiting each URL in the a ...

Is there a way to make res.render in node.js/express/mongo wait until the database search is finished before loading?

Having some difficulty with loading a table properly on my page because the information is not being passed to the ejs template before the page loads. I'm fairly new at this and could use some assistance! Just a heads up, owneditems consists of an ar ...

Ajax is able to fetch the URL without any issues, but the page is not being updated as expected. Instead,

I am currently working on implementing next/prev buttons using JavaScript, and have utilized a DynamicPage script for testing purposes. The only modification I made was to the beginning of the URL variable in pagenumber.js, although it essentially delivers ...

The text remains static and does not adjust its size when the screen is

I'm currently utilizing the text-only carousel feature found at this jsFiddle link. Below is the code I am using for resizing: window.addEventListener("resize", resetCarousel); function resetCarousel(){ setCarouselHeight('#carousel-text&apos ...

Is TypeScript being converted to JavaScript with both files in the same directory?

As I begin my journey with TypeScript in my new Angular project, I find myself pondering the best approach for organizing all these JS and TS files. Currently, it appears that the transpiler is placing the .js files in the same directory as the correspondi ...

Issue with floating date and time not functioning properly in ICS file for Yahoo Calendar

I have set up a calendar event in Google, Apple, and Yahoo calendars for each individual customer. The event is scheduled based on the customer's address at a specific time, so there should be no need for timezone conversion. However, I encountered an ...

Switch between different tables

I need assistance with implementing a functionality where there are two tables displaying different data and two buttons at the top of the page. Upon clicking one button, I want Table A to be displayed, while clicking on the other button will hide Table ...

Combining onmousedown and onchange for optimal event handling

There is a div with three slide-bars, each representing an 'rgb' value with a range of 0-255. When the slide-bars are dragged, the background-color of the div should change accordingly. Currently, an 'onchange' event is called to a func ...

The transition from material-ui v3 to v4 results in a redux form Field component error stating 'invalid prop component.'

Since upgrading from material-ui v3 to v4, I am encountering an error with all my <Field> components that have the component prop. Error: Warning: Failed prop type: Invalid prop component supplied to Field. The Field component is imported from im ...

What is the best way to incorporate multiple HTML buttons that utilize the same JavaScript function within looped results?

My HTML page contains the following codes: <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="metin" placeholder="Geben Sie Artikel Nr" style="width: 30%;"/><br/><br/> <input type="button" class="sin ...

Attaching a JQuery Event to a Custom Control on Google Maps

Struggling to enhance Google Maps with custom controls using CSS and JQuery. The documentation doesn't seem to address the issue I'm facing. Check out the markup, styling, and JQuery here. Following the instructions in the documentation, I adde ...

What could be causing my React Router to display empty pages?

I've been working with the latest version of create-react-app and have been struggling to display the pages within my app. I've tried both functional components and class-based components, but nothing seems to work. I've wrapped the content ...