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 8946365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:29:33+00:00 2026-06-15T12:29:33+00:00

In my extension’s content script, I request data from background.js like so: fireOnNewTopic ();

  • 0

In my extension’s content script, I request data from background.js like so:

fireOnNewTopic ();  // Initial run on cold start or full reload.

window.addEventListener ("hashchange", fireOnNewTopic,  false);

function fireOnNewTopic () {
/*-- For the pages we want, location.hash will contain values
    like: "#!newtopic/{group title}"
*/
  if (location.hash) {
    var locHashParts    = location.hash.split ('/');
    if (locHashParts.length > 1  &&  locHashParts[0] == '#!newtopic') {
        var subjectStr  = '';
        var bodyStr  = '';
        switch (locHashParts[1]) {
            case 'opencomments-site-discussions':
            chrome.extension.sendMessage({name:"domain"},
                function(response)
                {
                subjectStr = response.domain;   
                });
            chrome.extension.sendMessage({name:"url"},
                function(response)
                {
                bodyStr = "URL of last page visited: " +                        response.url;   
                });
                break;
            default:
                break;
        }

        if (subjectStr && bodyStr) {
            runPayloadCode (subjectStr, bodyStr);
        }
    }
  }
}

Unfortunately, since sendMessage() runs asynchronously with the callback, at the time the code reaches runPayloadCode(), subjectStr and bodyStr are still null, since the code in background.js hasn’t completed. What’s the best way to synchronize the code so that subjectStr and bodyStr are filled in by the time runPayloadCode() is called?

  • 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-15T12:29:34+00:00Added an answer on June 15, 2026 at 12:29 pm

    To elaborate on what Sudarshan said about combining the two requests into one (sorry, need code so couldn’t just comment) here’s what you could do…
    send

    chrome.extension.sendMessage({url: true, domain:true}, function(response) {
      console.debug('The url is "'+response.url+'" and the domain is"'+response.domain+'"');
      if (repsone.url && response.domain) {
                runPayloadCode (subjectStr, "URL of last page visited: " + response.domain);
            }
    });
    

    or, if you wanted it more like your case way for some reason, maybe this is the sorta thing you’d like…

        if(location.hash) {
            var locHashParts = location.hash.split('/');
            if(locHashParts.length > 1 && locHashParts[0] == '#!newtopic') {
                var subjectStr = '';
                var bodyStr = '';
                var request = {};
                switch(locHashParts[1]) {
                case 'opencomments-site-discussions':
    
                    request = {
                        url: true,
                        domain: true
                    }
                    break;
                default:
                    break;
                }
    
                chrome.extension.sendMessage(request, function(response) {
                    console.debug('The url is "' + response.url + '" and the domain is"' + response.domain + '"');
                    if(repsone.url && response.domain) {
                        runPayloadCode(subjectStr, "URL of last page visited: " + response.domain);
                    }
                });
    
            }
        }
    }  
    

    listen

    chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
        var response = {};
        if(request.url) {
            response.url = "someURL";
        }
        if(request.domain) {
            response.domain = "someDomain";
        }
        sendResponse(response);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My extension use content-scripts to grab data from some pages. But, sometimes users use
The extension methods: Response.AsJson Response.AsXml works fine when calling it from the constractor like:
In my extension, I have a content script that creates a <div> containing an
background.js : chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { console.log(request.filter) chrome.webRequest.onBeforeRequest.addListener(request.func, request.filter, [blocking]); } ); content
easy_install python extension allows to install python eggs from console like: easy_install py2app But
How to get file extension from the request.files collection in asp.net?
ffmpeg extension is loaded as it is shown at phpinfo(), my file and script
In a Chrome Extension I am developing, I want to access a data structure
An extension to my previous question: Text cleaning and replacement: delete \n from a
My extension allows users to set their own background image on the extension's page.

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.