【发布时间】:2012-09-28 11:20:39
【问题描述】:
我正在尝试将消息从我的内容脚本传递到我的后台页面。执行内容脚本时出现此错误:
Uncaught TypeError: Cannot call method 'sendRequest' of undefined
内容脚本:
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);
背景:
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"
]
}
这些是 google 文档中的示例,因此它们应该起作用。
有什么帮助吗?我完全迷路了。
【问题讨论】:
-
无法重现。我确定您没有将该脚本用作内容脚本(可能是注入的
<script>?)您能否展示代码的真正重要的结构? -
@RobW 感谢您的努力!我将整个内容脚本编辑到其中。你还需要什么?
标签: javascript google-chrome google-chrome-extension