Passing data from content script to background script

7,032 views
Skip to first unread message

Rodrigo Mello

unread,
Feb 15, 2014, 5:34:21 PM2/15/14
to chromium-...@chromium.org
I'm trying my first extension and need to pass massages between my content script and the background script. However, the content script always returns undefined. I tried to send the message request more than once, because I saw a post that said that sometimes just the first time does not work, but that did not solve my problem. Here are the codes:
Background:
chrome.browserAction.onClicked.addListener(function () {
chrome.tabs.query({active:true, currentWindow:true}, function(tabs)
    {
chrome.tabs.executeScript(tabs[0].id, {file:"content.js"}, function ()
{
console.log("sending first time");
console.log(tabs[0].id);
chrome.tabs.sendMessage(tabs[0].id,{test: "test"}, function (response)
{
if (undefined == response)
{
console.log ("first one didn't work");
chrome.tabs.sendMessage(tabs[0].id, {test: "test"}, function (response)
{
console.log(response.test);
});
}
});
});
});
});

Content Script:
chrome.runtime.onMessage.addListener( function (msg, sender, sendResponse)
{
/*var test1 = document.getElementsByTagName("h1")[0];
test1.innerHTML="test";*/
sendReponse({test: "123"});
return true;
/*var parentList =document.getElementsByTagName("a");
var link = "";
var child;
for (var i=2; i<parentList.length;i++)
{
child=parentList[i].firstChild;
if ("IMG"==child.nodeName)
{
link=parentList[i].href;
break;
}
}
teste1.innerHTML=link;
var answer = new Object();
Answer.link = link;
teste1.innerHTML=answer.link;
sendResponse({messager:answer});*/
});

manifest:
{
  "name": "Chrome Message Test",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Bug or not?",
  "permissions": [
    "tabs"
  ],
"app": {
    "launch": {
      "local_path": "caller.html"
    }
  }
}
Thanks! 

Rodrigo Mello

unread,
Feb 15, 2014, 5:41:48 PM2/15/14
to chromium-...@chromium.org
Sorry, I sent the wrong manifest.
manifest:
{
"manifest_version":2,
"name":"Thumbnail Opener",
"description":"Opens all thumbnails at once",
"version":"0.1",

"browser_action":
{
"default_title":"Thumbnail Opener",
"default_icon":"icon.png"
},

"background":
{
"scripts": ["opener.js"],
"persistent": false
},
"permissions":
[
"http://*/*",
"activeTab"
],
"content_scripts":
[
{
"matches":["http://*/*"],
"js":["viewer.js"]
}
]
}

Rodrigo Mello

unread,
Feb 18, 2014, 11:10:15 PM2/18/14
to chromium-...@chromium.org
Still no clue... stackoverflow couldn't solve it either


On Saturday, February 15, 2014 7:34:21 PM UTC-3, Rodrigo Mello wrote:

Alexandre Barreira

unread,
Feb 19, 2014, 5:05:20 AM2/19/14
to chromium-...@chromium.org
There's a typo in your content script: "sendReponse({test: "123"});" => use sendResponse instead.

Also, make sure you check that you haven't already injected the content script into a page before injecting it again (you can do it in the background script or in the injected script..), otherwise you'll be setting too many communication handlers on the page (except if that's what you're trying to achieve.. :) )

Rodrigo Mello

unread,
Feb 19, 2014, 5:49:07 PM2/19/14
to chromium-...@chromium.org
Thank you! But it is still not working. As to injecting unnecessarily, I changed my manifest.json to solve that. 
New manifest:
{
"manifest_version":2,
"name":"Thumbnail Opener",
"description":"Opens all thumbnails at once",
"version":"0.1",
"browser_action":
{
"default_title":"Thumbnail Opener",
"default_icon":"icon.png"
},
"background":
{
"scripts": ["opener.js"],
"persistent": false
},
"permissions":
[
"http://*/*",
"activeTab"
]
}

Any other ideas?

Rodrigo Mello

unread,
Feb 19, 2014, 7:06:51 PM2/19/14
to chromium-...@chromium.org
Looking at a sample code, I found out I had to change my permissions from "http://*/*" to "*://*/*" Didn't understand why, though.
Thanks for all help
P.S.: If someone could explain why I had to make that change, it would be awesome.

Rodrigo Mello

unread,
Feb 19, 2014, 7:40:51 PM2/19/14
to chromium-...@chromium.org
OK, now I got it all. I was testing the script at the extensions page! That's why http://*/* wasn't working. Also, it seems you can't inject javascript in the extensions page (probably because it is a settings page in the browser). The script works fine if I use it on ordinary pages.

Alexandre Barreira

unread,
Feb 20, 2014, 4:18:48 AM2/20/14
to chromium-...@chromium.org
Hehe, classic mistake :)

As for the *://*/* permission (host permission), that's what allows you to do advanced interaction with pages, like injecting content scripts. With the http://*/* pattern, you were only allowed to interact with http pages, but not with https ones. Check out this page for full details: http://developer.chrome.com/extensions/declare_permissions#host-permissions
Reply all
Reply to author
Forward
0 new messages