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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:46:47+00:00 2026-05-28T06:46:47+00:00

I know it’s been here like thousand times, but I’m stuck now. I’ve read

  • 0

I know it’s been here like thousand times, but I’m stuck now. I’ve read plenty of answers and studied the code.google.com but didn’t succeed. I am trying to send a request in a chrome extension from background.html to contentscript.js. I managed to get it work the other way though.

Code inside background.html:

  chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
      console.log(response.farewell);
     });
  });

Code inside contentscript.js:

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({farewell: "nope"});
});

The manifest.json should be fine as the communication is working backwards and anything else works properly. Thank you!

  • 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-28T06:46:48+00:00Added an answer on May 28, 2026 at 6:46 am

    Your code is good, there must be something wrong with your trigger. I created a simple extension using your code and it worked. Add these two bits to your background and contentscript. Then rmb on any page and choose ‘Send Message.’ You should receive an alert.

    Background.html

    var menuid = chrome.contextMenus.create({"title":"Send Message", "onclick": SendMessage, "documentUrlPatterns":["http://*/*"]});
    
    function SendMessage() {        
        chrome.tabs.getSelected(null, function(tab) {
                chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
                    console.log(response.farewell);
                 });
        });
    }
    

    contentscript

    chrome.extension.onRequest.addListener(
      function(request, sender, sendResponse) {
        if (request.greeting == "hello") {
                alert('hello');
          sendResponse({farewell: "goodbye"});
            }
        else
            {
                alert('goodbye');
          sendResponse({farewell: "nope"});
            }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this sort of question has been asked here before, but still
I know this question has been around, but I found answers a bit foggy,
I know this has been asked a million times, but I haven't had much
Know this might be rather basic, but I been trying to figure out how
I know this is a stupid question, but I've looked for 45 mins now
I know regex and substrings is a common question on here but i can
I know the input have the maxlength, but I would like to have minlength
I know that there has been one question about this but it is not
I know the title is not great but I think the code will come
I know this has been asked but I am unable to fix it For

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.