1

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;  
});
FractalBob
  • 3,225
  • 4
  • 29
  • 40
  • 1
    Seems to work fine. Can you post a minimal *tested* example to show your problem? (manifest.json, contentscript.js and background.js), with a method to set the values and a way to read the values. – Rob W Nov 29 '12 at 23:54

1 Answers1

2

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.

Community
  • 1
  • 1
Sudarshan
  • 18,140
  • 7
  • 53
  • 61
  • I need to rephrase the problem. Actually, it has more to do with the way the data is being used and the nature of the page that's being launched. The page is a Google Groups page and when Google changed it a year or so ago, the changes broke my extension and I'm struggling to fix it. Stay tuned. – FractalBob Nov 30 '12 at 07:07
  • Thanks, Sudarshan. Your code works fine. I think it will help a lot. – FractalBob Nov 30 '12 at 21:14