Transferring data from Content.js to Background.js

Is there a way for me to access a variable that I created in my content.js file from my background.js file?

Whenever I try to use a variable in my background.js file that was defined in my content.js file, I encounter a chrome error stating "Unexpected Identifier" (referring to ContentTag1 in background.js)

Any suggestions on how I can achieve this?

Snippet from Background.js File

function appendData(data) {
            var mainContainer = document.getElementById("myData");
            for (var i = 0; i < data.length; i++) {
                if data[i].DBTag == ContentTag1 {
                    var div = document.createElement("div");
                    div.innerHTML = 'Name: ' + data[i].Name + ' ' + data[i].Price;
                    mainContainer.appendChild(div);
                }
            }
        }

Snippet from Content Script

var ContentTag1 = "test";
var ContentTag2 = "test";
var ContentTag3 = "test";

Manifest Details:

{
  "name": "App",
  "version": "1.0",
  "description": "Description",
  "permissions": [ "activeTab", "declarativeContent", "storage", "http://localhost:3000/brands"],
  "background": {
    "scripts": [ "js/lib/jquery.min.js", "js/app/background.js"],
    "persistent": false
  },
  "content_scripts": [
    {
      "matches": [ "https:/somesite*" ],
      "css": ["style.css"],
      "js": [ 
        "js/lib/jquery.min.js",
        "js/app/content.js"
        ],
      "all_frames": false
    }
  ],
  "web_accessible_resources": ["content.html", 
                               "content.css",
                               "css/popup.css"
                             ],
  "browser_action": {
    "default_popup": "views/popup.html",
    "default_icon": {
      "16": "images/someimage.png",
      "32": "images/someimage.png",
      "48": "images/someimage.png",
      "128": "images/someimage.png"
    }
  },
  "icons": {
    "16": "images/someimage.png",
    "32": "images/someimage.png",
    "48": "images/someimage.png",
    "128": "images/someimage.png"
  },
  "manifest_version": 2
}

Answer №1

To successfully communicate between content and background, it's important to send a message and then add a listener on the background.

If you're looking for an example of how to do this, check out this answer.

For more information on messaging in Chrome extensions, you can refer to the documentation available here.

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

Node application experiencing issues retrieving SVG files during production

When testing my application locally, the svg files display correctly with the code below (Angular.js variables are used within curly brackets): <img ng-src="img/servant_{{servant.personality}}.svg" draggable="false"> However, once deployed on Herok ...

Saving an Image from HTML5 <canvas> using Java Servlet

Despite the numerous StackOverflow questions on this topic, I have gone through many of them without success. Now, I am reaching out with my own question. My objective is to save an image from an HTML5 <canvas> on my webpage to a file on my server u ...

Issue with showing an input field following the button click

I'm having a bit of trouble with this function that should add an input element to a div. I've tried using appendChild and also concatenating the object to innerHTML, but no luck so far. Could it be something my beginner's mind is missing? f ...

Retrieve information from a dropdown menu that is dependent on the selected value from another dropdown

   Check out this sample API data - I currently have three dropdown menus set up: From Release To Release Compatibility When a specific from release is selected, all corresponding to releases associated with that specific from release should be displ ...

Oops! We couldn't locate or access the file you're trying to import: ~bootstrap/scss/bootstrap

Following the steps outlined on getbootstrap.com, I assumed that everything would function smoothly. Unfortunately, that hasn't been the case so far. All seems well until I attempt to load the page, at which point my Express.js app throws a: [[ ...

What could be causing my server to not fully read my json file?

I created a function that sends a get request for fav.json and assigned the id="fav_button" button to execute this function. Additionally, I configured an express server. However, upon clicking the button, only the last item in the json file is displayed. ...

Deselect a checkbox that is already selected and choose the current option in the multiselect dropdown

I've created a code that allows for single select in a multiselect dropdown, disabling other options once one is chosen. However, I don't want to disable the checkboxes and would like to uncheck the selected option if a new one is chosen, all whi ...

What could be causing my Vue code to behave differently than anticipated?

There are a pair of components within the div. When both components are rendered together, clicking the button switches properly. However, when only one component is rendered, the switch behaves abnormally. Below is the code snippet: Base.vue <templa ...

Automatically update local library dependencies with npm

Utilizing the local package dependency functionality of npm, I have implemented it in the following manner: npm install --save "file:/path/to/module" After updating my library module by running npm run build to generate dist files, I then execut ...

What is the best approach to removing a component by utilizing the children prop in React?

I am currently working on a specific scenario: In my project, I have a component called Foo with a property named bar If the bar property is set to true, then I need to display the Bar component that is nested inside the Foo component If the bar prop ...

Expanding the functionality of express.Router

Can someone help me with how to extend the functionality of express.Router? I attempted the following: class Test extends express.Router() { }; However, I encountered an error when trying this in Express. Does anyone have a solution for this issue? ...

Utilize model instance methods in Sails.js for enhanced functionality within views

Is there a way to retain model instance functions when passed to a view? For example, my User model has a method called fullName which combines first name, last name, and prefix. In the controller: User.find().done(function(err,users){ res.view({ ...

Navigating between different components in React Router V4 allows for seamless transitions without having to reload the

I am currently learning React with React Router V4 and I have a specific scenario in mind that I would like to achieve, possibly illustrated by the image below: Click on the "Next" button Trigger a click event to Component A ("button got clicked") Upon c ...

Organize tabs with ease in the bootstrap-tabdrop plugin

My webpage features a navigation bar along with the bootstrap-tabdrop plugin, which places tabs in a dropdown menu if there are too many to display on one line. The main objective is: No action when navigating through currently displayed tabs. Clicking o ...

The duration of user engagement on a website with an embedded iframe

I am working on measuring the time users spend on web pages, excluding the time they navigate away from the browser. While exploring open source libraries like Timejs, I noticed that these tools do not accurately track user time when they are watching vid ...

PHP - Issue with submitting form data using $_POST from within a Div text container

Having some trouble with the submission of the "about_me_container" div in my form using $_POST. I know that divs are not typically used with $_POST, so I tried changing the div to input type="text" and textarea but it still won't work. I also attempt ...

Populate the dropdown list with option values based on the selection made in the preceding dropdown menu

I have a coding challenge with two drop-down lists. One is labeled "Specialization" and contains options like General, Cardiologist, Pediatrician, etc. The second one is labeled "Doctor" and includes the names of different doctors as options. What I am try ...

How do I dynamically generate a new ngController in AngularJS?

Is it possible to dynamically create new ngControllers without using ngRepeat in the template? The scenario is that the user may never instantiate a particular ngController. Here's an example of the current template: <div ng-controller="ViewUsers ...

Learn the steps for accessing and utilizing aria-label text in Google Chrome

Struggling with the aria-label attribute for blind users in Chrome. Can't seem to hear the aria-label text. Any insights on how aria-label behaves in Chrome or if I'm using the wrong approach? If I'm heading in the wrong direction or using ...

Unable to use OrbitControls with Node 12's ES6 import functionality

Currently, I am working with Node 12 (experimental-modules) and using npm for three.js. However, I'm facing issues with Imports when trying to include OrbitControls.js in my project. My index.js file is set as "script: module". Unfortunately, none of ...