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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:57:22+00:00 2026-05-23T02:57:22+00:00

Developing a chrome extension using javascript is one of my university projects. I don’t

  • 0

Developing a chrome extension using javascript is one of my university projects.

I don’t know how to establish a communication link between content script and background page using messaging. I need some help in this establishing the connection

background.html

chrome.tabs.getSelected(null, function(tab) { 
    chrome.tabs.sendRequest(tab.id, {method: "getHTML"}, function(response) { 
        console.log(response.data); 
    }); 
}); 

content_script.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
    if (request.method == "getHTML") 
        sendResponse({data: document.getElementById('header').innerHTML}); 
    else sendResponse({}); 
});
  • 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-23T02:57:23+00:00Added an answer on May 23, 2026 at 2:57 am

    A few major issues:

    1. You’re depending on some element on the page having the ID header. Such IDs are at the discretion of the site designer, so very few pages actually do have that (including Google). Maybe go for something a little more universal, like the title of the page (document.title).
    2. What does “the extension button” mean? If it means a browser action, that’s a part of your extension, so you’re correct in wanted to send something from the background script. This is also an easier case, as it’s probable that (aside from the issue above of no Google pages having an element of ID header), you’re just not capturing the browser action click event. If it’s some injected button, however, it’s the other way around.

    What you want (browser action version)

    background.html (inline):

    chrome.browserAction.onClicked.addListener(function(tab) {
      chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, { method: "getHTML"}, function(response) {
          console.log(response.data);
        });
      });
    });
    

    content_script.js

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
      if (request.method === "getHTML") {
        sendResponse({data: document.title});
      } else {
        sendResponse({});
      }
    });
    

    What you might want (injected button click version)

    background.html:

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
      if (request.method === "getHTML") {
        console.log(request.data);
      }
    });
    

    content_script.js:

    function buttonClick() {
      chrome.extension.sendRequest({method: "getHTML", data: document.title});
    }
    

    Code for response to comment below

    Very important recommendation: Chrome’s developer reference is probably one of the friendliest out there. If you want to know what parts of the chrome.* API are available, start there.

    function getHtml(tabId) {
      chrome.tabs.sendRequest(tabId, { method: "getHTML"}, function(response) {
        console.log(response.data);
      });
    }
    
    // Note that this will only work once a tab has loaded
    chrome.tabs.onSelectionChanged.addListener(function(tabId) {
      getHtml(tabId);
    });
    
    // This fires the first time a page is loaded
    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
      if (changeInfo.status === "complete") {
        getHtml(tabId);
      }
    });
    

    Code for second response to comment below

    background.html

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
      if (request.method === "getHTML") {
        console.log(request.data);
      }
    });
    

    content_script.js

    document.addEventListener("keypress", function(e) {
      chrome.extension.sendRequest({method: "getHTML", data: e.which});
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently developing a simple javascript extension for Chrome. The problem is simple but
I am developing a chrome extension that runs some javascript on a particular web
While developing a firefox extension, I create a wizard window from overlay.js using: this.wizard
I am developing a Firefox extension that uses XSL transformations. I have been using
I am developing a Google Chrome extension. When a popup is clicked, I would
I am developing an extension for Google Chrome. My background script, everytime, authorizes on
I'm developing an extension in Chrome 4 (currently 4.0.249.0) that will show the user's
I'm developing an extension for Google Chrome browser. I could not figure out how
I am developing an extension from Chrome and I need to do some changes
I'm having a particularly odd issue when developing Chrome extensions using jQuery. I have

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.