简体   繁体   English

Chrome扩展程序中的消息传递问题

[英]Message Passing issues in Chrome Extension

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: 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. 这些是Google文档中的示例,因此它们应该可以工作。

Any help? 有什么帮助吗? I'm completely lost. 我完全迷路了。

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. 阅读Chrome扩展程序代码vs内容脚本vs注入的脚本 ,了解更多信息。 To be precise, you're using a form of this method to execute code in the context of a web page . 确切地说,您正在使用此方法的一种形式在Web页面的上下文中执行代码。 Web pages do not have any access to the chrome.extension API. 网页无权访问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. 否则,您需要此方法才能正确执行代码。

sendRequest and onRequest are deprecated . sendRequestonRequest过时 You need to use sendMessage and onMessage . 您需要使用sendMessageonMessage

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. 另外,您正在向DOM注入函数,这使其在内容脚本上下文之外运行,因此chrome.extension API不再可用于此函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM