Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7816163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:46:00+00:00 2026-06-02T05:46:00+00:00

I am having trouble getting my content script request values from my background script.

  • 0

I am having trouble getting my content script request values from my background script.

content_script.js
=================
var elements = undefined
var properties = undefined
var targets = undefined
chrome.extension.sendRequest({greeting: "elements"}, function(response) {
    elements = response.input;
});
if (elements == undefined){
    var elements = ["a","img"];
}else{
    elements = elements.split(',');
}
chrome.extension.sendRequest({greeting: "properties"}, function(response) {
    properties = response.input;
});
if (properties == undefined){
    var properties = ["alt","id","class"];
}else{
    properties = properties.split(',');
}
chrome.extension.sendRequest({greeting: "targets"}, function(response) {
    targets = response.input;
});
if (targets == undefined){
    var targets = ["onclick","href"];
}else{
    targets = targets.split(',');
}...
...More code and references to elements following...

The above code only works when there is a break in the code (ie waiting) before doing any relating to the values set above, I suppose I could put something to do that but i would prefer to use a more efficient solution if possible.

(for reference:)

background.js
=============
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
    switch (request.greeting){
        case "elements":
                var elements = localStorage["elements"];
                sendResponse({input: elements});
                break;
        case "properties":
                var properties = localStorage["properties"];
                sendResponse({input: properties});
                break;
        case "targets":
                var targets = localStorage["targets"];
                sendResponse({input: targets});
                break;
        }
  });

I have been at this for 3 hours (still learning what I’m doing with JS)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:46:02+00:00Added an answer on June 2, 2026 at 5:46 am

    Uhhhhhm you cannot do that:

    chrome.extension.sendRequest({greeting: "properties"}, function(response) {
        properties = response.input;
    });
    if (properties == undefined){ // always undefined
        var properties = ["alt","id","class"];
    }else{
        properties = properties.split(',');
    }
    

    sendRequest is async.

    And the correct way of checking for undefined is:

    if (typeof properties == "undefined") 
    

    If you need all that stuff just make 1 request and do all your stuff inside the callback:

    chrome.extension.sendRequest({greeting: "all"}, function(response) {
        if (response.elements === null) {
            var elements = ["a","img"];
        } else {
            var elements = response.elements.split(',');
        }
    
        if (response.properties === null) {
            var properties = ["alt","id","class"];
        } else {
            var properties = response.properties.split(',');
        }
    
        if (response.targets === null) {
            var targets = ["onclick","href"];
        } else {
            var targets = response.targets.split(',');
        }
    });
    

    And in background:

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
        switch (request.greeting){
            case "all":
                sendResponse({
                    elements: localStorage.getItem("targets"),
                    properties: localStorage.getItem("properties"),
                    targets: localStorage.getItem("targets")
                });
                break;
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble getting my content script to communicate with my background page. I'm
I am having trouble getting dynamic content coming from a custom handler to be
I'm having trouble getting my table to behave. The content keeps overflowing and my
I am having trouble getting a background-image to overlay the border of another div.
I'm having trouble getting any information to display from this query. Anyone know where
I'm having trouble getting the following JQuery script to function properly - its functionality
I'm having trouble getting my cruisecontrol script to do something. I want to call
I'm having trouble getting the elements in my <div id=wrapper> to push down my
I am having trouble getting my background image to span the entirety of my
I'm having trouble getting jQuery to allow only one content-DIV to be visible at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.