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

The Archive Base Latest Questions

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

In background.js, I store some data into local storage: localStorage[domain] = site; //Save the

  • 0

In background.js, I store some data into local storage:

localStorage["domain"] = site;  //Save the site to local storage for retrieval later when requested by the content script
localStorage["page"] = tab.title;   //Save the page title to local storage for retrieval later
localStorage["url"] = tab.url;  //Save the URL of the current page to local storage for retrieval

Later, my content script requests the data with

chrome.extension.sendRequest({name:"domain"},
    function(response)
    {
    subjectStr = response.domain;   
    });

chrome.extension.sendRequest({name:"url"},
    function(response)
    {
    bodyStr = "URL of last page visited: " + response.url;  
    });

and background.js responds with

//Wait for request for the site value and URL from content script 
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) 
  {
  if (request.name == "url")
  {
  sendResponse({url: localStorage["url"]});
  }
  else
  {
  sendResponse({domain: localStorage["domain"] + ": " + localStorage["page"]});
  }
}
);

However, the data is never received by the content script. Anyone see why?

Here’s the manifest:

{
"name": "Test",
"version": "1.0",
"manifest_version": 2,
"description": "Test extension",
"browser_action": {
"default_icon": "no_msgs.png",
"default_title": "Press here to test."
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [{
"run_at": "document_end",
"js": ["postMsg.js"],
"matches": ["https://groups.google.com/forum/*"]
}],
"permissions": ["tabs",
"http://groups.google.com/forum/?fromgroups=#!forum/opencomments-site-discussions/*",
"https://groups.google.com/forum/?fromgroups=#!forum/opencomments-site-discussions/*"
]
}

and no_msgs.png:

no_msgs http://www.opencomments.com/no_msgs.png

and background.js:

var post_url = "https://groups.google.com/forum/?fromgroups=#!newtopic/opencomments-site-discussions";

chrome.browserAction.onClicked.addListener(function(main) {
});

function createEvent(tab){
}

function updateEvent(tabId, changeInfo, tab){
}

function miscEvent(tabId, eventInfo){
}

function getURL() {
  chrome.tabs.getSelected(undefined, function(tab) {
var tmp = tab.url;
var site;

if (tab.url.indexOf("http://") == 0 || tab.url.indexOf("https://") == 0) {
    site = getDomain(tab.url);
    chrome.tabs.create({url: post_url});

localStorage["domain"] = site;  //Save the site to local storage for retrieval later when requested by the content script
localStorage["page"] = tab.title;   //Save the page title to local storage for retrieval later
localStorage["url"] = tab.url;  //Save the URL of the current page to local storage for retrieval
}
  });
 }

//Wait for request for the site value and URL from content script 
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) 
  {
  if (request.name == "url")
    {
    sendResponse({url: localStorage["url"]});
    }
  else
    {
    sendResponse({domain: localStorage["domain"] + ": " + localStorage["page"]});
    }
  }
  );

//Fetches the domain from the URL
function getDomain(url){
var tmp = url.substring(url.indexOf("//") + 2);
var tmp2 = tmp.indexOf("/");
var str = tmp.substring(0, tmp2);
var index = str.indexOf(".");
while ((tmp = str.substring(index + 1)).indexOf(".") != -1){
str = str.substring(index + 1);
index = str.indexOf(".");
}
index = str.indexOf(".");
return str;
}

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
getURL();
});

and finally postMsg.js:

var subjectStr  = '';
var bodyStr  = '';

chrome.extension.sendRequest({name:"domain"},
function(response) {
subjectStr = response.domain;   
});

chrome.extension.sendRequest({name:"url"},
function(response) {
bodyStr = "URL of last page visited: " + response.url;  
});
  • 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-15T09:44:33+00:00Added an answer on June 15, 2026 at 9:44 am

    Works fine with your code, i used messages instead of requests.

    Follow Rob W posts(link1, link2) for more information on sendMessage() vs sendRequest()

    SAMPLE CODE AND OUTPUT

    Output from background.js

    enter image description here

    Output from Content Script scripts.js

    enter image description here

    manifest.json

    {
    "name":"Local Storage",
    "description":"Local Storage Demo",
    "manifest_version":2,
    "background":{
        "scripts":["background.js"]
    },
    "content_scripts": [
        {
          "matches": ["https://www.google.co.in/*"],
          "js": ["scripts.js"]
        }
      ],
    "permissions":[
        "tabs","<all_urls>"
    ],
    
    "version":"1"
    }
    

    background.js

    function storeData(){
        localStorage["domain"] = "google";  //Save the site to local storage for retrieval later when requested by the content script
        localStorage["page"] = "Google";   //Save the page title to local storage for retrieval later
        localStorage["url"] = "https://www.google.co.in/";  //Save the URL of the current page to local storage for retrieval
    }
    chrome.extension.onMessage.addListener(
      function(request, sender, sendResponse) 
      {
      console.log("request recieved is "+request);
      if (request.name == "url")
      {
        console.log("sending response for request URL"+ localStorage["url"]);
      sendResponse({url: localStorage["url"]});
      }
      else
      {
      console.log("sending response for request URL"+   localStorage["page"]);
      sendResponse({domain: localStorage["domain"] + ": " + localStorage["page"]});
      }
    }
    );
    window.onload = function(){
        storeData();
    }
    

    scripts.js

    function requestBackground(){
        chrome.extension.sendMessage({name:"domain"}, 
        function(response)
        {
        console.log("response recived for response.domain  "+response.domain);   
        });
    
    chrome.extension.sendMessage({name:"url"},
        function(response)
        {
        console.log("response recived for last page visited: " + response.url);  
        });
    }
    
    window.onload = function(){
        requestBackground();
    }
    

    Let me know if it still fails.

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

Sidebar

Related Questions

Recently I want to store some social graph information into our database, and user
Background: We're building an application that allows our customers to supply data in a
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Background The Von-Neumann architecture describes the stored-program computer where instructions and data are stored
I am rebuilding the background system of a site with a lot of traffic.
First some background on the questions. I have never used ravedb before and I'm
I'm having some issues with maintaining scroll bar locations. The Background I have a
I'm trying to use Android Application class (MyApplication.java) for storing data in some ArrayLists
I have some problems on elaborating a useful strategy to support background for NSOperationQueue
How would you tackle this problem: I have data in my data store. Each

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.