What would be the most effective method for storing an array in a Chrome extension?

The code in my background.js file is currently working as intended:

var targetList = ["youtube.com", "vimeo.com"];  

for (var i = 0, n = targetList.length; i < n; i++) {
    if (sender.tab.url.indexOf(targetList[i]) != -1) {
        // do something
    }
}

However, I now need to expand the targetList with more references and use it multiple times within my background.js.

What would be the best approach to store the targetList separately and access it when required? Do I need to make any changes to my manifest.json file?

Answer №1

If you find yourself frequently needing to access certain data, consider storing it in memory or on a separate background page to retrieve as needed. For more long-term use, you can save the information in local storage using the following code snippet:

chrome.storage.local.set({"savedData": savedData});

To fetch the data later on, you can use the following code:

chrome.storage.local.get("savedData", importantFunction);

function importantFunction(result) {
  savedData = result.savedData;
}

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

Middleware in Express.js that allows a function to be executed every time `res.send()` is called

As of now, I am creating APIs using express.js and I aim to send a standard response format like so : res.send({status : 'suceess' , msg : '' , data : [] }); Across all the controllers (functions managing my routes) My attempted sol ...

Ways to verify if one div in jQuery contains identical text to another div

I need help with a jQuery script to remove duplicate text in div elements Here's the requirement: If div1 contains the text "hi" and div2 also contains "hi", then div2 should be removed Below is my current code snippet: <script src="https:/ ...

What is the best way to integrate JavaScript and Python for seamless collaboration?

I'm looking to create a bidirectional communication model between JavaScript and Python. The idea is for JavaScript to handle data processing, send it to Python for further processing, and then receive the results back from Python. However, I'm u ...

What is the best way to pass an array of 8-digit strings from an input in Angular to a Node.js backend?

I am currently facing a challenge where I need to pass an array of 8 digit strings from an Angular input to a Node.js endpoint. The method below works perfectly fine when passing a single string, but how can I handle an array of 8 digit strings as input? ...

How can I incorporate arithmetic operators within a function in EJS?

Currently, I am developing an express app that includes a booking form using ejs with added functionality for payment processing. In the code, I have a select tag where the selected text is stored in a variable. Although console logging shows the value co ...

The alert box fails to display in the correct orientation when the condition is activated

Currently, I am working on implementing a sign-up feature using PHP. My main goal is to successfully store the user-entered data in MySQL database while ensuring that no duplicate usernames are allowed during account creation. Although everything is functi ...

What is the best way to dynamically update or display unique CSS styles when a service is invoked or provides a response in AngularJS using JavaScript

Seeking to display a unique CSS style on my HTML FORM prior to invoking a service in my code and then reverting back after receiving the response. I have implemented the use of ng-class to dynamically add the class when the boolean activeload1 is set to tr ...

Linking the checkbox to the previously specified form action in HTML

My form is set up with an action targeted for a specific task. I would like to add the ability to also have this form sent via email. This can be done using the :mailto option or another method. Mailto on submit button https://i.sstatic.net/9d5uS.png When ...

Is there a way to use the same color picker tool for adjusting both the background and text colors simultaneously?

My goal is to create functionality where the user can change the background color by clicking on the background of the div, and change text color by clicking on the text within the div. var box1 = document.querySelectorAll('.color1'); var pic ...

Adding a text field dynamically with angular2 can be achieved by using Angular's `

Hey everyone, I'm currently working on a project using angular2 and I've been attempting to dynamically add an input text field. It seems to be working fine but here's the code snippet: Typescript (TS): initArray() { return this._fb.gr ...

What is the process for deserializing a Json Object that contains an Array within it?

I need help processing JSON data that contains an array within an object. {"attr1":"value", "attr2":"value", "attr3": [{"chldattr1":"value", "chldattr2":"value"}], &q ...

Persistent vertical menu dropdown that remains expanded on sub menu pages

I am struggling to understand how to keep my menu sub items open when on the active page. Although I have tried similar solutions, I have not been successful in implementing them. I apologize if this question has been asked before. My approach involves usi ...

Assigning classes and data to each attribute by mapping a Json object using a for-each loop

I am struggling to separate the Jason data into individual divs with the class .name and the data-key za data attribute. I am encountering issues with getting the correct index for the data. My attempt so far has been buggy. While console.log successfully ...

Choosing a nested object within a JavaScript object

Is there a way to select the style contents of an object within another object? The data consists of a json encoded array. In my scenario, I am using data[i].replies and getting 2. How can I access data[i].userstyles instead? It seems to be an object. h ...

Creating a dependency between two checkboxes using jQueryFeedback: Looks good!

Looking for help with jQuery code to make two checkboxes dependent. When checkbox1 is checked, checkbox2 should automatically turn on. Here's my current code: //php $cus_centre = $this->createElement('checkbox', 'checkbox1'); ...

Enable erase functionality for touchscreen interactions (TouchEvent)

I have created a fun game where players must erase a colored layer to reveal an image. However, I am facing an issue as the game only works on my desktop and not on touchscreens like iPhones or iPads. I believe I need to replace MouseEvent with TouchEvent, ...

Establishing the default nested state in ui-router

I am facing an issue with two level nested states on ui-router where I cannot set a default nested state for a specific view. The challenge is to load the state cars.detail.supply.list when the state cars.detail is active without changing the current URL. ...

Change location of PHP key in associative array

The database generates the following array, with the key field "comp" being pushed into it. My goal is to navigate through the array and rearrange the order. Array Output Array ( [0] => Array ( [fname] => Cal ...

Storing Firebase credentials securely in Vue.js applications after deployment with environment variables (env_var)

I've been attempting to deploy my firebase&vue application, but I've encountered issues with adding firebase credentials to the environment variables. Here's the structure within vue.js: config ---- key.js ---- keys_dev.js ---- key ...

What is the method of using "*" as the value for allowedModules in a jsreport configuration file to enable all modules?

I am having an issue when trying to generate a report using jsreport STUDIO. The error message I received is as follows: An error occurred - Error during rendering report: Unsupported module in scripts: request. To enable require on a particular module, ...