1

I've been searching all over SO and reading through google docs but I can't seem to find a solution.

My Chrome extension is injecting a content script and I want to set an onRequest.listener in order to sendRequests to the content script. This is the script I used to for the onRequest.listener. The problem is I keep getting this error for some unknown reason.

Error Message:

Uncaught TypeError: Cannot ready property 'onRequest' of undefined

contentscript.js line 1;

Here's the relevant code...

Manifest.json

{
  "name": "Injector Extension",
  "version": "1.0",
  "manifest_version": 1,
  "icons": { "128": "icon.png" },
  "browser_action": {
    "default_icon": "icon.png",
    "default_title": "Injector Extension",
    "default_popup": "popup.html"
  },
  "options_page": "options.html",
  "background": {
    "page": "background.html"
  },
  "permissions": [
    "tabs", 
    "http://*/*",
    "https://*/*",
    "unlimitedStorage"],
  "content_scripts": [{
        "matches": [" (injector specific url) "],
        "js": ["contentscript.js"]
  }],
  "web_accessible_resources": ["js/script.js"] 
}

content script

 chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (request.method == "fromPopup") {

    // Send JSON data back to Popup.
    sendResponse({data: "from Content Script to Popup"});

  } else {
     sendResponse({}); // snub them.
  }
});

popup

chrome.tabs.getSelected(null, function(tab) {
   chrome.tabs.sendRequest(tab.id, {method: "fromPopup", tabid: tab.id}, function(response) {
      console.log(response.data);
   });
});
Community
  • 1
  • 1
Nick Fury
  • 1,313
  • 3
  • 13
  • 23
  • Please don't post images of your debugger, just post the error message and in which line of the given code the error happens. – Bergi May 16 '12 at 22:30

1 Answers1

5

chrome.extension.onRequest.addListener works only in extension context. It won't run inside a content script.

chrome.extension.sendRequest works in content script context

Update accordingly and will work.

Edit: Exemplifying simple message passing:

Extension script:

chrome.extension.onRequest.addListener(function(r,s,sr){ 
     if(r==='HELLO') return sr.call(this,'BACK AT YOU');
});

Content script:

chrome.extension.sendRequest('HELLO', function(data){ alert(data); });
// will alert "BACK AT YOU"
Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
  • So whats the best way to send requests to my content script? – Nick Fury May 17 '12 at 00:10
  • Look at the docs; Without complex message passing, `sendRequest` accepts a `responseCallback` function as a second parameter; this callback has a `response` parameter which the `onRequest` handler can send back to the content script, by calling `sendResponse` (the 3rd parameter) with a parameter. I'll update with an example just hold on. – Silviu-Marian May 17 '12 at 00:20
  • Oh okay, that's no big deal I understand the concept. I was just wondering if I should do the `response callback` or `Long-lived connections`. I think you've answered my question. Thanks again! – Nick Fury May 17 '12 at 00:21
  • 90% of situations don't need `Long-lived connections`; you're welcome – Silviu-Marian May 17 '12 at 00:26
  • I tried sendRequest and I got the same `Uncaught TypeError` but this time it was `sendRequest` – Nick Fury May 17 '12 at 17:59
  • 1
    Like I said, it will only work once and on extension side. It's really easy to lose track of contexts; and if it satisfies an `onRequest` subsequent `sendRequest` won't work anymore. Like IronMan's red lasers. – Silviu-Marian May 18 '12 at 12:19