0

I'm trying to pass a message from my content script to my background page. This error occurs when the content script is executed:

Uncaught TypeError: Cannot call method 'sendRequest' of undefined 

Content Script:

function injectFunction(func, exec) {
    var script = document.createElement("script");
    script.textContent = "-" + func + (exec ? "()" : "");
    document.body.appendChild(script);
}

function login() {

    chrome.extension.sendMessage({greeting: "hello"}, function(response) {
        console.log(response.farewell);
    });

    var d = window.mainFrame.document;
    d.getElementsByName("email")[0].value = "I need the response data here";
    d.getElementsByName("passwort")[0].value = "Here too.";
    d.forms["login"].submit();
}

injectFunction(login, true);

Background:

 chrome.extension.onMessage.addListener(
 function(request, sender, sendResponse) {
      if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
 });

manifest.json:

{
    "name": "Sephir Auto-Login",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Contact x@x.com for support or further information.",
    "options_page": "options.html",
    "icons":{
        "128":"icon.png"
    },
    "background": {
        "scripts": ["eventPage.js"]
    },
    "content_scripts": [
        {
          "matches": ["https://somewebsite/*"],
          "js": ["login.js"]
        }, 
        {
          "matches": ["somewebsite/*"],
          "js": ["changePicture.js"]
        }
    ],
     "permissions": [
        "storage",
        "http://*/*",
        "https://*/*",
        "tabs"
    ]
}

Those are the examples on the documentation from google, so they should work.

Any help? I'm completely lost.

dislick
  • 667
  • 1
  • 7
  • 25
  • Unable to reproduce. I'm certain that you're not using that script as a content script (perhaps an injected ` – Rob W Oct 08 '12 at 08:35
  • @RobW Thanks for your effort! I edited my whole content script into it. What do you need more? – dislick Oct 08 '12 at 08:38

2 Answers2

2

The problem is caused by your misunderstanding of the script executing environment. Read Chrome extension code vs Content scripts vs Injected scripts for more information. To be precise, you're using a form of this method to execute code in the context of a web page. Web pages do not have any access to the chrome.extension API.

I suggest to rewrite your code to not use injected scripts, because it's not necessary in this case.

function login() {

    chrome.extension.sendRequest({greeting: "hello"}, function(response) {
        console.log(response.farewell);
    });

    var d = document.getElementById('mainFrame').contentDocument;
    d.getElementsByName("email")[0].value = "I need the response data here";
    d.getElementsByName("passwort")[0].value = "Here too.";
    d.forms["login"].submit();
}

login();

* Only works if the frame is located at the same origin. Otherwise, you need this method to execute code correctly.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Thanks, I think I understand it now a bit more. Anyways, I now get this error: Port error: Could not establish connection. Receiving end does not exist. – dislick Oct 08 '12 at 08:54
  • @dislick Make sure that you use either `onMessage` + `sendMessage` *or* `onRequest` + `sendRequest`. Even though these methods have a similar signature, they are [**not** compatible](http://stackoverflow.com/questions/11811554/chrome-extension-port-error-could-not-establish-connection-receiving-end-does/11811936#11811936). – Rob W Oct 08 '12 at 08:57
1

sendRequest and onRequest are deprecated. You need to use sendMessage and onMessage.

Also, you are injecting function to the DOM, that makes it run outside the content script context so chrome.extension API is no longer available for this function.

Konrad Dzwinel
  • 36,825
  • 12
  • 98
  • 105