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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:12:40+00:00 2026-05-24T05:12:40+00:00

I am writing a Google Chrome extension. I have reached the stage where I

  • 0

I am writing a Google Chrome extension. I have reached the stage where I can pass messages back and forth readily but I am running into trouble with using the response callback. My background page opens a message page and then the message page requests more information from background. When the message page receives the response I want to replace some of the standard text on the message page with custom text based on the response. Here is the code:

chrome.extension.sendRequest({cmd: "sendKeyWords"}, function(response) {
    keyWordList=response.keyWordsFound;
        var keyWords="";
        for (var i = 0; i<keyWordList.length; ++i)
        {
            keyWords=keyWords+" "+keyWordList[i];
        }
        document.getElementsByClassName("comment")[1].firstChild.innerHTML=keyWords;
        alert (document.getElementsByClassName("comment")[1].firstChild.innerHTML);
});

FIRST QUESTION: This all seems to work fine but the text on the page doesn’t change. I am almost certainly because the callback completes after the page is finished loading and the rest of the code finishes before the callback completes, too. How do I update the page with the new text? Can I listen for the callback to complete or something like that?

SECOND QUESTION: The procedure I am pursuing first opens the message page and then the message page requests the keyword list from background. Since I always want the keyword list, it makes more sense to just send it when I create the tab. Can I do that? Here is the code from background that opens the message page:

//when request from detail page to open message page 
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "openMessage") {
        console.log("Received Request to Open Message, Profile Score: "+request.keyWordsFound.length);
        keyWordList=request.keyWordsFound;
        chrome.tabs.create({url: request.url}, function(tab){
        msgTabId=tab.id; //needed to determine if message tab has later been closed
        chrome.tabs.executeScript(tab.id, {file: "message.js"});
        });
        console.log("Opening Message");
    }
});

Along the lines of the second question, I also tried this in background:

//when request from detail page to open message page 
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "openMessage") {
        console.log("Received Request to Open Message, Profile Score: "+request.keyWordsFound.length);
        keyWordList=request.keyWordsFound;
        var keyWords="";
        for (var i = 0; i<keyWordList.length; ++i)
        {
            keyWords=keyWords+" "+keyWordList[i];
        }
        console.log(keyWords);
        chrome.tabs.create({url: request.url}, function(tab){
        msgTabId=tab.id; //needed to determine if message tab has later been closed
        chrome.tabs.executeScript(tab.id, {code: "document.getElementsByClassName('comment')[1].firstChild.innerHTML=keyWords;", file: "message.js"});
    });
        console.log("Opening Message");
    }
});

But this doesn’t work either, it just breaks and neither script is executed.

  • 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-05-24T05:12:41+00:00Added an answer on May 24, 2026 at 5:12 am

    I can answer the first part of the question after reading a previous answer by @serg. The problem stems from the asynchronous nature of sendRequest. A callback is required. Here is the code that works:

    function getKeyWords(action, callback){
        chrome.extension.sendRequest(
                {
                    cmd: action
                },
                function(response)
                {
                    callback(response.keyWordsFound);
                }
        );
    }
    
    var keyWords="";
    getKeyWords("sendKeyWords", function(reply) {
        keyWordList=reply;
    
        for (var i = 0; i<keyWordList.length; ++i)
        {
            keyWords=keyWords+" "+keyWordList[i];
        }
        msgComment1.innerHTML="<strong>"+keyWords+"</strong>";
        console.log("reply is:", keyWords);
    });
    

    Once again, I am indebted to @serg. Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a linker for a Google Chrome Extension. In the gwt.xml file,
I'm writing a Google Chrome extension that needs to do a lot of things
I'm writing a Google Chrome extension. As the JavaScript files are loaded from disk,
I'm writing a small Chrome extension to manipulate Google's search results' DOM to add
I am writing a Google Chrome Extension and trying to make the transition to
I am writing an page action extension for Google Chrome. The extension injects the
I am writing an extension for Google Chrome. The content script never sees that
I am writing a Google Chrome extension with the help of Google closure compiler.
I'm writing a Chrome extension to add Google Maps autocomplete to Google Calendar's new
I am writing a Chrome extension that synchronizes with Opera Link, but I keep

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.